diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index ab6b944..2690ffa 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -769,6 +769,7 @@ entity_reference_selection: target_bundles: type: sequence label: 'types' + nullable: true sequence: type: string label: 'Type' diff --git a/core/lib/Drupal/Core/Config/Schema/ArrayElement.php b/core/lib/Drupal/Core/Config/Schema/ArrayElement.php index 88914c0..fb89063 100644 --- a/core/lib/Drupal/Core/Config/Schema/ArrayElement.php +++ b/core/lib/Drupal/Core/Config/Schema/ArrayElement.php @@ -186,4 +186,14 @@ public function setTypedConfig(TypedConfigManagerInterface $typed_config) { $this->typedConfig = $typed_config; } + /** + * Determines if this element allows NULL as a value. + * + * @return bool + * TRUE if NULL is a valid value, FALSE otherwise. + */ + public function isNullable() { + return isset($this->definition['nullable']) && $this->definition['nullable'] == TRUE; + } + } diff --git a/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php b/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php index e1ce8dc..2193a55 100644 --- a/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php +++ b/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php @@ -106,9 +106,13 @@ protected function checkValue($key, $value) { (($type == 'double' || $type == 'integer') && $element instanceof FloatInterface) || ($type == 'boolean' && $element instanceof BooleanInterface) || ($type == 'string' && $element instanceof StringInterface) || - // Null values are allowed for all types. + // Null values are allowed for all primitive types. ($value === NULL); } + // Array elements can also opt-in for allowing a NULL value. + elseif ($element instanceof ArrayElement && $element->isNullable() && $value === NULL) { + $success = TRUE; + } $class = get_class($element); if (!$success) { return array($error_key => "variable type is $type but applied schema class is $class"); diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index c851f14..3f8bccd 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -111,6 +111,9 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta // 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', @@ -128,7 +131,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#type' => 'checkboxes', '#title' => $this->t('Bundles'), '#options' => $bundle_options, - '#default_value' => $selection_handler_settings['target_bundles'], + '#default_value' => (array) $selection_handler_settings['target_bundles'], '#required' => TRUE, '#size' => 6, '#multiple' => TRUE, @@ -336,7 +339,7 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') // If the 'target_bundles' setting is an empty array, force the query to // never return anything and bail out early. - if ($handler_settings['target_bundles'] === []) { + if (isset($handler_settings['target_bundles']) && $handler_settings['target_bundles'] === []) { $query->condition($entity_type->getKey('id'), NULL, '='); return $query; diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php index 274df65..46cc952 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php @@ -77,7 +77,7 @@ public function testNodeHandler() { 'target_type' => 'node', 'handler' => 'default', 'handler_settings' => array( - 'target_bundles' => array(), + 'target_bundles' => NULL, ), ); @@ -203,7 +203,7 @@ public function testUserHandler() { 'target_type' => 'user', 'handler' => 'default', 'handler_settings' => array( - 'target_bundles' => array(), + 'target_bundles' => NULL, 'include_anonymous' => TRUE, ), ); @@ -345,7 +345,7 @@ public function testCommentHandler() { 'target_type' => 'comment', 'handler' => 'default', 'handler_settings' => array( - 'target_bundles' => array(), + 'target_bundles' => NULL, ), ); diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php index 98be261..3d0d4d1 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php @@ -100,7 +100,7 @@ public function testSort() { 'target_type' => 'node', 'handler' => 'default', 'handler_settings' => array( - 'target_bundles' => array(), + 'target_bundles' => NULL, // Add sorting. 'sort' => array( 'field' => 'field_text.value',