diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php index c951cf4..c7c9c1a 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php @@ -36,7 +36,7 @@ class DefaultSelection extends SelectionPluginBase implements SelectionWithAutoc /** * {@inheritdoc} */ - public function getDefaultHandlerSettings() { + 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 @@ -412,7 +412,8 @@ public function onDependencyRemoval(array $dependencies, FieldDefinitionInterfac // Update the 'target_bundles' handler setting if a bundle config dependency // has been removed. - $handler_settings = $this->getConfiguration()['handler_settings']; + $configuration = $this->getConfiguration(); + $handler_settings = $configuration['handler_settings']; if (!empty($handler_settings['target_bundles'])) { $target_entity_type = $this->entityManager->getDefinition($this->configuration['target_type']); if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) { @@ -428,22 +429,26 @@ public function onDependencyRemoval(array $dependencies, FieldDefinitionInterfac $handler_settings['auto_create'] = NULL; $handler_settings['auto_create_bundle'] = NULL; } + } + } - $this->setConfiguration(['handler_settings' => $handler_settings]); - $changed = TRUE; - - // In case we deleted the only target bundle allowed by the field - // we have to log a critical message because the field will not - // function correctly anymore. - if ($handler_settings['target_bundles'] === []) { - \Drupal::logger('entity_reference')->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [ - '%target_bundle' => $bundle->label(), - '%target_entity_type' => $bundle->getEntityType()->getBundleOf(), - '%field_name' => $field_definition->getName(), - '%entity_type' => $field_definition->getTargetEntityTypeId(), - '%bundle' => $field_definition->getTargetBundle() - ]); - } + if ($handler_settings !== $configuration['handler_settings']) { + // Update the configuration only if handler settings were changed. + $configuration['handler_settings'] = $handler_settings; + $this->setConfiguration($configuration); + $changed = TRUE; + + // In case we deleted the only target bundle allowed by the field we + // have to log a critical message because the field will not + // function correctly anymore. + if (!empty($bundle) && ($handler_settings['target_bundles'] === [])) { + \Drupal::logger('entity_reference')->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [ + '%target_bundle' => $bundle->label(), + '%target_entity_type' => $bundle->getEntityType()->getBundleOf(), + '%field_name' => $field_definition->getName(), + '%entity_type' => $field_definition->getTargetEntityTypeId(), + '%bundle' => $field_definition->getTargetBundle() + ]); } } } diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionPluginBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionPluginBase.php index d998f96..2617f91 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionPluginBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionPluginBase.php @@ -82,7 +82,11 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function defaultConfiguration() { - return ['handler_settings' => $this->getDefaultHandlerSettings()]; + return [ + 'target_type' => NULL, + 'handler' => NULL, + 'handler_settings' => $this->defaultHandlerSettings(), + ]; } /** @@ -123,7 +127,7 @@ public function onDependencyRemoval(array $dependencies) { * * @todo Add this to SelectionInterface in Drupal 9.0.x. */ - public function getDefaultHandlerSettings() { + public function defaultHandlerSettings() { return []; } diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index 53114a0..d6f9ded 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -38,9 +38,21 @@ class ViewsSelection extends SelectionPluginBase { /** * {@inheritdoc} */ + public function defaultHandlerSettings() { + return [ + 'view' => [ + 'view_name' => NULL, + 'display_name' => NULL, + 'arguments' => [], + ], + ] + parent::defaultHandlerSettings(); + } + + /** + * {@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(); + $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. @@ -136,11 +148,10 @@ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $ $display_name = $handler_settings['view']['display_name']; // Check that the view is valid and the display still exists. - $this->view = Views::getView($view_name); - if (!$this->view) { + if (!$view_name || !($this->view = Views::getView($view_name))) { throw new FieldException("The reference view $view_name cannot be found."); } - elseif (!$this->view->access($display_name)) { + elseif (!$display_name || !$this->view->access($display_name)) { throw new FieldException("The reference display $view_name:$display_name doesn't exist or is inaccessible."); } $this->view->setDisplay($display_name);