diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index 86014e4..b192687 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -552,7 +552,7 @@ field.value.changed: field.storage_settings.entity_reference: type: mapping - label: 'Entity reference settings' + label: 'Entity reference storage settings' mapping: target_type: type: string @@ -560,13 +560,13 @@ field.storage_settings.entity_reference: field.field_settings.entity_reference: type: mapping - label: 'Entity reference settings' + label: 'Entity reference field settings' mapping: handler: type: string label: 'Reference method' handler_settings: - type: entity_reference.[%parent.handler].handler_settings + type: entity_reference_selection.[%parent.handler] label: 'Reference method settings' field.value.entity_reference: @@ -750,3 +750,32 @@ text_format: label: '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. + +entity_reference_selection: + type: mapping + label: 'View handler settings' + mapping: + target_bundles: + type: sequence + label: 'types' + sequence: + type: string + label: 'Type' + sort: + type: mapping + label: 'Sort settings' + mapping: + field: + type: string + label: 'Sort by' + direction: + type: string + label: 'Sort direction' + auto_create: + type: boolean + label: 'Create referenced entities if they don''t already exist' + +entity_reference_selection.*: + type: entity_reference_selection diff --git a/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginManager.php b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginManager.php index 089dee8..0d7896c 100644 --- a/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginManager.php +++ b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionPluginManager.php @@ -45,17 +45,20 @@ public function getInstance(array $options) { // Initialize default options. $options += array( - 'handler' => 'default', + 'handler' => 'default:' . $options['target_type'], 'handler_settings' => array(), ); // Get all available selection plugins for this entity type. $selection_handler_groups = $this->getSelectionGroups($options['target_type']); + // Extract the selection group from the handler. + list($selection_group) = explode(':', $options['handler'], 2); + // Sort the selection plugins by weight and select the best match. - uasort($selection_handler_groups[$options['handler']], array('Drupal\Component\Utility\SortArray', 'sortByWeightElement')); - end($selection_handler_groups[$options['handler']]); - $plugin_id = key($selection_handler_groups[$options['handler']]); + uasort($selection_handler_groups[$selection_group], array('Drupal\Component\Utility\SortArray', 'sortByWeightElement')); + end($selection_handler_groups[$selection_group]); + $plugin_id = key($selection_handler_groups[$selection_group]); return $this->createInstance($plugin_id, $options); } @@ -83,10 +86,11 @@ public function getSelectionGroups($entity_type_id) { * {@inheritdoc} */ public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) { + $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type'); $options = array( - 'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'), - 'handler' => $field_definition->getSetting('handler'), - 'handler_settings' => $field_definition->getSetting('handler_settings'), + 'target_type' => $target_type, + 'handler' => $field_definition->getSetting('handler') ?: 'default:' . $target_type, + 'handler_settings' => $field_definition->getSetting('handler_settings') ?: [], 'entity' => $entity, ); return $this->getInstance($options); diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index 07ec8cd..3293951 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Entity\Plugin\EntityReferenceSelection; +use Drupal\Component\Plugin\ConfigurablePluginInterface; +use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; use Drupal\Core\Database\Query\AlterableInterface; use Drupal\Core\Database\Query\SelectInterface; @@ -39,7 +41,7 @@ * deriver = "Drupal\Core\Entity\Plugin\Derivative\SelectionBase" * ) */ -class SelectionBase extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { +class SelectionBase extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface, ConfigurablePluginInterface { /** * The entity manager. @@ -80,7 +82,7 @@ class SelectionBase extends PluginBase implements SelectionInterface, ContainerF */ 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->setConfiguration($configuration); $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; $this->currentUser = $current_user; @@ -109,15 +111,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta $entity_type = $this->entityManager->getDefinition($entity_type_id); $bundles = $this->entityManager->getBundleInfo($entity_type_id); - // Merge-in default values. - $selection_handler_settings += array( - 'target_bundles' => array(), - 'sort' => array( - 'field' => '_none', - ), - 'auto_create' => FALSE, - ); - if ($entity_type->hasKey('bundle')) { $bundle_options = array(); foreach ($bundles as $bundle_name => $bundle_info) { @@ -388,4 +381,47 @@ protected function reAlterQuery(AlterableInterface $query, $tag, $base_table) { $query->alterMetaData = $old_metadata; } + /** + * {@inheritdoc} + */ + public function setConfiguration(array $configuration) { + $this->configuration = NestedArray::mergeDeep( + $this->defaultConfiguration(), + $configuration + ); + } + /** + * Returns default configuration for this plugin. + * + * @return array + * An associative array with the default configuration. + */ + public function defaultConfiguration() { + return [ + 'handler' => $this->getPluginId(), + 'handler_settings' => [ + 'target_bundles' => [], + 'sort' => [ + 'field' => '_none', + ], + 'auto_create' => FALSE, + ] + ]; + } + + /** + * {@inheritdoc} + */ + public function getConfiguration() { + return $this->configuration; + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + // @todo add dependency on target bundles. + return []; + } + } diff --git a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php index 244f25b..77ec81d 100644 --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@ -63,7 +63,7 @@ public static function create($type) { // settings for the field type. // @todo Cleanup in https://drupal.org/node/2116341. $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); - $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type); + $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type, $field_definition); $field_definition->itemDefinition->setSettings($default_settings); return $field_definition; } diff --git a/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php b/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php index b6b2a3c..7f4a18d 100644 --- a/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php +++ b/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php @@ -158,7 +158,7 @@ public function preSave(EntityStorageInterface $storage) { // that a complete field definition is passed to the various hooks and // written to config. $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); - $default_settings = $field_type_manager->getDefaultFieldSettings($this->getType()); + $default_settings = $field_type_manager->getDefaultFieldSettings($this->getType(), $this); $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings; // Call the parent's presave method to perform validate and calculate diff --git a/core/lib/Drupal/Core/Field/FieldTypePluginManager.php b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php index bb35ae6..c869b7f 100644 --- a/core/lib/Drupal/Core/Field/FieldTypePluginManager.php +++ b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php @@ -117,11 +117,11 @@ public function getDefaultStorageSettings($type) { /** * {@inheritdoc} */ - public function getDefaultFieldSettings($type) { + public function getDefaultFieldSettings($type, FieldDefinitionInterface $field = NULL) { $plugin_definition = $this->getDefinition($type, FALSE); if (!empty($plugin_definition['class'])) { $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition); - return $plugin_class::defaultFieldSettings(); + return $plugin_class::defaultFieldSettings($field); } return array(); } diff --git a/core/modules/entity_reference/config/schema/entity_reference.schema.yml b/core/modules/entity_reference/config/schema/entity_reference.schema.yml deleted file mode 100644 index cfb29ea..0000000 --- a/core/modules/entity_reference/config/schema/entity_reference.schema.yml +++ /dev/null @@ -1,41 +0,0 @@ -# Schema for the configuration files of the Entity Reference module. - -entity_reference.default.handler_settings: - type: mapping - label: 'View handler settings' - mapping: - target_bundles: - type: sequence - label: 'types' - sequence: - type: string - label: 'Type' - sort: - type: mapping - label: 'Sort settings' - mapping: - field: - type: string - label: 'Sort by' - direction: - type: string - label: 'Sort direction' - filter: - type: mapping - label: 'Filter settings' - mapping: - type: - type: string - label: 'Filter by' - role: - type: sequence - label: 'Restrict to the selected roles' - sequence: - type: string - label: 'Role' - auto_create: - type: boolean - label: 'Create referenced entities if they don''t already exist' - include_anonymous: - type: boolean - label: 'Include the anonymous user in the matched entities.' diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index 526c60c..c54a1b3 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -194,7 +194,8 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query * @param string $target_entity_type * The type of the referenced entity. * @param string $selection_handler - * The selection handler used by this field. + * The selection handler used by this field. Defaults to NULL. If NULL then + * the default handler for the target entity type will be used. * @param array $selection_handler_settings * An array of settings supported by the selection handler specified above. * (e.g. 'target_bundles', 'sort', 'auto_create', etc). @@ -203,7 +204,7 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query * * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm() */ -function entity_reference_create_field($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1) { +function entity_reference_create_field($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = NULL, $selection_handler_settings = array(), $cardinality = 1) { // Look for or add the specified field to the requested entity bundle. if (!FieldStorageConfig::loadByName($entity_type, $field_name)) { entity_create('field_storage_config', array( @@ -217,15 +218,18 @@ function entity_reference_create_field($entity_type, $bundle, $field_name, $fiel ))->save(); } if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) { + $settings = array( + 'handler_settings' => $selection_handler_settings, + ); + if ($selection_handler) { + $settings['handler'] = $selection_handler; + } entity_create('field_config', array( 'field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'label' => $field_label, - 'settings' => array( - 'handler' => $selection_handler, - 'handler_settings' => $selection_handler_settings, - ), + 'settings' => $settings, ))->save(); } } diff --git a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php index 5f0f75d..50a8ace 100644 --- a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php @@ -7,8 +7,10 @@ namespace Drupal\entity_reference; +use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; -use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\OptGroup; @@ -45,10 +47,20 @@ public static function defaultStorageSettings() { /** * {@inheritdoc} */ - public static function defaultFieldSettings() { - return array( - 'handler_settings' => array(), - ) + parent::defaultFieldSettings(); + public static function defaultFieldSettings(FieldDefinitionInterface $field = NULL) { + $defaults = [ + 'handler' => 'default', + 'handler_settings' => [] + ]; + + // Get the default settings from the entity reference selection plugin if + // possible. + if ($field && $field->getFieldStorageDefinition()->getSetting('target_type')) { + $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field); + $defaults = NestedArray::mergeDeep($defaults, $handler->defaultConfiguration()); + } + + return $defaults; } /** @@ -149,7 +161,7 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { } elseif (array_key_exists($selection_group_id . ':' . $this->getSetting('target_type'), $selection_plugins[$selection_group_id])) { $selection_group_plugin = $selection_group_id . ':' . $this->getSetting('target_type'); - $handlers_options[$selection_group_id] = String::checkPlain($selection_plugins[$selection_group_id][$selection_group_plugin]['base_plugin_label']); + $handlers_options[$selection_group_plugin] = String::checkPlain($selection_plugins[$selection_group_id][$selection_group_plugin]['base_plugin_label']); } } @@ -168,11 +180,12 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { '#process' => array('_entity_reference_form_process_merge_parent'), ); + $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field); $form['handler']['handler'] = array( '#type' => 'select', '#title' => t('Reference method'), '#options' => $handlers_options, - '#default_value' => $field->getSetting('handler'), + '#default_value' => $handler->getPluginId(), '#required' => TRUE, '#ajax' => TRUE, '#limit_validation_errors' => array(), @@ -192,7 +205,6 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { '#attributes' => array('class' => array('entity_reference-settings')), ); - $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field); $form['handler']['handler_settings'] += $handler->buildConfigurationForm(array(), $form_state); return $form; diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index 19ef644..e3fcc16 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -78,7 +78,7 @@ public function testFieldAdminHandler() { $this->drupalPostForm(NULL, array(), t('Save field settings')); // The base handler should be selected by default. - $this->assertFieldByName('field[settings][handler]', 'default'); + $this->assertFieldByName('field[settings][handler]', 'default:node'); // The base handler settings should be displayed. $entity_type_id = 'node'; diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php index dc87dc8..4d84f81 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php @@ -62,7 +62,6 @@ protected function setUp() { 'entity_type' => 'node', 'bundle' => $referencing->id(), 'settings' => array( - 'handler' => 'default', 'handler_settings' => array( // Reference a single vocabulary. 'target_bundles' => array( diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceFieldDefaultValueTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceFieldDefaultValueTest.php index c2ceed9..8ce5f08 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceFieldDefaultValueTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldDefaultValueTest.php @@ -65,7 +65,6 @@ function testEntityReferenceDefaultValue() { 'field_storage' => $field_storage, 'bundle' => 'reference_content', 'settings' => array( - 'handler' => 'default', 'handler_settings' => array( 'target_bundles' => array('referenced_content'), 'sort' => array('field' => '_none'), @@ -129,7 +128,6 @@ function testEntityReferenceDefaultConfigValue() { 'field_storage' => $field_storage, 'bundle' => 'reference_content', 'settings' => array( - 'handler' => 'default', 'handler_settings' => array( 'sort' => array('field' => '_none'), ), diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php index 2203fd8..1c3198b 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php @@ -68,7 +68,7 @@ public function testSupportedEntityTypesAndWidgets() { $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId(); // Create an Entity reference field. - entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2); + entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), NULL, array(), 2); // Test the default 'entity_reference_autocomplete' widget. entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save(); diff --git a/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php b/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php index 9ae79a7..05557be 100644 --- a/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php +++ b/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php @@ -69,10 +69,6 @@ protected function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'field_test', 'bundle' => 'entity_test', - 'settings' => array( - 'handler' => 'default', - 'handler_settings' => array(), - ), )); $field->save(); diff --git a/core/modules/field/src/Entity/FieldConfig.php b/core/modules/field/src/Entity/FieldConfig.php index 802bc1b..e6c5df1 100644 --- a/core/modules/field/src/Entity/FieldConfig.php +++ b/core/modules/field/src/Entity/FieldConfig.php @@ -7,6 +7,7 @@ namespace Drupal\field\Entity; +use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldConfigBase; @@ -141,8 +142,8 @@ public function preSave(EntityStorageInterface $storage) { // Filter out unknown settings and make sure all settings are present, so // that a complete field definition is passed to the various hooks and // written to config. - $default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->getType()); - $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings; + $default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->getType(), $this); + $this->settings = NestedArray::mergeDeep($default_settings, array_intersect_key($this->settings, $default_settings)); if ($this->isNew()) { // Notify the entity storage. diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php index d940188..576daff 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php @@ -77,7 +77,7 @@ protected function setUp() { $this->installSchema('system', 'router'); $this->container->get('router.builder')->rebuild(); - entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', array(), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); + entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, NULL, array(), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); // Set up a field, so that the entity that'll be referenced bubbles up a // cache tag when rendering it entirely. diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php index c1179f0..885c339 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php @@ -11,6 +11,8 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Tests\FieldUnitTestBase; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; @@ -27,7 +29,7 @@ class EntityReferenceItemTest extends FieldUnitTestBase { * * @var array */ - public static $modules = array('entity_reference', 'taxonomy', 'text', 'filter'); + public static $modules = array('entity_reference', 'taxonomy', 'text', 'filter', 'views'); /** * The taxonomy vocabulary to test with. @@ -214,4 +216,37 @@ public function testEntitySaveOrder() { $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id()); } + /** + * Tests that the 'handler' selection setting. + */ + public function testSelectionHandlerSettings() { + $field_name = Unicode::strtolower($this->randomMachineName()); + $field_storage = FieldStorageConfig::create(array( + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'type' => 'entity_reference', + 'settings' => array( + 'target_type' => 'entity_test' + ), + )); + $field_storage->save(); + + $field = FieldConfig::create(array( + 'field_storage' => $field_storage, + 'bundle' => 'entity_test', + )); + $field->save(); + + $field = FieldConfig::load($field->id()); + $this->assertTrue($field->getSetting('handler') == 'default:entity_test'); + + $field->settings = [ + 'handler' => 'views', + 'handler_settings' => [], + ]; + $field->save(); + $field = FieldConfig::load($field->id()); + $this->assertTrue($field->getSetting('handler') == 'views'); + } + } diff --git a/core/modules/field/src/Tests/FieldCrudTest.php b/core/modules/field/src/Tests/FieldCrudTest.php index 66d727c..f0689f9 100644 --- a/core/modules/field/src/Tests/FieldCrudTest.php +++ b/core/modules/field/src/Tests/FieldCrudTest.php @@ -99,7 +99,7 @@ function testCreateField() { $this->assertIdentical($config['description'], '', 'Description defaults to empty string.'); // Check that default settings are set. - $this->assertEqual($config['settings'], $field_type_manager->getDefaultFieldSettings($this->fieldStorageDefinition['type']) , 'Default field settings have been written.'); + $this->assertEqual($config['settings'], $field_type_manager->getDefaultFieldSettings($this->fieldStorageDefinition['type'], $field) , 'Default field settings have been written.'); // Check that the denormalized 'field_type' was properly written. $this->assertEqual($config['field_type'], $this->fieldStorageDefinition['type']); diff --git a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php index a27b39c..0404c7f 100644 --- a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php @@ -228,13 +228,10 @@ protected function createReferenceTestEntities($referenced_entity) { 'entity_type' => $entity_type, 'bundle' => $bundle, 'settings' => array( - 'handler' => 'default', 'handler_settings' => array( 'target_bundles' => array( $referenced_entity->bundle() => $referenced_entity->bundle(), ), - 'sort' => array('field' => '_none'), - 'auto_create' => FALSE, ), ), ))->save(); diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php index cf92093..fe3acb4 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php @@ -76,7 +76,7 @@ protected function setUp() { $this->fieldName, 'Field test', $this->referencedEntityType, - 'default', + NULL, array('target_bundles' => array($this->bundle)), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ); @@ -184,7 +184,7 @@ public function testReferencedEntitiesStringId() { $field_name, 'Field test', 'entity_test_string_id', - 'default', + NULL, array('target_bundles' => array($this->bundle)), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ); diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php index b7bf65e..8e71888 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php @@ -74,7 +74,7 @@ protected function assertReferenceable(array $selection_options, $tests, $handle public function testNodeHandler() { $selection_options = array( 'target_type' => 'node', - 'handler' => 'default', + 'handler' => 'default:node', 'handler_settings' => array( 'target_bundles' => array(), ), @@ -200,7 +200,7 @@ public function testNodeHandler() { public function testUserHandler() { $selection_options = array( 'target_type' => 'user', - 'handler' => 'default', + 'handler' => 'default:user', 'handler_settings' => array( 'target_bundles' => array(), 'include_anonymous' => TRUE, @@ -342,7 +342,7 @@ public function testUserHandler() { public function testCommentHandler() { $selection_options = array( 'target_type' => 'comment', - 'handler' => 'default', + 'handler' => 'default:comment', 'handler_settings' => array( 'target_bundles' => array(), ), diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php index 9370dde..6428dfe 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php @@ -98,7 +98,7 @@ public function testSort() { $selection_options = array( 'target_type' => 'node', - 'handler' => 'default', + 'handler' => 'default:node', 'handler_settings' => array( 'target_bundles' => array(), // Add sorting. diff --git a/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php b/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php index fc452ff..1ce6ee4 100644 --- a/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php +++ b/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php @@ -58,7 +58,6 @@ function testSelectionTestVocabularyRestriction() { 'entity_type' => 'entity_test', 'bundle' => 'test_bundle', 'settings' => array( - 'handler' => 'default', 'handler_settings' => array( // Restrict selection of terms to a single vocabulary. 'target_bundles' => array( diff --git a/core/modules/user/config/schema/user.schema.yml b/core/modules/user/config/schema/user.schema.yml index 40bde95..f38fd4c 100644 --- a/core/modules/user/config/schema/user.schema.yml +++ b/core/modules/user/config/schema/user.schema.yml @@ -174,3 +174,23 @@ condition.plugin.user_role: type: sequence sequence: type: string + +entity_reference_selection.default:user: + type: entity_reference_selection + mapping: + filter: + type: mapping + label: 'Filter settings' + mapping: + type: + type: string + label: 'Filter by' + role: + type: sequence + label: 'Restrict to the selected roles' + sequence: + type: string + label: 'Role' + include_anonymous: + type: boolean + label: 'Include the anonymous user in the matched entities.' diff --git a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php index 164006c..57dcf71 100644 --- a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php +++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php @@ -202,4 +202,12 @@ public function entityQueryAlter(SelectInterface $query) { } } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + // @todo add dependency on roles. + return parent::calculateDependencies(); + } + } 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 1451368..027c62f 100644 --- a/core/modules/views/config/schema/views.entity_reference.schema.yml +++ b/core/modules/views/config/schema/views.entity_reference.schema.yml @@ -1,6 +1,6 @@ # Schema for the views entity reference selection plugins. -entity_reference.views.handler_settings: +entity_reference_selection.views: type: mapping label: 'View handler settings' mapping: diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index ae59751..ffba66b 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -7,6 +7,8 @@ namespace Drupal\views\Plugin\EntityReferenceSelection; +use Drupal\Component\Plugin\ConfigurablePluginInterface; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -29,7 +31,7 @@ * weight = 0 * ) */ -class ViewsSelection extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { +class ViewsSelection extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface, ConfigurablePluginInterface { /** * The entity manager. @@ -78,6 +80,7 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container 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->setConfiguration($configuration); $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; $this->currentUser = $current_user; @@ -298,4 +301,40 @@ public static function settingsFormValidate($element, FormStateInterface $form_s $form_state->setValueForElement($element, $value); } + /** + * {@inheritdoc} + */ + public function setConfiguration(array $configuration) { + $this->configuration = NestedArray::mergeDeep( + $this->defaultConfiguration(), + $configuration + ); + } + /** + * Returns default configuration for this plugin. + * + * @return array + * An associative array with the default configuration. + */ + public function defaultConfiguration() { + return [ + 'handler' => $this->getPluginId(), + 'handler_settings' => [], + ]; + } + + /** + * {@inheritdoc} + */ + public function getConfiguration() { + return $this->configuration; + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return []; + } + }