summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal
diff options
context:
space:
mode:
authorAlex Pott2014-01-25 10:26:34 (GMT)
committerAlex Pott2014-01-25 10:26:34 (GMT)
commit19e8cf38489477f036e475dd6d463c853342ce1b (patch)
tree5ee32d761dfbfeb0e37d17a54d47e61068eb3438 /core/lib/Drupal
parent3d0c65a8f47fc13a64a17a1c4ae7c1c3655f7263 (diff)
Issue #2110467 by Berdir: Add first(), get($index) and possibly other methods to ListInterface.
Diffstat (limited to 'core/lib/Drupal')
-rw-r--r--core/lib/Drupal/Core/Config/Schema/Sequence.php23
-rw-r--r--core/lib/Drupal/Core/Entity/Query/Sql/Tables.php4
-rw-r--r--core/lib/Drupal/Core/Field/FieldItemList.php21
-rw-r--r--core/lib/Drupal/Core/Field/FieldItemListInterface.php7
-rw-r--r--core/lib/Drupal/Core/TypedData/ListInterface.php34
-rw-r--r--core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php64
6 files changed, 102 insertions, 51 deletions
diff --git a/core/lib/Drupal/Core/Config/Schema/Sequence.php b/core/lib/Drupal/Core/Config/Schema/Sequence.php
index 6333349..720b850 100644
--- a/core/lib/Drupal/Core/Config/Schema/Sequence.php
+++ b/core/lib/Drupal/Core/Config/Schema/Sequence.php
@@ -51,17 +51,26 @@ class Sequence extends ArrayElement implements ListInterface {
}
/**
- * Gets a typed configuration element from the sequence.
- *
- * @param string $key
- * The key of the sequence to get.
- *
- * @return \Drupal\Core\Config\Schema\Element
- * Typed configuration element.
+ * {@inheritdoc}
*/
public function get($key) {
$elements = $this->getElements();
return $elements[$key];
}
+ /**
+ * {@inheritdoc}
+ */
+ public function first() {
+ return $this->get(0);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function set($index, $item) {
+ $this->offsetSet($index, $item);
+ return $this;
+ }
+
}
diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
index 8ec71e1..9f2848b 100644
--- a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
+++ b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
@@ -151,7 +151,7 @@ class Tables implements TablesInterface {
// column, i.e. target_id or fid.
// Otherwise, the code executing the relationship will throw an
// exception anyways so no need to do it here.
- if (!$column && isset($propertyDefinitions[$relationship_specifier]) && $entity->{$field->getName()}->get('entity') instanceof EntityReference) {
+ if (!$column && isset($propertyDefinitions[$relationship_specifier]) && $entity->{$field->getName()}->first()->get('entity') instanceof EntityReference) {
$column = current(array_keys($propertyDefinitions));
}
// Prepare the next index prefix.
@@ -203,7 +203,7 @@ class Tables implements TablesInterface {
$next_index_prefix = $relationship_specifier;
}
// Check for a valid relationship.
- if (isset($propertyDefinitions[$relationship_specifier]) && $entity->{$specifier}->get('entity') instanceof EntityReference) {
+ if (isset($propertyDefinitions[$relationship_specifier]) && $entity->get($specifier)->first()->get('entity') instanceof EntityReference) {
// If it is, use the entity type.
$entity_type = $propertyDefinitions[$relationship_specifier]->getConstraint('EntityType');
$entity_info = $entity_manager->getDefinition($entity_type);
diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php
index 37f6b19..e8bea18 100644
--- a/core/lib/Drupal/Core/Field/FieldItemList.php
+++ b/core/lib/Drupal/Core/Field/FieldItemList.php
@@ -144,49 +144,42 @@ class FieldItemList extends ItemList implements FieldItemListInterface {
* {@inheritdoc}
*/
public function getPropertyDefinition($name) {
- return $this->offsetGet(0)->getPropertyDefinition($name);
+ return $this->first()->getPropertyDefinition($name);
}
/**
* {@inheritdoc}
*/
public function getPropertyDefinitions() {
- return $this->offsetGet(0)->getPropertyDefinitions();
+ return $this->first()->getPropertyDefinitions();
}
/**
* {@inheritdoc}
*/
public function __get($property_name) {
- return $this->offsetGet(0)->__get($property_name);
- }
-
- /**
- * {@inheritdoc}
- */
- public function get($property_name) {
- return $this->offsetGet(0)->get($property_name);
+ return $this->first()->__get($property_name);
}
/**
* {@inheritdoc}
*/
public function __set($property_name, $value) {
- $this->offsetGet(0)->__set($property_name, $value);
+ $this->first()->__set($property_name, $value);
}
/**
* {@inheritdoc}
*/
public function __isset($property_name) {
- return $this->offsetGet(0)->__isset($property_name);
+ return $this->first()->__isset($property_name);
}
/**
* {@inheritdoc}
*/
public function __unset($property_name) {
- return $this->offsetGet(0)->__unset($property_name);
+ return $this->first()->__unset($property_name);
}
/**
@@ -215,7 +208,7 @@ class FieldItemList extends ItemList implements FieldItemListInterface {
// are valid default values.
if (!isset($value) || (is_array($value) && empty($value))) {
// Create one field item and apply defaults.
- $this->offsetGet(0)->applyDefaultValue(FALSE);
+ $this->first()->applyDefaultValue(FALSE);
}
else {
$this->setValue($value, $notify);
diff --git a/core/lib/Drupal/Core/Field/FieldItemListInterface.php b/core/lib/Drupal/Core/Field/FieldItemListInterface.php
index f86f040..7990948 100644
--- a/core/lib/Drupal/Core/Field/FieldItemListInterface.php
+++ b/core/lib/Drupal/Core/Field/FieldItemListInterface.php
@@ -77,13 +77,6 @@ interface FieldItemListInterface extends ListInterface, AccessibleInterface {
public function filterEmptyItems();
/**
- * Gets a property object from the first field item.
- *
- * @see \Drupal\Core\Field\FieldItemInterface::get()
- */
- public function get($property_name);
-
- /**
* Magic method: Gets a property value of to the first field item.
*
* @see \Drupal\Core\Field\FieldItemInterface::__get()
diff --git a/core/lib/Drupal/Core/TypedData/ListInterface.php b/core/lib/Drupal/Core/TypedData/ListInterface.php
index 53f5eac..3f8972b 100644
--- a/core/lib/Drupal/Core/TypedData/ListInterface.php
+++ b/core/lib/Drupal/Core/TypedData/ListInterface.php
@@ -43,4 +43,38 @@ interface ListInterface extends TypedDataInterface, \ArrayAccess, \Countable, \T
* The delta of the item which is changed.
*/
public function onChange($delta);
+
+ /**
+ * Returns the item at the specified position in this list.
+ *
+ * @param int $index
+ * Index of the item to return.
+ *
+ * @return \Drupal\Core\TypedData\TypedDataInterface
+ * The item at the specified position in this list. An empty item is created
+ * if it does not exist yet.
+ */
+ public function get($index);
+
+ /**
+ * Replaces the item at the specified position in this list.
+ *
+ * @param int $index
+ * Index of the item to replace.
+ * @param mixed
+ * Item to be stored at the specified position.
+ *
+ * @return static
+ * Returns the list.
+ */
+ public function set($index, $item);
+
+ /**
+ * Returns the first item in this list.
+ *
+ * @return \Drupal\Core\TypedData\TypedDataInterface
+ * The first item in this list.
+ */
+ public function first();
+
}
diff --git a/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php b/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php
index 2e906d0..d2b1a06 100644
--- a/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php
+++ b/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php
@@ -101,10 +101,49 @@ class ItemList extends TypedData implements \IteratorAggregate, ListInterface {
}
/**
+ * {@inheritdoc}
+ */
+ public function get($index) {
+ if (!is_numeric($index)) {
+ throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
+ }
+ // Allow getting not yet existing items as well.
+ // @todo: Maybe add a public createItem() method in addition?
+ elseif (!isset($this->list[$index])) {
+ $this->list[$index] = $this->createItem($index);
+ }
+ return $this->list[$index];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function set($index, $item) {
+ if (is_numeric($index)) {
+ // Support setting values via typed data objects.
+ if ($item instanceof TypedDataInterface) {
+ $item = $item->getValue();
+ }
+ $this->get($index)->setValue($item);
+ return $this;
+ }
+ else {
+ throw new \InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function first() {
+ return $this->get(0);
+ }
+
+ /**
* Implements \ArrayAccess::offsetExists().
*/
public function offsetExists($offset) {
- return isset($this->list) && array_key_exists($offset, $this->list) && $this->offsetGet($offset)->getValue() !== NULL;
+ return isset($this->list) && array_key_exists($offset, $this->list) && $this->get($offset)->getValue() !== NULL;
}
/**
@@ -117,18 +156,10 @@ class ItemList extends TypedData implements \IteratorAggregate, ListInterface {
}
/**
- * Implements \ArrayAccess::offsetGet().
+ * {@inheritdoc}
*/
public function offsetGet($offset) {
- if (!is_numeric($offset)) {
- throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
- }
- // Allow getting not yet existing items as well.
- // @todo: Maybe add a public createItem() method in addition?
- elseif (!isset($this->list[$offset])) {
- $this->list[$offset] = $this->createItem($offset);
- }
- return $this->list[$offset];
+ return $this->get($offset);
}
/**
@@ -155,16 +186,7 @@ class ItemList extends TypedData implements \IteratorAggregate, ListInterface {
// The [] operator has been used so point at a new entry.
$offset = $this->list ? max(array_keys($this->list)) + 1 : 0;
}
- if (is_numeric($offset)) {
- // Support setting values via typed data objects.
- if ($value instanceof TypedDataInterface) {
- $value = $value->getValue();
- }
- $this->offsetGet($offset)->setValue($value);
- }
- else {
- throw new \InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
- }
+ $this->set($offset, $value);
}
/**