Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Config/Schema/ArrayElement.php
- 9 core/lib/Drupal/Core/Config/Schema/ArrayElement.php
Namespace
Drupal\Core\Config\Schema
File
core/lib/Drupal/Core/Config/Schema/ArrayElement.php
View source
<?php
namespace Drupal\Core\Config\Schema;
use Drupal\Core\TypedData\ComplexDataInterface;
abstract class ArrayElement extends Element implements \IteratorAggregate, TypedConfigInterface, ComplexDataInterface {
protected $elements;
public function hasTranslatableElements() : bool {
foreach ($this as $element) {
if ($element
->getDataDefinition()['translatable'] === TRUE) {
return TRUE;
}
if ($element instanceof ArrayElement && $element
->hasTranslatableElements()) {
return TRUE;
}
}
return FALSE;
}
protected function getAllKeys() {
return is_array($this->value) ? array_keys($this->value) : [];
}
protected function parse() {
$elements = [];
foreach ($this
->getAllKeys() as $key) {
$value = $this->value[$key] ?? NULL;
$definition = $this
->getElementDefinition($key);
$elements[$key] = $this
->createElement($definition, $value, $key);
}
return $elements;
}
protected abstract function getElementDefinition($key);
public function get($name) {
$parts = explode('.', $name);
$root_key = array_shift($parts);
$elements = $this
->getElements();
if (isset($elements[$root_key])) {
$element = $elements[$root_key];
while ($element && ($key = array_shift($parts)) !== NULL) {
if ($element instanceof TypedConfigInterface) {
$element = $element
->get($key);
}
else {
$element = NULL;
}
}
}
if (isset($element)) {
return $element;
}
else {
throw new \InvalidArgumentException("The configuration property {$name} doesn't exist.");
}
}
public function getElements() {
if (!isset($this->elements)) {
$this->elements = $this
->parse();
}
return $this->elements;
}
public function isEmpty() {
return empty($this->value);
}
public function toArray() {
return $this->value ?? [];
}
public function onChange($name) {
if (isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
public function getIterator() {
return new \ArrayIterator($this
->getElements());
}
protected function createElement($definition, $value, $key) {
return $this
->getTypedDataManager()
->create($definition, $value, $key, $this);
}
protected function buildDataDefinition($definition, $value, $key) {
return $this
->getTypedDataManager()
->buildDataDefinition($definition, $value, $key, $this);
}
public function isNullable() {
return isset($this->definition['nullable']) && $this->definition['nullable'] == TRUE;
}
public function set($property_name, $value, $notify = TRUE) {
$this->value[$property_name] = $value;
return $this;
}
public function getProperties($include_computed = FALSE) {
$properties = [];
foreach (array_keys($this->value) as $name) {
$properties[$name] = $this
->get($name);
}
return $properties;
}
}
Classes
Name |
Description |
ArrayElement |
Defines a generic configuration element that contains multiple properties. |