summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjibran2017-07-27 10:12:32 (GMT)
committerJibran Ijaz2017-07-27 10:12:32 (GMT)
commit4225e988c8b9b73d40563c9c75d10f17ae776841 (patch)
treeb9a7401bb749576bffe8b2cfefc301de90174bc9
parentb5e4fc2bba55fc16c853172d162802f5542c90c1 (diff)
Issue #2881996 by jibran, dpi: Select at least one entity type ID on field storage setting form8.x-2.0-alpha5
-rw-r--r--src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php23
-rw-r--r--tests/src/Functional/DynamicEntityReferenceTest.php104
2 files changed, 67 insertions, 60 deletions
diff --git a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
index b26a0f1..d78f240 100644
--- a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
+++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
@@ -191,12 +191,35 @@ class DynamicEntityReferenceItem extends EntityReferenceItem {
'#default_value' => $this->getSetting('entity_type_ids'),
'#disabled' => $has_data,
'#multiple' => TRUE,
+ '#element_validate' => [
+ [DynamicEntityReferenceItem::class, 'storageSettingsFormValidate'],
+ ],
];
return $element;
}
/**
+ * Form element validation for storage settings.
+ *
+ * @param array $element
+ * The form element .
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The form state.
+ * @param array $form
+ * The complete form.
+ */
+ public static function storageSettingsFormValidate(array &$element, FormStateInterface $form_state, array $form) {
+ $labels = \Drupal::service('entity_type.repository')->getEntityTypeLabels(TRUE);
+ $exclude_entity_types = $form_state->getValue(['settings', 'exclude_entity_types'], 0);
+ $entity_type_ids = $form_state->getValue(['settings', 'entity_type_ids'], []);
+ $diff = array_diff(array_keys($labels[(string) t('Content', [], ['context' => 'Entity type group'])]), $entity_type_ids);
+ if ((!$exclude_entity_types && empty($entity_type_ids)) || ($exclude_entity_types && empty($diff))) {
+ $form_state->setError($element, t('Select at least one entity type ID.'));
+ }
+ }
+
+ /**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
diff --git a/tests/src/Functional/DynamicEntityReferenceTest.php b/tests/src/Functional/DynamicEntityReferenceTest.php
index 45ca353..6d23073 100644
--- a/tests/src/Functional/DynamicEntityReferenceTest.php
+++ b/tests/src/Functional/DynamicEntityReferenceTest.php
@@ -104,36 +104,23 @@ class DynamicEntityReferenceTest extends BrowserTestBase {
foreach (array_keys($labels[(string) t('Configuration')]) as $entity_type) {
$assert_session->fieldNotExists('settings[' . $entity_type . '][handler]');
}
-
- $edit = [
- 'settings[entity_test_view_builder][handler_settings][target_bundles][entity_test_view_builder]' => TRUE,
- 'settings[entity_test_multivalue_basefield][handler_settings][target_bundles][entity_test_multivalue_basefield]' => TRUE,
- 'settings[entity_test_no_id][handler_settings][target_bundles][entity_test_no_id]' => TRUE,
- 'settings[entity_test_no_label][handler_settings][target_bundles][entity_test_no_label]' => TRUE,
- 'settings[entity_test_label_callback][handler_settings][target_bundles][entity_test_label_callback]' => TRUE,
- 'settings[entity_test][handler_settings][target_bundles][entity_test]' => TRUE,
- 'settings[entity_test_admin_routes][handler_settings][target_bundles][entity_test_admin_routes]' => TRUE,
- 'settings[entity_test_base_field_display][handler_settings][target_bundles][entity_test_base_field_display]' => TRUE,
- 'settings[entity_test_mul][handler_settings][target_bundles][entity_test_mul]' => TRUE,
- 'settings[entity_test_mul_changed][handler_settings][target_bundles][entity_test_mul_changed]' => TRUE,
- 'settings[entity_test_mul_default_value][handler_settings][target_bundles][entity_test_mul_default_value]' => TRUE,
- 'settings[entity_test_mul_langcode_key][handler_settings][target_bundles][entity_test_mul_langcode_key]' => TRUE,
- 'settings[entity_test_rev][handler_settings][target_bundles][entity_test_rev]' => TRUE,
- 'settings[entity_test_mulrev_changed][handler_settings][target_bundles][entity_test_mulrev_changed]' => TRUE,
- 'settings[entity_test_mulrev][handler_settings][target_bundles][entity_test_mulrev]' => TRUE,
- 'settings[entity_test_constraints][handler_settings][target_bundles][entity_test_constraints]' => TRUE,
- 'settings[entity_test_composite_constraint][handler_settings][target_bundles][entity_test_composite_constraint]' => TRUE,
- 'settings[entity_test_constraint_violation][handler_settings][target_bundles][entity_test_constraint_violation]' => TRUE,
- 'settings[entity_test_field_override][handler_settings][target_bundles][entity_test_field_override]' => TRUE,
- 'settings[entity_test_default_value][handler_settings][target_bundles][entity_test_default_value]' => TRUE,
- 'settings[entity_test_with_bundle][handler_settings][target_bundles][test]' => TRUE,
- 'settings[entity_test_default_access][handler_settings][target_bundles][entity_test_default_access]' => TRUE,
- 'settings[entity_test_cache][handler_settings][target_bundles][entity_test_cache]' => TRUE,
- 'settings[entity_test_string_id][handler_settings][target_bundles][entity_test_string_id]' => TRUE,
- 'settings[entity_test_field_methods][handler_settings][target_bundles][entity_test_field_methods]' => TRUE,
- 'settings[entity_test_mulrevpub][handler_settings][target_bundles][entity_test_mulrevpub]' => TRUE,
- 'settings[entity_test_mulrev_chnged_revlog][handler_settings][target_bundles][entity_test_mulrev_chnged_revlog]' => TRUE,
+ $edit = [];
+ $excluded_entity_type_ids = [
+ 'user',
+ 'file',
+ 'entity_test_label',
+ 'entity_test_no_bundle',
];
+ foreach ($labels[(string) t('Content', [], ['context' => 'Entity type group'])] as $entity_type_id => $entity_type_label) {
+ if (!in_array($entity_type_id, $excluded_entity_type_ids)) {
+ if ($entity_type_id !== 'entity_test_with_bundle') {
+ $edit["settings[$entity_type_id][handler_settings][target_bundles][$entity_type_id]"] = TRUE;
+ }
+ else {
+ $edit['settings[entity_test_with_bundle][handler_settings][target_bundles][test]'] = TRUE;
+ }
+ }
+ }
$this->submitForm($edit, t('Save settings'));
$assert_session->responseContains(t('Saved %name configuration', ['%name' => 'Foobar']));
$excluded_entity_type_ids = FieldStorageConfig::loadByName('entity_test', 'field_foobar')
@@ -145,6 +132,18 @@ class DynamicEntityReferenceTest extends BrowserTestBase {
$this->submitForm([
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings[exclude_entity_types]' => FALSE,
+ 'settings[entity_type_ids][]' => [],
+ ], t('Save field settings'));
+ $assert_session->pageTextContains('Select at least one entity type ID.');
+ $this->submitForm([
+ 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
+ 'settings[exclude_entity_types]' => TRUE,
+ 'settings[entity_type_ids][]' => array_keys($labels[(string) t('Content', [], ['context' => 'Entity type group'])]),
+ ], t('Save field settings'));
+ $assert_session->pageTextContains('Select at least one entity type ID.');
+ $this->submitForm([
+ 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
+ 'settings[exclude_entity_types]' => FALSE,
'settings[entity_type_ids][]' => ['user', 'entity_test_label'],
], t('Save field settings'));
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar');
@@ -194,38 +193,23 @@ class DynamicEntityReferenceTest extends BrowserTestBase {
$this->submitForm([
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
], t('Save field settings'));
-
- $edit = [
- 'settings[entity_test_label][handler_settings][target_bundles][entity_test_label]' => TRUE,
- 'settings[entity_test_view_builder][handler_settings][target_bundles][entity_test_view_builder]' => TRUE,
- 'settings[entity_test_multivalue_basefield][handler_settings][target_bundles][entity_test_multivalue_basefield]' => TRUE,
- 'settings[entity_test_no_id][handler_settings][target_bundles][entity_test_no_id]' => TRUE,
- 'settings[entity_test_no_label][handler_settings][target_bundles][entity_test_no_label]' => TRUE,
- 'settings[entity_test_label_callback][handler_settings][target_bundles][entity_test_label_callback]' => TRUE,
- 'settings[entity_test][handler_settings][target_bundles][entity_test]' => TRUE,
- 'settings[entity_test_admin_routes][handler_settings][target_bundles][entity_test_admin_routes]' => TRUE,
- 'settings[entity_test_base_field_display][handler_settings][target_bundles][entity_test_base_field_display]' => TRUE,
- 'settings[entity_test_mul][handler_settings][target_bundles][entity_test_mul]' => TRUE,
- 'settings[entity_test_mul_changed][handler_settings][target_bundles][entity_test_mul_changed]' => TRUE,
- 'settings[entity_test_mul_default_value][handler_settings][target_bundles][entity_test_mul_default_value]' => TRUE,
- 'settings[entity_test_mul_langcode_key][handler_settings][target_bundles][entity_test_mul_langcode_key]' => TRUE,
- 'settings[entity_test_rev][handler_settings][target_bundles][entity_test_rev]' => TRUE,
- 'settings[entity_test_mulrev_changed][handler_settings][target_bundles][entity_test_mulrev_changed]' => TRUE,
- 'settings[entity_test_mulrev][handler_settings][target_bundles][entity_test_mulrev]' => TRUE,
- 'settings[entity_test_constraints][handler_settings][target_bundles][entity_test_constraints]' => TRUE,
- 'settings[entity_test_composite_constraint][handler_settings][target_bundles][entity_test_composite_constraint]' => TRUE,
- 'settings[entity_test_constraint_violation][handler_settings][target_bundles][entity_test_constraint_violation]' => TRUE,
- 'settings[entity_test_field_override][handler_settings][target_bundles][entity_test_field_override]' => TRUE,
- 'settings[entity_test_default_value][handler_settings][target_bundles][entity_test_default_value]' => TRUE,
- 'settings[entity_test_with_bundle][handler_settings][target_bundles][test]' => TRUE,
- 'settings[entity_test_default_access][handler_settings][target_bundles][entity_test_default_access]' => TRUE,
- 'settings[entity_test_cache][handler_settings][target_bundles][entity_test_cache]' => TRUE,
- 'settings[entity_test_string_id][handler_settings][target_bundles][entity_test_string_id]' => TRUE,
- 'settings[entity_test_field_methods][handler_settings][target_bundles][entity_test_field_methods]' => TRUE,
- 'settings[entity_test_mulrevpub][handler_settings][target_bundles][entity_test_mulrevpub]' => TRUE,
- 'settings[entity_test_mulrev_chnged_revlog][handler_settings][target_bundles][entity_test_mulrev_chnged_revlog]' => TRUE,
-
+ $labels = $this->container->get('entity_type.repository')->getEntityTypeLabels(TRUE);
+ $edit = [];
+ $excluded_entity_type_ids = [
+ 'user',
+ 'file',
+ 'entity_test_no_bundle',
];
+ foreach ($labels[(string) t('Content', [], ['context' => 'Entity type group'])] as $entity_type_id => $entity_type_label) {
+ if (!in_array($entity_type_id, $excluded_entity_type_ids)) {
+ if ($entity_type_id !== 'entity_test_with_bundle') {
+ $edit["settings[$entity_type_id][handler_settings][target_bundles][$entity_type_id]"] = TRUE;
+ }
+ else {
+ $edit['settings[entity_test_with_bundle][handler_settings][target_bundles][test]'] = TRUE;
+ }
+ }
+ }
$this->submitForm($edit, t('Save settings'));
$assert_session->responseContains(t('Saved %name configuration', ['%name' => 'Foobar']));
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();