From 42ebadcac2cbc446bb01cf160453d1611b811474 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 29 Jul 2013 13:57:27 +0200 Subject: [PATCH] Issue #2017851 by pcambra, amateescu, Pancho: Move entity_reference_get_selection_handler() to a method on SelectionPluginManager. --- .../entity_reference/entity_reference.module | 19 +++------------- .../entity_reference.services.yml | 2 +- .../EntityReferenceAutocomplete.php | 17 +++++++++++--- .../Plugin/Type/SelectionPluginManager.php | 22 +++++++++++++++++++ .../field/widget/AutocompleteTagsWidget.php | 2 +- .../field/widget/AutocompleteWidget.php | 2 +- .../EntityReferenceSelectionAccessTest.php | 2 +- .../EntityReferenceSelectionSortTest.php | 4 ++-- .../Tests/Views/SelectionTest.php | 2 +- 9 files changed, 46 insertions(+), 26 deletions(-) diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index 54a7bac57649..a13d5cddb4aa 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -76,19 +76,6 @@ function entity_reference_field_widget_info_alter(&$info) { } } -/** - * Gets the selection handler for a given entity_reference field. - * - * @return \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface - */ -function entity_reference_get_selection_handler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) { - $options = array( - 'field_definition' => $field_definition, - 'entity' => $entity, - ); - return Drupal::service('plugin.manager.entity_reference.selection')->getInstance($options); -} - /** * Implements hook_field_presave(). * @@ -115,7 +102,7 @@ function entity_reference_field_validate(EntityInterface $entity = NULL, $field, } if ($ids) { - $valid_ids = entity_reference_get_selection_handler($instance, $entity)->validateReferenceableEntities(array_keys($ids)); + $valid_ids = Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($instance, $entity)->validateReferenceableEntities(array_keys($ids)); $invalid_entities = array_diff_key($ids, array_flip($valid_ids)); if ($invalid_entities) { @@ -256,7 +243,7 @@ function entity_reference_field_instance_settings_form($field, $instance, $form_ '#attributes' => array('class' => array('entity_reference-settings')), ); - $handler = entity_reference_get_selection_handler($instance); + $handler = Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($instance); $form['handler']['handler_settings'] += $handler->settingsForm($field, $instance); return $form; @@ -353,7 +340,7 @@ function entity_reference_settings_ajax_submit($form, &$form_state) { * Implements hook_options_list(). */ function entity_reference_options_list(FieldDefinitionInterface $field_definition, EntityInterface $entity) { - if (!$options = entity_reference_get_selection_handler($field_definition, $entity)->getReferenceableEntities()) { + if (!$options = Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition, $entity)->getReferenceableEntities()) { return array(); } diff --git a/core/modules/entity_reference/entity_reference.services.yml b/core/modules/entity_reference/entity_reference.services.yml index 91842ca98e38..e06ef888f0fd 100644 --- a/core/modules/entity_reference/entity_reference.services.yml +++ b/core/modules/entity_reference/entity_reference.services.yml @@ -4,4 +4,4 @@ services: arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler'] entity_reference.autocomplete: class: Drupal\entity_reference\EntityReferenceAutocomplete - arguments: ['@plugin.manager.entity'] + arguments: ['@plugin.manager.entity', '@plugin.manager.entity_reference.selection'] diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php index de01c9cfb957..492fa25b9602 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php @@ -4,9 +4,11 @@ * @file * Contains \Drupal\entity_reference/EntityReferenceAutocomplete. */ + namespace Drupal\entity_reference; use Drupal\Core\Entity\EntityManager; +use Drupal\entity_reference\Plugin\Type\SelectionPluginManager; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; /** @@ -21,14 +23,24 @@ class EntityReferenceAutocomplete { */ protected $entityManager; + /** + * The Entity reference selection handler plugin manager. + * + * @var \Drupal\entity_reference\Plugin\Type\SelectionPluginManager + */ + protected $selectionHandlerManager; + /** * Constructs a EntityReferenceAutocomplete object. * * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager. + * @param \Drupal\entity_reference\Plugin\Type\SelectionPluginManager $selection_manager + * The Entity reference selection handler plugin manager. */ - public function __construct(EntityManager $entity_manager) { + public function __construct(EntityManager $entity_manager, SelectionPluginManager $selection_manager) { $this->entityManager = $entity_manager; + $this->selectionHandlerManager = $selection_manager; } /** @@ -60,7 +72,6 @@ class EntityReferenceAutocomplete { * @see \Drupal\entity_reference\EntityReferenceController */ public function getMatches($field, $instance, $entity_type, $entity_id = '', $prefix = '', $string = '') { - $target_type = $field['settings']['target_type']; $matches = array(); $entity = NULL; @@ -70,7 +81,7 @@ class EntityReferenceAutocomplete { throw new AccessDeniedHttpException(); } } - $handler = entity_reference_get_selection_handler($instance, $entity); + $handler = $this->selectionHandlerManager->getSelectionHandler($instance, $entity); if (isset($string)) { // Get an array of matching entities. diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php index bce6ebfb8aea..11582f274b45 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/SelectionPluginManager.php @@ -10,6 +10,8 @@ namespace Drupal\entity_reference\Plugin\Type; use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Plugin\Factory\ReflectionFactory; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\Field\FieldDefinitionInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Plugin\DefaultPluginManager; @@ -88,4 +90,24 @@ class SelectionPluginManager extends DefaultPluginManager { return $plugins; } + + /** + * Gets the selection handler for a given entity_reference field. + * + * @param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field_definition + * The field definition for the operation. + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity for the operation. + * + * @return \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface + * The selection plugin. + */ + public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) { + $options = array( + 'field_definition' => $field_definition, + 'entity' => $entity, + ); + return $this->getInstance($options); + } + } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php index 52e082de4a5d..6d46c2f4d6c5 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php @@ -38,7 +38,7 @@ class AutocompleteTagsWidget extends AutocompleteWidgetBase { public function elementValidate($element, &$form_state, $form) { $value = array(); // If a value was entered into the autocomplete. - $handler = entity_reference_get_selection_handler($this->fieldDefinition); + $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->fieldDefinition); $bundles = entity_get_bundles($this->getFieldSetting('target_type')); $auto_create = $this->getSelectionHandlerSetting('auto_create'); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php index 3dcbdcc1e0ee..fb2b575fbb45 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php @@ -69,7 +69,7 @@ class AutocompleteWidget extends AutocompleteWidgetBase { else { // Try to get a match from the input string when the user didn't use the // autocomplete but filled in a value manually. - $handler = entity_reference_get_selection_handler($this->fieldDefinition); + $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->fieldDefinition); $value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form, !$auto_create); } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php index ba21eea3d9a0..ebfccca2a0b9 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionAccessTest.php @@ -34,7 +34,7 @@ class EntityReferenceSelectionAccessTest extends WebTestBase { } protected function assertReferenceable(FieldDefinitionInterface $field_definition, $tests, $handler_name) { - $handler = entity_reference_get_selection_handler($field_definition); + $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition); foreach ($tests as $test) { foreach ($test['arguments'] as $arguments) { diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php index 558c1ce172d3..b44c54d25e02 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php @@ -121,7 +121,7 @@ class EntityReferenceSelectionSortTest extends WebTestBase { $normal_user = $this->drupalCreateUser(array('access content')); $GLOBALS['user'] = $normal_user; - $handler = entity_reference_get_selection_handler($instance); + $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance); // Not only assert the result, but make sure the keys are sorted as // expected. @@ -137,7 +137,7 @@ class EntityReferenceSelectionSortTest extends WebTestBase { 'field' => 'nid', 'direction' => 'ASC', ); - $handler = entity_reference_get_selection_handler($instance); + $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance); $result = $handler->getReferenceableEntities(); $expected_result = array( $nodes['published1']->id() => $node_labels['published1'], diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php index 01cb7768cb11..cad4bd8e8d36 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php @@ -70,7 +70,7 @@ class SelectionTest extends WebTestBase { $instance->save(); // Get values from selection handler. - $handler = entity_reference_get_selection_handler($instance); + $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance); $result = $handler->getReferenceableEntities(); $success = FALSE;