diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php index b1949de..a95d886 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php @@ -7,8 +7,6 @@ namespace Drupal\field\Plugin\Type\FieldType; -use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem; -use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface; use Drupal\field\FieldInterface; /** @@ -20,7 +18,7 @@ * Required settings (below the definition's 'settings' key) are: * - target_type: The entity type to reference. */ -class ConfigEntityReferenceItemBase extends EntityReferenceItem implements ConfigFieldItemInterface { +class ConfigEntityReferenceItemBase extends ConfigFieldItemBase { /** * Definitions of the contained properties. @@ -36,7 +34,7 @@ class ConfigEntityReferenceItemBase extends EntityReferenceItem implements Confi * * @var \Drupal\field\Plugin\Core\Entity\FieldInstance */ - protected $instance; + public $instance; /** * Returns the field instance definition. @@ -62,8 +60,25 @@ public function getPropertyDefinitions() { $key .= isset($this->definition['settings']['target_bundle']) ? $this->definition['settings']['target_bundle'] : ''; if (!isset(static::$propertyDefinitions[$key])) { - // Call the parent to define the target_id and entity properties. - parent::getPropertyDefinitions(); + static::$propertyDefinitions[$key]['target_id'] = array( + // @todo: Lookup the entity type's ID data type and use it here. + 'type' => 'integer', + 'label' => t('Entity ID'), + 'constraints' => array( + 'Range' => array('min' => 0), + ), + ); + static::$propertyDefinitions[$key]['entity'] = array( + 'type' => 'entity_reference', + 'constraints' => array( + 'EntityType' => $this->definition['settings']['target_type'], + ), + 'label' => t('Entity'), + 'description' => t('The referenced entity'), + // The entity object is computed out of the entity ID. + 'computed' => TRUE, + 'read-only' => FALSE, + ); static::$propertyDefinitions[$key]['revision_id'] = array( // @todo: Lookup the entity type's ID data type and use it here. @@ -83,6 +98,9 @@ public function getPropertyDefinitions() { 'label' => t('Access'), 'computed' => TRUE, ); + if (isset($this->definition['settings']['target_bundle'])) { + static::$propertyDefinitions[$key]['entity']['constraints']['Bundle'] = $this->definition['settings']['target_bundle']; + } } return static::$propertyDefinitions[$key]; } @@ -148,27 +166,6 @@ public function instanceSettingsForm(array $form, array &$form_state) { } /** - * {@inheritdoc} - */ - public function defaultValuesForm(array &$form, array &$form_state) { - return array(); - } - - /** - * {@inheritdoc} - */ - public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) { - return array(); - } - - /** - * {@inheritdoc} - */ - public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) { - return array(); - } - - /** * Returns the legacy callback for a given field type "hook". * * Copied from \Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem, @@ -189,4 +186,64 @@ protected function getLegacyCallback($hook) { } } + /** + * {@inheritdoc} + */ + public function __get($name) { + $name = ($name == 'value') ? 'target_id' : $name; + return parent::__get($name); + } + + /** + * {@inheritdoc} + */ + public function get($property_name) { + $property_name = ($property_name == 'value') ? 'target_id' : $property_name; + return parent::get($property_name); + } + + /** + * {@inheritdoc} + */ + public function __isset($property_name) { + $property_name = ($property_name == 'value') ? 'target_id' : $property_name; + return parent::__isset($property_name); + } + + /** + * {@inheritdoc} + */ + public function setValue($values, $notify = TRUE) { + if (isset($values) && !is_array($values)) { + // Directly update the property instead of invoking the parent, so it can + // handle objects and IDs. + $this->properties['entity']->setValue($values, $notify); + // If notify was FALSE, ensure the target_id property gets synched. + if (!$notify) { + $this->set('target_id', $this->properties['entity']->getTargetIdentifier(), FALSE); + } + } + else { + // Make sure that the 'entity' property gets set as 'target_id'. + if (isset($values['target_id']) && !isset($values['entity'])) { + $values['entity'] = $values['target_id']; + } + parent::setValue($values, $notify); + } + } + + /** + * {@inheritdoc} + */ + public function onChange($property_name) { + // Make sure that the target ID and the target property stay in sync. + if ($property_name == 'target_id') { + $this->properties['entity']->setValue($this->target_id, FALSE); + } + elseif ($property_name == 'entity') { + $this->set('target_id', $this->properties['entity']->getTargetIdentifier(), FALSE); + } + parent::onChange($property_name); + } + } 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 66df59d..01ba99a 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 @@ -132,8 +132,9 @@ protected function defaultValueWidget(array &$form_state) { $entity = $this->getParent()->getParent(); $entity_form_display = entity_get_form_display($entity->entityType(), $entity->bundle(), 'default'); + // Use the widget currently configured for the 'default' form mode, + // or fallback to the default widget for the field type. $widget = $entity_form_display->getRenderer($field_name); - // When field is hidden, use the default widget and store it in the form. if (!$widget) { $widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition())); } diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/widget/TestFieldWidgetNoDefault.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/widget/TestFieldWidgetNoDefault.php deleted file mode 100644 index d17d6de..0000000 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/widget/TestFieldWidgetNoDefault.php +++ /dev/null @@ -1,29 +0,0 @@ -type); $this->assertEqual($instance['default_value'], NULL, 'The default value was correctly saved.'); + + // Check that the default widget is displayed when the field is hidden. + entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default') + ->removeComponent($field_name)->save(); + $this->drupalGet($admin_path); + $this->assertFieldById($element_id, '', 'The default value widget was displayed when field is hidden.'); } /**