Commit f5ba64c9 authored by alexpott's avatar alexpott

Issue #2156337 by yched, amateescu, smiletrl: Merge...

Issue #2156337 by yched, amateescu, smiletrl: Merge ConfigEntityReferenceItemBase up into EntityReferenceItem, and fix inconsistencies.
parent 44b98ffa
<?php
/**
* @file
* Contains \Drupal\Core\Field\ConfigEntityReferenceItemBase.
*/
namespace Drupal\Core\Field;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
/**
* A common base class for configurable entity reference fields.
*
* Extends the Core 'entity_reference' entity field item with common methods
* used in general configurable entity reference field.
*/
class ConfigEntityReferenceItemBase extends EntityReferenceItem {
/**
* {@inheritdoc}
*/
public function isEmpty() {
// Avoid loading the entity by first checking the 'target_id'.
$target_id = $this->target_id;
if (!empty($target_id)) {
return FALSE;
}
// Allow auto-create entities.
if (empty($target_id) && ($entity = $this->get('entity')->getValue()) && $entity->isNew()) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getValue() {
$values = parent::getValue();
// If there is an unsaved entity, return it as part of the field item values
// to ensure idempotency of getValue() / setValue().
if (empty($this->target_id) && !empty($this->entity)) {
$values['entity'] = $this->entity;
}
return $values;
}
/**
* {@inheritdoc}
*/
public function preSave() {
$entity = $this->get('entity')->getValue();
$target_id = $this->get('target_id')->getValue();
if (!$target_id && !empty($entity) && $entity->isNew()) {
$entity->save();
$this->set('target_id', $entity->id());
}
}
}
...@@ -166,6 +166,20 @@ public function setValue($values, $notify = TRUE) { ...@@ -166,6 +166,20 @@ public function setValue($values, $notify = TRUE) {
} }
} }
/**
* {@inheritdoc}
*/
public function getValue($include_computed = FALSE) {
$values = parent::getValue($include_computed);
// If there is an unsaved entity, return it as part of the field item values
// to ensure idempotency of getValue() / setValue().
if ($this->hasUnsavedEntity()) {
$values['entity'] = $this->entity;
}
return $values;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -187,4 +201,44 @@ public function getMainPropertyName() { ...@@ -187,4 +201,44 @@ public function getMainPropertyName() {
return 'target_id'; return 'target_id';
} }
/**
* {@inheritdoc}
*/
public function isEmpty() {
// Avoid loading the entity by first checking the 'target_id'.
$target_id = $this->target_id;
if ($target_id !== NULL) {
return FALSE;
}
// Allow auto-create entities.
if ($this->hasUnsavedEntity()) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function preSave() {
if ($this->hasUnsavedEntity()) {
$this->entity->save();
$this->target_id = $this->entity->id();
}
}
/**
* Determines whether the item holds an unsaved entity.
*
* This is notably used for "autocreate" widgets, and more generally to
* support referencing freshly created entities (they will get saved
* automatically as the hosting entity gets saved).
*
* @return bool
* TRUE if the item holds an unsaved entity.
*/
public function hasUnsavedEntity() {
return $this->target_id === NULL && ($entity = $this->entity) && $entity->isNew();
}
} }
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
namespace Drupal\entity_reference; namespace Drupal\entity_reference;
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Core\Field\ConfigFieldItemInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\AllowedValuesInterface; use Drupal\Core\TypedData\AllowedValuesInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\ConfigEntityReferenceItemBase;
use Drupal\Core\Field\ConfigFieldItemInterface;
use Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint; use Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint;
/** /**
...@@ -26,9 +26,8 @@ ...@@ -26,9 +26,8 @@
* - target_type: The entity type to reference. * - target_type: The entity type to reference.
* *
* @see entity_reference_field_info_alter(). * @see entity_reference_field_info_alter().
*
*/ */
class ConfigurableEntityReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface, AllowedValuesInterface { class ConfigurableEntityReferenceItem extends EntityReferenceItem implements ConfigFieldItemInterface, AllowedValuesInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -48,7 +47,7 @@ public function getPossibleOptions(AccountInterface $account = NULL) { ...@@ -48,7 +47,7 @@ public function getPossibleOptions(AccountInterface $account = NULL) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSettableValues(AccountInterface $account = NULL) { public function getSettableValues(AccountInterface $account = NULL) {
// Flatten options firstly, because Settable Options may contain group // Flatten options first, because "settable options" may contain group
// arrays. // arrays.
$flatten_options = \Drupal::formBuilder()->flattenOptions($this->getSettableOptions($account)); $flatten_options = \Drupal::formBuilder()->flattenOptions($this->getSettableOptions($account));
return array_keys($flatten_options); return array_keys($flatten_options);
......
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
namespace Drupal\entity_reference\Plugin\Field\FieldWidget; namespace Drupal\entity_reference\Plugin\Field\FieldWidget;
use Drupal\entity_reference\Plugin\Field\FieldWidget\AutocompleteWidgetBase;
/** /**
* Plugin implementation of the 'entity_reference autocomplete-tags' widget. * Plugin implementation of the 'entity_reference autocomplete-tags' widget.
* *
...@@ -64,9 +62,10 @@ public function elementValidate($element, &$form_state, $form) { ...@@ -64,9 +62,10 @@ public function elementValidate($element, &$form_state, $form) {
$value[] = array('target_id' => $match); $value[] = array('target_id' => $match);
} }
elseif ($auto_create && (count($this->getSelectionHandlerSetting('target_bundles')) == 1 || count($bundles) == 1)) { elseif ($auto_create && (count($this->getSelectionHandlerSetting('target_bundles')) == 1 || count($bundles) == 1)) {
// Auto-create item. see entity_reference_field_presave(). // Auto-create item. See
// \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::presave().
$value[] = array( $value[] = array(
'target_id' => 0, 'target_id' => NULL,
'entity' => $this->createNewEntity($input, $element['#autocreate_uid']), 'entity' => $this->createNewEntity($input, $element['#autocreate_uid']),
); );
} }
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Drupal\entity_reference\Plugin\Field\FieldWidget; namespace Drupal\entity_reference\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\entity_reference\Plugin\Field\FieldWidget\AutocompleteWidgetBase;
/** /**
* Plugin implementation of the 'entity_reference autocomplete' widget. * Plugin implementation of the 'entity_reference autocomplete' widget.
...@@ -54,7 +53,7 @@ public function elementValidate($element, &$form_state, $form) { ...@@ -54,7 +53,7 @@ public function elementValidate($element, &$form_state, $form) {
$auto_create = $this->getSelectionHandlerSetting('auto_create'); $auto_create = $this->getSelectionHandlerSetting('auto_create');
// If a value was entered into the autocomplete. // If a value was entered into the autocomplete.
$value = ''; $value = NULL;
if (!empty($element['#value'])) { if (!empty($element['#value'])) {
// Take "label (entity id)', match the id from parenthesis. // Take "label (entity id)', match the id from parenthesis.
// @todo: Lookup the entity type's ID data type and use it here. // @todo: Lookup the entity type's ID data type and use it here.
...@@ -73,9 +72,10 @@ public function elementValidate($element, &$form_state, $form) { ...@@ -73,9 +72,10 @@ public function elementValidate($element, &$form_state, $form) {
} }
if (!$value && $auto_create && (count($this->getSelectionHandlerSetting('target_bundles')) == 1)) { if (!$value && $auto_create && (count($this->getSelectionHandlerSetting('target_bundles')) == 1)) {
// Auto-create item. see entity_reference_field_presave(). // Auto-create item. See
// \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::presave().
$value = array( $value = array(
'target_id' => 0, 'target_id' => NULL,
'entity' => $this->createNewEntity($element['#value'], $element['#autocreate_uid']), 'entity' => $this->createNewEntity($element['#value'], $element['#autocreate_uid']),
// Keep the weight property. // Keep the weight property.
'_weight' => $element['#weight'], '_weight' => $element['#weight'],
......
...@@ -196,13 +196,6 @@ public function instanceSettingsForm(array $form, array &$form_state) { ...@@ -196,13 +196,6 @@ public function instanceSettingsForm(array $form, array &$form_state) {
return $element; return $element;
} }
/**
* {@inheritdoc}
*/
public function isEmpty() {
return empty($this->target_id);
}
/** /**
* Form API callback * Form API callback
* *
......
...@@ -421,7 +421,7 @@ function image_filter_keyword($value, $current_pixels, $new_pixels) { ...@@ -421,7 +421,7 @@ function image_filter_keyword($value, $current_pixels, $new_pixels) {
* *
* Transforms default image of image field from array into single value at save. * Transforms default image of image field from array into single value at save.
*/ */
function image_entity_presave(EntityInterface $entity, $type) { function image_entity_presave(EntityInterface $entity) {
$field = FALSE; $field = FALSE;
if ($entity instanceof FieldInstance) { if ($entity instanceof FieldInstance) {
$field = $entity->getField(); $field = $entity->getField();
......
...@@ -284,6 +284,8 @@ public function instanceSettingsForm(array $form, array &$form_state) { ...@@ -284,6 +284,8 @@ public function instanceSettingsForm(array $form, array &$form_state) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function preSave() { public function preSave() {
parent::preSave();
$width = $this->width; $width = $this->width;
$height = $this->height; $height = $this->height;
......
...@@ -48,7 +48,7 @@ public function prepareView(array $entities_items) { ...@@ -48,7 +48,7 @@ public function prepareView(array $entities_items) {
$item->entity = $terms[$item->target_id]; $item->entity = $terms[$item->target_id];
} }
// Terms to be created are not in $terms, but are still legitimate. // Terms to be created are not in $terms, but are still legitimate.
elseif ($item->target_id === NULL && isset($item->entity)) { elseif ($item->hasUnsavedEntity()) {
// Leave the item in place. // Leave the item in place.
} }
// Otherwise, unset the instance value, since the term does not exist. // Otherwise, unset the instance value, since the term does not exist.
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
namespace Drupal\taxonomy\Plugin\Field\FieldType; namespace Drupal\taxonomy\Plugin\Field\FieldType;
use Drupal\Core\Field\ConfigEntityReferenceItemBase;
use Drupal\Core\Field\ConfigFieldItemInterface; use Drupal\Core\Field\ConfigFieldItemInterface;
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\AllowedValuesInterface; use Drupal\Core\TypedData\AllowedValuesInterface;
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
* list_class = "\Drupal\taxonomy\Plugin\Field\FieldType\TaxonomyTermReferenceFieldItemList" * list_class = "\Drupal\taxonomy\Plugin\Field\FieldType\TaxonomyTermReferenceFieldItemList"
* ) * )
*/ */
class TaxonomyTermReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface, AllowedValuesInterface { class TaxonomyTermReferenceItem extends EntityReferenceItem implements ConfigFieldItemInterface, AllowedValuesInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment