diff --git a/core/lib/Drupal/Core/Entity/EntityBCDecorator.php b/core/lib/Drupal/Core/Entity/EntityBCDecorator.php index 93bafb4..f0eef75 100644 --- a/core/lib/Drupal/Core/Entity/EntityBCDecorator.php +++ b/core/lib/Drupal/Core/Entity/EntityBCDecorator.php @@ -107,7 +107,7 @@ public function &__get($name) { foreach ($this->decorated->fields[$name] as $langcode => $field) { // Only set if it's not empty, otherwise there can be ghost values. if (!$field->isEmpty()) { - $this->decorated->values[$name][$langcode] = $field->getValue(); + $this->decorated->values[$name][$langcode] = $field->getValue(TRUE); } } // The returned values might be changed by reference, so we need to remove @@ -126,9 +126,9 @@ public function &__get($name) { if (is_array($this->decorated->values[$name][Language::LANGCODE_DEFAULT])) { // This will work with all defined properties that have a single value. // We need to ensure the key doesn't matter. Mostly it's 'value' but - // e.g. EntityReferenceItem uses target_id. - if (isset($this->decorated->values[$name][Language::LANGCODE_DEFAULT][0]) && count($this->decorated->values[$name][Language::LANGCODE_DEFAULT][0]) == 1) { - return $this->decorated->values[$name][Language::LANGCODE_DEFAULT][0][key($this->decorated->values[$name][Language::LANGCODE_DEFAULT][0])]; + // e.g. EntityReferenceItem uses target_id - so just take the first one. + if (isset($this->decorated->values[$name][Language::LANGCODE_DEFAULT][0]) && is_array($this->decorated->values[$name][Language::LANGCODE_DEFAULT][0])) { + return $this->decorated->values[$name][Language::LANGCODE_DEFAULT][0][current(array_keys($this->decorated->values[$name][Language::LANGCODE_DEFAULT][0]))]; } } return $this->decorated->values[$name][Language::LANGCODE_DEFAULT]; diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php index 7820f13..4b617b1 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php @@ -199,14 +199,7 @@ public function invokeFieldMethodMultiple($method, array $entities, $langcode) { // @todo Remove the condition and the second code branch when all core // entity types are converted. if ($entity->getNGEntity() instanceof EntityNG) { - // @todo $entity->getTranslation()->get($name) sometimes fails, - // because Entity\Translation::getPropertyDefinitions() is empty() ?? - try { - $entities_items[$id] = $entity->getTranslation($langcode)->get($field_name); - } - catch (\InvalidArgumentException $e) { - break; - } + $entities_items[$id] = $entity->getNGEntity()->getTranslation($langcode)->get($field_name); } else { $BC_mode = TRUE; diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Type/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Type/ConfigurableEntityReferenceItem.php index 9e15864..16ba508 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Type/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Type/ConfigurableEntityReferenceItem.php @@ -41,22 +41,18 @@ class ConfigurableEntityReferenceItem extends EntityReferenceItem implements Con protected $instance; /** - * Constructs a \Drupal\Component\Plugin\ConfigurableEntityReferenceItem - * object. + * Returns the field instance definition. * * Duplicated from \Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase, * since we cannot extend it. + * + * @var \Drupal\field\Plugin\Core\Entity\FieldInstance */ - public function __construct(array $definition, $name = NULL, TypedDataInterface $parent = NULL) { - parent::__construct($definition, $name, $parent); - if (isset($definition['instance'])) { - $this->instance = $definition['instance']; - } - else { - $entity = $parent->getParent(); - $instances = FieldAPI::fieldInfo()->getBundleInstances($entity->entityType(), $entity->bundle()); - $this->instance = $instances[$parent->name]; + public function getInstance() { + if (!isset($this->instance) && $parent = $this->getParent()) { + $this->instance = $parent->getInstance(); } + return $this->instance; } /** @@ -133,7 +129,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { if ($callback = $this->getLegacyCallback('settings_form')) { // hook_field_settings_form() used to receive the $instance (not actually // needed), and the value of field_has_data(). - return $callback($this->instance->getField(), $this->instance, $has_data); + return $callback($this->getInstance()->getField(), $this->getInstance(), $has_data); } } @@ -145,7 +141,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { */ public function instanceSettingsForm(array $form, array &$form_state) { if ($callback = $this->getLegacyCallback('instance_settings_form')) { - return $callback($this->instance->getField(), $this->instance, $form_state); + return $callback($this->getInstance()->getField(), $this->getInstance(), $form_state); } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php index 1adf8bc..37034a9 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php @@ -33,10 +33,19 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface if (isset($definition['instance'])) { $this->instance = $definition['instance']; } - else { + } + + /** + * Returns the field instance definition. + * + * @var \Drupal\field\Plugin\Core\Entity\FieldInstance + */ + public function getInstance() { + if (!isset($this->instance) && $parent = $this->getParent()) { $instances = FieldAPI::fieldInfo()->getBundleInstances($parent->entityType(), $parent->bundle()); - $this->instance = $instances[$name]; + $this->instance = $instances[$this->getName()]; } + return $this->instance; } /** @@ -47,13 +56,13 @@ public function getConstraints() { // Check that the number of values doesn't exceed the field cardinality. For // form submitted values, this can only happen with 'multiple value' // widgets. - $cardinality = $this->instance->getField()->cardinality; + $cardinality = $this->getInstance()->getField()->cardinality; if ($cardinality != FIELD_CARDINALITY_UNLIMITED) { $constraints[] = \Drupal::typedData() ->getValidationConstraintManager() ->create('Count', array( 'max' => $cardinality, - 'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->instance->label, '@count' => $cardinality)), + 'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getInstance()->label, '@count' => $cardinality)), )); } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php index 0176892..5fb4811 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php @@ -21,21 +21,18 @@ * * @var \Drupal\field\Plugin\Core\Entity\FieldInstance */ - protected $instance; + public $instance; /** - * {@inheritdoc} + * Returns the field instance definition. + * + * @var \Drupal\field\Plugin\Core\Entity\FieldInstance */ - public function __construct(array $definition, $name = NULL, TypedDataInterface $parent = NULL) { - parent::__construct($definition, $name, $parent); - if (isset($definition['instance'])) { - $this->instance = $definition['instance']; - } - else { - $entity = $parent->getParent(); - $instances = Field::fieldInfo()->getBundleInstances($entity->entityType(), $entity->bundle()); - $this->instance = $instances[$parent->name]; + public function getInstance() { + if (!isset($this->instance) && $parent = $this->getParent()) { + $this->instance = $parent->getInstance(); } + return $this->instance; } /** diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php index 3e1eb68..a1780e7 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php @@ -42,12 +42,12 @@ public function validate() { $entity = $this->getParent(); $langcode = $entity->language()->langcode; - if (isset($legacy_errors[$this->instance->getField()->id()][$langcode])) { - foreach ($legacy_errors[$this->instance->getField()->id()][$langcode] as $delta => $item_errors) { + if (isset($legacy_errors[$this->getInstance()->getField()->id()][$langcode])) { + foreach ($legacy_errors[$this->getInstance()->getField()->id()][$langcode] as $delta => $item_errors) { foreach ($item_errors as $item_error) { // We do not have the information about which column triggered the // error, so assume the first column... - $column = key($this->instance->getField()->getColumns()); + $column = key($this->getInstance()->getField()->getColumns()); $violations->add(new ConstraintViolation($item_error['message'], $item_error['message'], array(), $this, $delta . '.' . $column, $this->offsetGet($delta)->get($column)->getValue(), NULL, $item_error['error'])); } } @@ -115,8 +115,8 @@ protected function legacyCallback($hook, $args = array()) { $items = (array) $this->getValue(TRUE); $args = array_merge(array( $entity, - $this->instance->getField(), - $this->instance, + $this->getInstance()->getField(), + $this->getInstance(), $langcode, &$items ), $args); diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php index 2b92362..232b2aa 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php +++ b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php @@ -48,7 +48,7 @@ public function isEmpty() { $item = $this->getValue(TRUE); // The previous hook was never called on an empty item, but EntityNG always // creates a FieldItem element for an empty field. - return empty($item) || $callback($item, $this->instance->getField()->type); + return empty($item) || $callback($item, $this->getInstance()->getField()->type); } /** @@ -58,7 +58,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { if ($callback = $this->getLegacyCallback('settings_form')) { // hook_field_settings_form() used to receive the $instance (not actually // needed), and the value of field_has_data(). - return $callback($this->instance->getField(), $this->instance, $has_data); + return $callback($this->getInstance()->getField(), $this->getInstance(), $has_data); } return array(); } @@ -68,7 +68,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { */ public function instanceSettingsForm(array $form, array &$form_state) { if ($callback = $this->getLegacyCallback('instance_settings_form')) { - return $callback($this->instance->getField(), $this->instance, $form_state); + return $callback($this->getInstance()->getField(), $this->getInstance(), $form_state); } return array(); } @@ -95,8 +95,8 @@ public function prepareCache() { $args = array( $entity->entityType(), array($entity_id => $entity), - $this->instance->getField(), - array($entity_id => $this->instance), + $this->getInstance()->getField(), + array($entity_id => $this->getInstance()), $langcode, &$items, FIELD_LOAD_CURRENT, @@ -119,7 +119,7 @@ public static function prepareView(array $entities_items, array $definition) { $itemsBC = array(); foreach ($entities_items as $id => $items) { $entities[$id] = $items->getParent(); - $instances[$id] = $items->offsetGet(0)->instance; + $instances[$id] = $items->offsetGet(0)->getInstance(); // We need to remove the empty "prototype" item here. // @todo Revisit after http://drupal.org/node/1988492. $items->filterEmptyValues(); diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItem.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItem.php index e735ea5..4bcf53e 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItem.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItem.php @@ -69,7 +69,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { $element['max_length'] = array( '#type' => 'number', '#title' => t('Maximum length'), - '#default_value' => $this->instance->getField()->settings['max_length'], + '#default_value' => $this->getInstance()->getField()->settings['max_length'], '#required' => TRUE, '#description' => t('The maximum length of the field in characters.'), '#min' => 1, @@ -90,7 +90,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { $element['text_processing'] = array( '#type' => 'radios', '#title' => t('Text processing'), - '#default_value' => $this->instance->settings['text_processing'], + '#default_value' => $this->getInstance()->settings['text_processing'], '#options' => array( t('Plain text'), t('Filtered text (user selects text format)'), diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php index a74fb3e..0b2b6a0 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextItemBase.php @@ -72,18 +72,18 @@ public function getConstraints() { // 'value' => array( // 'Length' => array( // 'max' => 3, -// 'maxMessage' => t('%name: testing - max is @max.', array('%name' => $this->instance->label, '@max' => 3)), +// 'maxMessage' => t('%name: testing - max is @max.', array('%name' => $this->getInstance()->label, '@max' => 3)), // ), // ), // )); - if (!empty($this->instance->getField()->settings['max_length'])) { - $max_length = $this->instance->getField()->settings['max_length']; + if (!empty($this->getInstance()->getField()->settings['max_length'])) { + $max_length = $this->getInstance()->getField()->settings['max_length']; $constraints[] = $constraint_manager->create('ComplexData', array( 'value' => array( 'Length' => array( 'max' => $max_length, - 'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->instance->label, '@max' => $max_length)), + 'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->getInstance()->label, '@max' => $max_length)), ) ), )); @@ -99,12 +99,12 @@ public function prepareCache() { // Where possible, generate the sanitized version of each field early so // that it is cached in the field cache. This avoids the need to look up the // field in the filter cache separately. - if (!$this->instance->settings['text_processing'] || filter_format_allowcache($this->get('format')->getValue())) { + if (!$this->getInstance()->settings['text_processing'] || filter_format_allowcache($this->get('format')->getValue())) { $itemBC = $this->getValue(); $langcode = $this->getParent()->getParent()->language()->langcode; - $this->set('safe_value', text_sanitize($this->instance->settings['text_processing'], $langcode, $itemBC, 'value')); + $this->set('safe_value', text_sanitize($this->getInstance()->settings['text_processing'], $langcode, $itemBC, 'value')); if ($this->getType() == 'configurable_field_type:text_with_summary') { - $this->set('safe_summary', text_sanitize($this->instance->settings['text_processing'], $langcode, $itemBC, 'summary')); + $this->set('safe_summary', text_sanitize($this->getInstance()->settings['text_processing'], $langcode, $itemBC, 'summary')); } } } diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextLongItem.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextLongItem.php index f4fed06..578aaa8 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextLongItem.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextLongItem.php @@ -66,7 +66,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { $element['text_processing'] = array( '#type' => 'radios', '#title' => t('Text processing'), - '#default_value' => $this->instance->settings['text_processing'], + '#default_value' => $this->getInstance()->settings['text_processing'], '#options' => array( t('Plain text'), t('Filtered text (user selects text format)'), diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextWithSummaryItem.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextWithSummaryItem.php index 2706d4a..0c42014 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextWithSummaryItem.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/ConfigTextWithSummaryItem.php @@ -110,13 +110,13 @@ public function getConstraints() { $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); $constraints = parent::getConstraints(); - if (!empty($this->instance->getField()->settings['max_length'])) { - $max_length = $this->instance->getField()->settings['max_length']; + if (!empty($this->getInstance()->getField()->settings['max_length'])) { + $max_length = $this->getInstance()->getField()->settings['max_length']; $constraints[] = $constraint_manager->create('ComplexData', array( 'value' => array( 'Length' => array( 'max' => $max_length, - 'maxMessage' => t('%name: the summary may not be longer than @max characters.', array('%name' => $this->instance->label, '@max' => $max_length)), + 'maxMessage' => t('%name: the summary may not be longer than @max characters.', array('%name' => $this->getInstance()->label, '@max' => $max_length)), ) ), )); @@ -134,7 +134,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { $element['text_processing'] = array( '#type' => 'radios', '#title' => t('Text processing'), - '#default_value' => $this->instance->settings['text_processing'], + '#default_value' => $this->getInstance()->settings['text_processing'], '#options' => array( t('Plain text'), t('Filtered text (user selects text format)'), @@ -143,7 +143,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { $element['display_summary'] = array( '#type' => 'checkbox', '#title' => t('Summary input'), - '#default_value' => $this->instance->settings['display_summary'], + '#default_value' => $this->getInstance()->settings['display_summary'], '#description' => t('This allows authors to input an explicit summary, to be displayed instead of the automatically trimmed text when using the "Summary or trimmed" display type.'), );