Commit f2015b70 authored by alexpott's avatar alexpott

Issue #2162005 by plopesc: Clean up entity_reference default values once users...

Issue #2162005 by plopesc: Clean up entity_reference default values once users 0 and 1 provides UUID.
parent bf7b1b01
......@@ -7,6 +7,9 @@
namespace Drupal\Core\Field;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines a item list class for entity reference fields.
*/
......@@ -46,4 +49,69 @@ public function referencedEntities() {
return $target_entities;
}
/**
* {@inheritdoc}
*/
public static function processDefaultValue($default_value, ContentEntityInterface $entity, FieldDefinitionInterface $definition) {
$default_value = parent::processDefaultValue($default_value, $entity, $definition);
if ($default_value) {
// Convert UUIDs to numeric IDs.
$uuids = array();
foreach ($default_value as $delta => $properties) {
if (isset($properties['target_uuid'])) {
$uuids[$delta] = $properties['target_uuid'];
}
}
if ($uuids) {
$target_type = $definition->getSetting('target_type');
$entity_ids = \Drupal::entityQuery($target_type)
->condition('uuid', $uuids, 'IN')
->execute();
$entities = \Drupal::entityManager()
->getStorage($target_type)
->loadMultiple($entity_ids);
foreach ($entities as $id => $entity) {
$entity_uuids[$entity->uuid()] = $id;
}
foreach ($uuids as $delta => $uuid) {
if ($entity_uuids[$uuid]) {
$default_value[$delta]['target_id'] = $entity_uuids[$uuid];
unset($default_value[$delta]['target_uuid']);
}
else {
unset($default_value[$delta]);
}
}
}
// Ensure we return consecutive deltas, in case we removed unknown UUIDs.
$default_value = array_values($default_value);
}
return $default_value;
}
/**
* {@inheritdoc}
*/
public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
$default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);
// Convert numeric IDs to UUIDs to ensure config deployability.
$ids = array();
foreach ($default_value as $delta => $properties) {
$ids[] = $properties['target_id'];
}
$entities = \Drupal::entityManager()
->getStorage($this->getSetting('target_type'))
->loadMultiple($ids);
foreach ($default_value as $delta => $properties) {
unset($default_value[$delta]['target_id']);
$default_value[$delta]['target_uuid'] = $entities[$properties['target_id']]->uuid();
}
return $default_value;
}
}
......@@ -45,7 +45,7 @@ function entity_reference_field_info_alter(&$info) {
// Make the entity reference field configurable.
$info['entity_reference']['no_ui'] = FALSE;
$info['entity_reference']['class'] = '\Drupal\entity_reference\ConfigurableEntityReferenceItem';
$info['entity_reference']['list_class'] = '\Drupal\entity_reference\Plugin\Field\FieldType\ConfigurableEntityReferenceFieldItemList';
$info['entity_reference']['list_class'] = '\Drupal\Core\Field\EntityReferenceFieldItemList';
$info['entity_reference']['default_widget'] = 'entity_reference_autocomplete';
$info['entity_reference']['default_formatter'] = 'entity_reference_label';
$info['entity_reference']['provider'] = 'entity_reference';
......
<?php
/**
* @file
* Contains \Drupal\entity_reference\Plugin\Field\FieldType\ConfigurableEntityReferenceFieldItemList.
*/
namespace Drupal\entity_reference\Plugin\Field\FieldType;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Represents a configurable entity_reference entity field.
*/
class ConfigurableEntityReferenceFieldItemList extends EntityReferenceFieldItemList {
/**
* {@inheritdoc}
*/
public static function processDefaultValue($default_value, ContentEntityInterface $entity, FieldDefinitionInterface $definition) {
$default_value = parent::processDefaultValue($default_value, $entity, $definition);
if ($default_value) {
// Convert UUIDs to numeric IDs.
$uuids = array();
$fixed = array();
foreach ($default_value as $delta => $properties) {
if ($properties['target_uuid'] == 'anonymous' || $properties['target_uuid'] == 'administrator') {
$fixed[$delta] = ($properties['target_uuid'] == 'anonymous') ? '0' : '1';
}
else {
$uuids[$delta] = $properties['target_uuid'];
}
}
if ($uuids) {
$target_type = $definition->getSetting('target_type');
$entity_ids = \Drupal::entityQuery($target_type)
->condition('uuid', $uuids, 'IN')
->execute();
$entities = \Drupal::entityManager()
->getStorage($target_type)
->loadMultiple($entity_ids);
foreach ($entities as $id => $entity) {
$entity_ids[$entity->uuid()] = $id;
}
foreach ($uuids as $delta => $uuid) {
if ($entity_ids[$uuid]) {
$default_value[$delta]['target_id'] = $entity_ids[$uuid];
unset($default_value[$delta]['target_uuid']);
}
else {
unset($default_value[$delta]);
}
}
}
if ($fixed) {
foreach ($fixed as $delta => $id) {
$default_value[$delta]['target_id'] = $id;
unset($default_value[$delta]['target_uuid']);
}
}
// Ensure we return consecutive deltas, in case we removed unknown UUIDs.
$default_value = array_values($default_value);
}
return $default_value;
}
/**
* {@inheritdoc}
*/
public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
$default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);
// Convert numeric IDs to UUIDs to ensure config deployability.
$ids = array();
foreach ($default_value as $delta => $properties) {
$ids[] = $properties['target_id'];
}
$entities = \Drupal::entityManager()
->getStorage($this->getSetting('target_type'))
->loadMultiple($ids);
foreach ($default_value as $delta => $properties) {
$uuid = $entities[$properties['target_id']]->uuid();
// @todo Some entities do not have uuid. IE: Anonymous and admin user.
// Remove this special case once http://drupal.org/node/2050843
// has been fixed.
if (!$uuid) {
$uuid = ($properties['target_id'] == '0') ? 'anonymous' : 'administrator';
}
unset($default_value[$delta]['target_id']);
$default_value[$delta]['target_uuid'] = $uuid;
}
return $default_value;
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\Field\FieldType\TaxonomyTermReferenceFieldItemList.
*/
namespace Drupal\taxonomy\Plugin\Field\FieldType;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Represents a configurable taxonomy_term_reference entity field item list.
*/
class TaxonomyTermReferenceFieldItemList extends EntityReferenceFieldItemList {
/**
* {@inheritdoc}
*/
public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
$default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);
// Convert numeric IDs to UUIDs to ensure config deployability.
$ids = array();
foreach ($default_value as $delta => $properties) {
$ids[] = $properties['target_id'];
}
$entities = \Drupal::entityManager()
->getStorage('taxonomy_term')
->loadMultiple($ids);
foreach ($default_value as $delta => $properties) {
unset($default_value[$delta]['target_id']);
$default_value[$delta]['target_uuid'] = $entities[$properties['target_id']]->uuid();
}
return $default_value;
}
/**
* {@inheritdoc}
*/
public static function processDefaultValue($default_value, ContentEntityInterface $entity, FieldDefinitionInterface $definition) {
$default_value = parent::processDefaultValue($default_value, $entity, $definition);
// Convert UUIDs to numeric IDs.
$uuids = array();
foreach ($default_value as $delta => $properties) {
$uuids[$delta] = $properties['target_uuid'];
}
if ($uuids) {
$entity_ids = \Drupal::entityQuery('taxonomy_term')
->condition('uuid', $uuids, 'IN')
->execute();
$entities = \Drupal::entityManager()
->getStorage('taxonomy_term')
->loadMultiple($entity_ids);
foreach ($entities as $id => $entity) {
$entity_ids[$entity->uuid()] = $id;
}
foreach ($uuids as $delta => $uuid) {
if (isset($entity_ids[$uuid])) {
$default_value[$delta]['target_id'] = $entity_ids[$uuid];
unset($default_value[$delta]['target_uuid']);
}
else {
unset($default_value[$delta]);
}
}
}
// Ensure we return consecutive deltas, in case we removed unknown UUIDs.
$default_value = array_values($default_value);
return $default_value;
}
}
......@@ -23,7 +23,7 @@
* description = @Translation("This field stores a reference to a taxonomy term."),
* default_widget = "options_select",
* default_formatter = "taxonomy_term_reference_link",
* list_class = "\Drupal\taxonomy\Plugin\Field\FieldType\TaxonomyTermReferenceFieldItemList"
* list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList"
* )
*/
class TaxonomyTermReferenceItem extends EntityReferenceItem implements AllowedValuesInterface {
......
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