Commit edc0ccc1 authored by catch's avatar catch

Issue #2160575 by agentrickard, amateescu, plopesc: Option widgets integration...

Issue #2160575 by agentrickard, amateescu, plopesc: Option widgets integration is broken for the entity reference field.
parent 55890b47
...@@ -167,27 +167,6 @@ function entity_reference_settings_ajax_submit($form, &$form_state) { ...@@ -167,27 +167,6 @@ function entity_reference_settings_ajax_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE; $form_state['rebuild'] = TRUE;
} }
/**
* Implements hook_options_list().
*/
function entity_reference_options_list(FieldDefinitionInterface $field_definition, EntityInterface $entity) {
if (!$options = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition, $entity)->getReferenceableEntities()) {
return array();
}
// Rebuild the array by changing the bundle key into the bundle label.
$target_type = $field_definition->getSetting('target_type');
$bundles = entity_get_bundles($target_type);
$return = array();
foreach ($options as $bundle => $entity_ids) {
$bundle_label = check_plain($bundles[$bundle]['label']);
$return[$bundle_label] = $entity_ids;
}
return count($return) == 1 ? reset($return) : $return;
}
/** /**
* Implements hook_query_TAG_alter(). * Implements hook_query_TAG_alter().
*/ */
......
...@@ -7,10 +7,14 @@ ...@@ -7,10 +7,14 @@
namespace Drupal\entity_reference; namespace Drupal\entity_reference;
use Drupal\Component\Utility\String;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\AllowedValuesInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\ConfigEntityReferenceItemBase; use Drupal\Core\Field\ConfigEntityReferenceItemBase;
use Drupal\Core\Field\ConfigFieldItemInterface; use Drupal\Core\Field\ConfigFieldItemInterface;
use Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint;
/** /**
* Alternative plugin implementation of the 'entity_reference' field type. * Alternative plugin implementation of the 'entity_reference' field type.
...@@ -24,7 +28,53 @@ ...@@ -24,7 +28,53 @@
* @see entity_reference_field_info_alter(). * @see entity_reference_field_info_alter().
* *
*/ */
class ConfigurableEntityReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface { class ConfigurableEntityReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface, AllowedValuesInterface {
/**
* {@inheritdoc}
*/
public function getPossibleValues(AccountInterface $account = NULL) {
return $this->getSettableValues($account);
}
/**
* {@inheritdoc}
*/
public function getPossibleOptions(AccountInterface $account = NULL) {
return $this->getSettableOptions($account);
}
/**
* {@inheritdoc}
*/
public function getSettableValues(AccountInterface $account = NULL) {
// Flatten options firstly, because Settable Options may contain group
// arrays.
$flatten_options = \Drupal::formBuilder()->flattenOptions($this->getSettableOptions($account));
return array_keys($flatten_options);
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
$field_definition = $this->getFieldDefinition();
if (!$options = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition, $this->getEntity())->getReferenceableEntities()) {
return array();
}
// Rebuild the array by changing the bundle key into the bundle label.
$target_type = $field_definition->getSetting('target_type');
$bundles = \Drupal::entityManager()->getBundleInfo($target_type);
$return = array();
foreach ($options as $bundle => $entity_ids) {
$bundle_label = String::checkPlain($bundles[$bundle]['label']);
$return[$bundle_label] = $entity_ids;
}
return count($return) == 1 ? reset($return) : $return;
}
/** /**
* Definitions of the contained properties. * Definitions of the contained properties.
...@@ -63,6 +113,23 @@ public function getPropertyDefinitions() { ...@@ -63,6 +113,23 @@ public function getPropertyDefinitions() {
return static::$propertyDefinitions[$key]; return static::$propertyDefinitions[$key];
} }
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = parent::getConstraints();
// Remove the 'AllowedValuesConstraint' validation constraint because entity
// reference fields already use the 'ValidReference' constraint.
foreach ($constraints as $key => $constraint) {
if ($constraint instanceof AllowedValuesConstraint) {
unset($constraints[$key]);
}
}
return $constraints;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -40,7 +40,7 @@ class EntityReferenceIntegrationTest extends WebTestBase { ...@@ -40,7 +40,7 @@ class EntityReferenceIntegrationTest extends WebTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('config_test', 'entity_test', 'entity_reference'); public static $modules = array('config_test', 'entity_test', 'entity_reference', 'options');
public static function getInfo() { public static function getInfo() {
return array( return array(
...@@ -62,7 +62,7 @@ public function setUp() { ...@@ -62,7 +62,7 @@ public function setUp() {
} }
/** /**
* Tests the entity reference field with all its widgets. * Tests the entity reference field with all its supported field widgets.
*/ */
public function testSupportedEntityTypesAndWidgets() { public function testSupportedEntityTypesAndWidgets() {
foreach ($this->getTestEntities() as $referenced_entities) { foreach ($this->getTestEntities() as $referenced_entities) {
...@@ -111,6 +111,22 @@ public function testSupportedEntityTypesAndWidgets() { ...@@ -111,6 +111,22 @@ public function testSupportedEntityTypesAndWidgets() {
$entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
$this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save')); $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
$this->assertFieldValues($entity_name, $referenced_entities); $this->assertFieldValues($entity_name, $referenced_entities);
// Test all the other widgets supported by the entity reference field.
// Since we don't know the form structure for these widgets, just test
// that editing and saving an already created entity works.
$entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
$supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference');
$supported_widget_types = array_diff(array_keys($supported_widgets), array('entity_reference_autocomplete', 'entity_reference_autocomplete_tags'));
foreach ($supported_widget_types as $widget_type) {
entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array(
'type' => $widget_type,
))->save();
$this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
$this->assertFieldValues($entity_name, $referenced_entities);
}
} }
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\taxonomy\Plugin\entity_reference\selection; namespace Drupal\taxonomy\Plugin\entity_reference\selection;
use Drupal\Component\Utility\String;
use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase; use Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase;
...@@ -63,9 +64,9 @@ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTA ...@@ -63,9 +64,9 @@ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTA
foreach ($bundle_names as $bundle) { foreach ($bundle_names as $bundle) {
if ($vocabulary = entity_load('taxonomy_vocabulary', $bundle)) { if ($vocabulary = entity_load('taxonomy_vocabulary', $bundle)) {
if ($terms = taxonomy_get_tree($vocabulary->id(), 0)) { if ($terms = taxonomy_get_tree($vocabulary->id(), 0, NULL, TRUE)) {
foreach ($terms as $term) { foreach ($terms as $term) {
$options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . check_plain($term->name); $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . String::checkPlain($term->label());
} }
} }
} }
......
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