Unverified Commit 4225e988 authored by jibran's avatar jibran Committed by jibran

Issue #2881996 by jibran, dpi: Select at least one entity type ID on field storage setting form

parent b5e4fc2b
......@@ -191,11 +191,34 @@ 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}
*/
......
......@@ -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')
......@@ -142,6 +129,18 @@ class DynamicEntityReferenceTest extends BrowserTestBase {
$this->assertSame(array_keys($excluded_entity_type_ids), ['user', 'entity_test_label']);
// Check the include entity settings.
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar/storage');
$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,
......@@ -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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment