summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjibran2017-07-27 10:13:27 (GMT)
committerJibran Ijaz2017-07-27 10:13:27 (GMT)
commita65c8210fac486c586d38cb63a3efee003ecae59 (patch)
tree999eecc01cc93c08235965d6235022eb2b948e0e
parent561dbcb304e87c02cbba5eaef81699afab514aaa (diff)
Issue #2881996 by jibran, dpi: Select at least one entity type ID on field storage setting form8.x-1.4
-rw-r--r--src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php26
-rw-r--r--tests/src/Functional/DynamicEntityReferenceTest.php111
2 files changed, 81 insertions, 56 deletions
diff --git a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
index 39bd0c2..d6b51ad 100644
--- a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
+++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
@@ -196,12 +196,38 @@ 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);
+ $options = array_filter(array_keys($labels[(string) t('Content', [], ['context' => 'Entity type group'])]), function ($entity_type_id) {
+ return static::entityHasIntegerId($entity_type_id);
+ });
+ $exclude_entity_types = $form_state->getValue(['settings', 'exclude_entity_types'], 0);
+ $entity_type_ids = $form_state->getValue(['settings', 'entity_type_ids'], []);
+ $diff = array_diff($options, $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 1761635..7e26fc4 100644
--- a/tests/src/Functional/DynamicEntityReferenceTest.php
+++ b/tests/src/Functional/DynamicEntityReferenceTest.php
@@ -8,6 +8,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
+use Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\field\Entity\FieldConfig;
@@ -99,35 +100,27 @@ class DynamicEntityReferenceTest extends BrowserTestBase {
$assert_session->fieldExists('default_value_input[field_foobar][0][target_type]');
$assert_session->optionExists('default_value_input[field_foobar][0][target_type]', 'entity_test');
$assert_session->optionNotExists('default_value_input[field_foobar][0][target_type]', 'user');
- $assert_session->fieldNotExists('settings[entity_test_no_id][handler_settings][target_bundles][entity_test_no_id]');
- $assert_session->fieldNotExists('settings[entity_test_string_id][handler_settings][target_bundles][entity_test_string_id]');
- $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_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_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_label',
+ 'entity_test_no_id',
+ 'entity_test_no_bundle',
+ 'entity_test_string_id',
];
+ 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')
@@ -139,6 +132,22 @@ 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.');
+ $options = array_filter(array_keys($labels[(string) t('Content', [], ['context' => 'Entity type group'])]), function ($entity_type_id) {
+ return DynamicEntityReferenceItem::entityHasIntegerId($entity_type_id);
+ });
+ unset($options['entity_test_no_id']);
+ $this->submitForm([
+ 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
+ 'settings[exclude_entity_types]' => TRUE,
+ 'settings[entity_type_ids][]' => $options,
+ ], 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');
@@ -192,35 +201,25 @@ class DynamicEntityReferenceTest extends BrowserTestBase {
], t('Save field settings'));
$assert_session->fieldNotExists('settings[entity_test_no_id][handler_settings][target_bundles][entity_test_no_id]');
$assert_session->fieldNotExists('settings[entity_test_string_id][handler_settings][target_bundles][entity_test_string_id]');
- $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_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_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_id',
+ 'entity_test_no_bundle',
+ 'entity_test_string_id',
];
+ 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();