diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index 73db361..88ea43e 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -769,11 +769,20 @@ text_format: # The text format should not be translated as part of the string # translation system, so this is not marked as translatable. -# Schema for the configuration of the Entity reference selection plugins. - +# Base schema for all entity reference selection handler schemas. entity_reference_selection: type: mapping - label: 'Entity reference selection plugin configuration' + label: 'Entity reference selection handler settings' + +# Schema for all entity reference selection handlers that are not providing a +# specific schema. +entity_reference_selection.*: + type: entity_reference_selection + +# Schema for the entity reference 'default' selection handler settings. +entity_reference_selection.default: + type: entity_reference_selection + label: 'Default selection handler settings' mapping: target_bundles: type: sequence @@ -781,7 +790,7 @@ entity_reference_selection: nullable: true sequence: type: string - label: 'Type' + label: 'Bundle' sort: type: mapping label: 'Sort settings' @@ -799,5 +808,7 @@ entity_reference_selection: type: string label: 'Bundle assigned to the auto-created entities.' -entity_reference_selection.*: - type: entity_reference_selection +# Schema for all entity reference 'default:*' selection handlers that are not +# providing a specific schema. +entity_reference_selection.default:*: + type: entity_reference_selection.default diff --git a/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginBase.php b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginBase.php new file mode 100644 index 0000000..68484e1 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginBase.php @@ -0,0 +1,149 @@ +setConfiguration($configuration); + $this->entityManager = $entity_manager; + $this->moduleHandler = $module_handler; + $this->currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager'), + $container->get('module_handler'), + $container->get('current_user') + ); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'target_type' => NULL, + 'handler' => NULL, + 'handler_settings' => $this->defaultHandlerSettings(), + ]; + } + + /** + * {@inheritdoc} + */ + public function getConfiguration() { + return $this->configuration; + } + + /** + * {@inheritdoc} + */ + public function setConfiguration(array $configuration) { + $this->configuration = NestedArray::mergeDeep( + $this->defaultConfiguration(), + $configuration + ); + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return []; + } + + /** + * Returns the default handler settings. + * + * @return array + * + * @todo Add this to SelectionInterface in Drupal 9.0.x. + */ + public function defaultHandlerSettings() { + return []; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { } + + /** + * {@inheritdoc} + */ + public function entityQueryAlter(SelectInterface $query) { } + +} diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php index d74fb6e..3686865 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/Broken.php @@ -3,7 +3,7 @@ namespace Drupal\Core\Entity\Plugin\EntityReferenceSelection; use Drupal\Core\Database\Query\SelectInterface; -use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface; +use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase; use Drupal\Core\Form\FormStateInterface; /** @@ -14,12 +14,13 @@ * label = @Translation("Broken/Missing") * ) */ -class Broken implements SelectionInterface { +class Broken extends SelectionPluginBase { /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); $form['selection_handler'] = array( '#markup' => t('The selected selection handler is broken.'), ); @@ -29,16 +30,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta /** * {@inheritdoc} */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { } - - /** - * {@inheritdoc} - */ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { return array(); } @@ -57,9 +48,4 @@ public function validateReferenceableEntities(array $ids) { return array(); } - /** - * {@inheritdoc} - */ - public function entityQueryAlter(SelectInterface $query) { } - } diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php index 9565e77..5d25347 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php @@ -4,18 +4,11 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Database\Query\AlterableInterface; -use Drupal\Core\Database\Query\SelectInterface; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase; use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Plugin\PluginBase; -use Drupal\Core\Session\AccountInterface; use Drupal\user\EntityOwnerInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Default plugin implementation of the Entity Reference Selection plugin. @@ -37,89 +30,36 @@ * deriver = "Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver" * ) */ -class DefaultSelection extends PluginBase implements SelectionInterface, SelectionWithAutocreateInterface, ContainerFactoryPluginInterface { - - /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - - /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** - * Constructs a new SelectionBase object. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager service. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - - $this->entityManager = $entity_manager; - $this->moduleHandler = $module_handler; - $this->currentUser = $current_user; - } +class DefaultSelection extends SelectionPluginBase implements SelectionWithAutocreateInterface { /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity.manager'), - $container->get('module_handler'), - $container->get('current_user') - ); + public function defaultHandlerSettings() { + return [ + // For the 'target_bundles' setting, a NULL value is equivalent to "allow + // entities from any bundle to be referenced" and an empty array value is + // equivalent to "no entities from any bundle can be referenced". + 'target_bundles' => NULL, + 'sort' => [ + 'field' => '_none', + ], + 'auto_create' => FALSE, + 'auto_create_bundle' => NULL, + ] + parent::defaultHandlerSettings(); } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + $entity_type_id = $this->configuration['target_type']; $selection_handler_settings = $this->configuration['handler_settings']; $entity_type = $this->entityManager->getDefinition($entity_type_id); $bundles = $this->entityManager->getBundleInfo($entity_type_id); - // Merge-in default values. - $selection_handler_settings += array( - // For the 'target_bundles' setting, a NULL value is equivalent to "allow - // entities from any bundle to be referenced" and an empty array value is - // equivalent to "no entities from any bundle can be referenced". - 'target_bundles' => NULL, - 'sort' => array( - 'field' => '_none', - ), - 'auto_create' => FALSE, - 'auto_create_bundle' => NULL, - ); - if ($entity_type->hasKey('bundle')) { $bundle_options = array(); foreach ($bundles as $bundle_name => $bundle_info) { @@ -247,6 +187,8 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::validateConfigurationForm($form, $form_state); + // If no checkboxes were checked for 'target_bundles', store NULL ("all // bundles are referenceable") rather than empty array ("no bundle is // referenceable" - typically happens when all referenceable bundles have @@ -261,11 +203,6 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form } /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { } - - /** * Form element validation handler; Filters the #value property of an element. */ public static function elementValidateFilter(&$element, FormStateInterface $form_state) { @@ -416,11 +353,6 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') } /** - * {@inheritdoc} - */ - public function entityQueryAlter(SelectInterface $query) { } - - /** * Helper method: Passes a query to the alteration system again. * * This allows Entity Reference to add a tag to an existing query so it can diff --git a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php index 2492fc4..414c5b2 100644 --- a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php +++ b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php @@ -66,6 +66,8 @@ public function validateReferenceableNewEntities(array $entities) { * {@inheritdoc} */ public function entityQueryAlter(SelectInterface $query) { + parent::entityQueryAlter($query); + $tables = $query->getTables(); $data_table = 'comment_field_data'; if (!isset($tables['comment_field_data']['alias'])) { diff --git a/core/modules/file/config/schema/file.schema.yml b/core/modules/file/config/schema/file.schema.yml index b9f8918..9526758 100644 --- a/core/modules/file/config/schema/file.schema.yml +++ b/core/modules/file/config/schema/file.schema.yml @@ -48,7 +48,7 @@ base_file_field_field_settings: label: 'Reference method' handler_settings: type: entity_reference_selection.[%parent.handler] - label: 'Entity reference selection settings' + label: 'File selection handler settings' file_directory: type: string label: 'File directory' diff --git a/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php b/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php index 3b1c236..028246a 100644 --- a/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php +++ b/core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php @@ -24,13 +24,6 @@ class TermSelection extends DefaultSelection { /** * {@inheritdoc} */ - public function entityQueryAlter(SelectInterface $query) { - // @todo: How to set access, as vocabulary is now config? - } - - /** - * {@inheritdoc} - */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); diff --git a/core/modules/user/config/schema/user.schema.yml b/core/modules/user/config/schema/user.schema.yml index 627d8a6..2f9bda4 100644 --- a/core/modules/user/config/schema/user.schema.yml +++ b/core/modules/user/config/schema/user.schema.yml @@ -166,8 +166,10 @@ condition.plugin.user_role: sequence: type: string +# Schema for the entity reference 'default:user' selection handler settings. entity_reference_selection.default:user: - type: entity_reference_selection + type: entity_reference_selection.default + label: 'User selection handler settings' mapping: filter: type: mapping diff --git a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php index 8dfba39..2b0e411 100644 --- a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php +++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php @@ -82,16 +82,20 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $selection_handler_settings = $this->configuration['handler_settings']; - - // Merge in default values. - $selection_handler_settings += array( - 'filter' => array( + public function defaultHandlerSettings() { + return [ + 'filter' => [ 'type' => '_none', - ), + ], 'include_anonymous' => TRUE, - ); + ] + parent::defaultHandlerSettings(); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $selection_handler_settings = $this->getConfiguration()['handler_settings']; $form['include_anonymous'] = array( '#type' => 'checkbox', @@ -209,6 +213,8 @@ public function validateReferenceableNewEntities(array $entities) { * {@inheritdoc} */ public function entityQueryAlter(SelectInterface $query) { + parent::entityQueryAlter($query); + // Bail out early if we do not need to match the Anonymous user. $handler_settings = $this->configuration['handler_settings']; if (isset($handler_settings['include_anonymous']) && !$handler_settings['include_anonymous']) { diff --git a/core/modules/views/config/schema/views.entity_reference.schema.yml b/core/modules/views/config/schema/views.entity_reference.schema.yml index 027c62f..f13645a 100644 --- a/core/modules/views/config/schema/views.entity_reference.schema.yml +++ b/core/modules/views/config/schema/views.entity_reference.schema.yml @@ -1,8 +1,8 @@ -# Schema for the views entity reference selection plugins. +# Schema for the entity reference 'views' selection handler settings. entity_reference_selection.views: - type: mapping - label: 'View handler settings' + type: entity_reference_selection + label: 'Views selection handler settings' mapping: view: type: mapping diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index db19052..8464b6b 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -3,16 +3,10 @@ namespace Drupal\views\Plugin\EntityReferenceSelection; use Drupal\Core\Database\Query\SelectInterface; -use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Plugin\PluginBase; -use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\views\Views; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin implementation of the 'selection' entity_reference. @@ -24,80 +18,35 @@ * weight = 0 * ) */ -class ViewsSelection extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { +class ViewsSelection extends SelectionPluginBase { /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - - /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** - * Constructs a new SelectionBase object. + * The loaded View object. * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager service. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. + * @var \Drupal\views\ViewExecutable; */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - - $this->entityManager = $entity_manager; - $this->moduleHandler = $module_handler; - $this->currentUser = $current_user; - } + protected $view; /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity.manager'), - $container->get('module_handler'), - $container->get('current_user') - ); + public function defaultHandlerSettings() { + return [ + 'view' => [ + 'view_name' => NULL, + 'display_name' => NULL, + 'arguments' => [], + ], + ] + parent::defaultHandlerSettings(); } /** - * The loaded View object. - * - * @var \Drupal\views\ViewExecutable; - */ - protected $view; - - /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $selection_handler_settings = $this->configuration['handler_settings']; - $view_settings = !empty($selection_handler_settings['view']) ? $selection_handler_settings['view'] : array(); + $form = parent::buildConfigurationForm($form, $form_state); + + $view_settings = $this->getConfiguration()['handler_settings']['view']; $displays = Views::getApplicableViews('entity_reference_display'); // Filter views that list the entity type we want, and group the separate // displays by view. @@ -157,16 +106,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { } - - /** * Initializes a view. * * @param string|null $match @@ -283,9 +222,4 @@ public static function settingsFormValidate($element, FormStateInterface $form_s $form_state->setValueForElement($element, $value); } - /** - * {@inheritdoc} - */ - public function entityQueryAlter(SelectInterface $query) { } - }