diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index df2a2fd..8554532 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -248,6 +248,26 @@ field.widget.settings.entity_reference_autocomplete: type: label label: 'Placeholder' +field.widget.settings.machine_name: + type: mapping + label: 'Machine Name' + mapping: + source_field: + type: string + label: 'The source field for the machine name' + disable_on_edit: + type: boolean + label: 'Disable the machine name after initial creation' + exists: + type: string + label: 'A callable to invoke for checking whether a submitted machine name value already exists' + replace_pattern: + type: string + label: 'A regular expression (without delimiters) matching disallowed characters in the machine name' + replace: + type: string + label: 'A character to replace disallowed characters in the machine name' + field.formatter.settings.boolean: type: mapping mapping: diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index faa6d0d..92dc8b4 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -160,8 +160,15 @@ public function buildForm(FieldableEntityInterface $entity, array &$form, FormSt // Set #parents to 'top-level' by default. $form += ['#parents' => []]; + // Sort the components by their weight in order to allow a form element from + // a widget to depend on the processed elements of another form element from + // another widget. For example, the 'machine_name' widget needs the + // processed '#id' property of its source field. + $components = $this->getComponents(); + uasort($components, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']); + // Let each widget generate the form elements. - foreach ($this->getComponents() as $name => $options) { + foreach ($components as $name => $options) { if ($widget = $this->getRenderer($name)) { $items = $entity->get($name); $items->filterEmptyItems(); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/MachineNameWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/MachineNameWidget.php new file mode 100644 index 0000000..c6c5716 --- /dev/null +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/MachineNameWidget.php @@ -0,0 +1,196 @@ +entityFieldManager = $entity_field_manager; + $this->elementInfo = $element_info; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['third_party_settings'], + $container->get('entity_field.manager'), + $container->get('element_info') + ); + } + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return [ + 'source_field' => '', + 'disable_on_edit' => TRUE, + // The 'exists' property is not configurable in the UI but it can be set + // manually in code. + 'exists' => '', + 'replace_pattern' => '[^a-z0-9_]+', + 'replace' => '_', + ] + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ + $form_display = $form_state->getFormObject()->getEntity(); + + $available_fields = $this->entityFieldManager->getFieldDefinitions($form_display->getTargetEntityTypeId(), $form_display->getTargetBundle()); + $displayed_fields = $form_display->getComponents(); + + $options = []; + /**@var \Drupal\Core\Field\FieldDefinitionInterface $field */ + foreach (array_intersect_key($available_fields, $displayed_fields) as $field_name => $field) { + // The source field can only be another string field. + if ($field->getType() === 'string' && $field->getName() !== $this->fieldDefinition->getName()) { + $options[$field_name] = $field->getLabel(); + } + } + $element['source_field'] = [ + '#type' => 'select', + '#title' => $this->t('Source field'), + '#default_value' => $this->getSetting('source_field'), + '#options' => $options, + '#description' => $this->t('The field that should be used as a source for the machine name element. This field needs to be displayed in the entity form before the @field_label field.', ['@field_label' => $this->fieldDefinition->getLabel()]), + ]; + $element['disable_on_edit'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Disable after initial creation'), + '#default_value' => $this->getSetting('disable_on_edit'), + '#description' => $this->t('Disable the machine name after the content has been saved for the first time.'), + ]; + $element['replace_pattern'] = [ + '#type' => 'textfield', + '#title' => $this->t('Replace pattern'), + '#default_value' => $this->getSetting('replace_pattern'), + '#description' => $this->t('A regular expression (without delimiters) matching disallowed characters in the machine name.'), + '#size' => 30, + ]; + $element['replace'] = [ + '#type' => 'textfield', + '#title' => $this->t('Replace character'), + '#default_value' => $this->getSetting('replace'), + '#description' => $this->t("A character to replace disallowed characters in the machine name. When using a different character than '_', Replace pattern needs to be set accordingly."), + '#size' => 1, + '#maxlength' => 1, + ]; + + return $element; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = []; + + $field_definitions = $this->entityFieldManager->getFieldDefinitions($this->fieldDefinition->getTargetEntityTypeId(), $this->fieldDefinition->getTargetBundle()); + if (!empty($this->getSetting('source_field')) && isset($field_definitions[$this->getSetting('source_field')])) { + $summary[] = $this->t('Source field: @source_field', ['@source_field' => $field_definitions[$this->getSetting('source_field')]->getLabel()]); + $summary[] = $this->t('Disable on edit: @disable_on_edit', ['@disable_on_edit' => $this->getSetting('disable_on_edit') ? $this->t('Yes') : $this->t('No')]); + $summary[] = $this->t('Replace pattern: @replace_pattern', ['@replace_pattern' => $this->getSetting('replace_pattern')]); + $summary[] = $this->t('Replace character: @replace', ['@replace' => $this->getSetting('replace')]); + } + else { + $summary[] = $this->t('Missing configuration.'); + } + + return $summary; + } + + /** + * {@inheritdoc} + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + $element_info = $this->elementInfo->getInfo('machine_name'); + $element['value'] = $element + [ + '#type' => 'machine_name', + '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, + '#maxlength' => $this->getFieldSetting('max_length'), + '#source_field' => $this->getSetting('source_field'), + '#process' => array_merge([[get_class($this), 'processMachineNameSource']], $element_info['#process']), + '#machine_name' => [ + 'exists' => $this->getSetting('exists'), + 'label' => $this->fieldDefinition->getLabel(), + 'replace_pattern' => $this->getSetting('replace_pattern'), + 'replace' => $this->getSetting('replace'), + ], + '#disabled' => $this->getSetting('disable_on_edit') && !$items->getEntity()->isNew(), + ]; + + return $element; + } + + /** + * Form API callback: Sets the 'source' property of a machine_name element. + * + * This method is assigned as a #process callback in formElement() method. + */ + public static function processMachineNameSource($element, FormStateInterface $form_state, $form) { + $source_field_state = static::getWidgetState($form['#parents'], $element['#source_field'], $form_state); + + // Hide the field widget if the source field is not configured properly or + // if it doesn't exist in the form. + if (empty($element['#source_field']) || empty($source_field_state['array_parents'])) { + $element['#access'] = FALSE; + } + else { + $source_field_element = NestedArray::getValue($form_state->getCompleteForm(), $source_field_state['array_parents']); + $element['#machine_name']['source'] = $source_field_element[$element['#delta']]['value']['#array_parents']; + } + + return $element; + } + +} diff --git a/core/lib/Drupal/Core/Render/Element/MachineName.php b/core/lib/Drupal/Core/Render/Element/MachineName.php index ace452b..4179207 100644 --- a/core/lib/Drupal/Core/Render/Element/MachineName.php +++ b/core/lib/Drupal/Core/Render/Element/MachineName.php @@ -247,7 +247,7 @@ public static function validateMachineName(&$element, FormStateInterface $form_s } // Verify that the machine name is unique. - if ($element['#default_value'] !== $element['#value']) { + if ($element['#default_value'] !== $element['#value'] && !empty($element['#machine_name']['exists'])) { $function = $element['#machine_name']['exists']; if (call_user_func($function, $element['#value'], $element, $form_state)) { $form_state->setError($element, t('The machine-readable name is already in use. It must be unique.')); diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/Field/Widget/MachineNameWidgetTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/Field/Widget/MachineNameWidgetTest.php new file mode 100644 index 0000000..acc085b --- /dev/null +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/Field/Widget/MachineNameWidgetTest.php @@ -0,0 +1,229 @@ +testFieldSource = FieldStorageConfig::create([ + 'field_name' => 'test_field_source', + 'entity_type' => 'entity_test', + 'type' => 'string', + ]); + $this->testFieldSource->save(); + FieldConfig::create([ + 'field_storage' => $this->testFieldSource, + 'bundle' => 'entity_test', + ])->save(); + + $this->testField = FieldStorageConfig::create([ + 'field_name' => 'test_field', + 'entity_type' => 'entity_test', + 'type' => 'string', + ]); + $this->testField->save(); + FieldConfig::create([ + 'field_storage' => $this->testField, + 'bundle' => 'entity_test', + ])->save(); + + // Create a web user. + $this->drupalLogin($this->drupalCreateUser(['view test entity', 'administer entity_test content', 'administer entity_test form display'])); + } + + /** + * Tests the machine name field widget. + */ + public function testMachineNameWidget() { + $assert_session = $this->assertSession(); + $page = $this->getSession()->getPage(); + + // Go to the entity add form and check that the test fields are not + // displayed because they are not configured in the form display yet. + $this->drupalGet('/entity_test/add'); + $assert_session->fieldNotExists('test_field[0][value]'); + $assert_session->fieldNotExists('test_field_source[0][value]'); + + // Configure the test field to use the machine name widget with no initial + // settings. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->testField->getName(), [ + 'type' => 'machine_name', + 'weight' => 5, + ]) + ->save(); + + // Check that the widget displays an "error" summary when it has missing or + // broken settings. + $this->drupalGet('/entity_test/structure/entity_test/form-display'); + $assert_session->pageTextContains('Missing configuration.'); + + // Check that test field is configured in the form display while the source + // field is not. + $this->assertTrue($assert_session->optionExists('fields[test_field][region]', 'content')->isSelected()); + $this->assertTrue($assert_session->optionExists('fields[test_field_source][region]', 'hidden')->isSelected()); + + // Open the widget settings form and check that a field which is not present + // in the form display can not be selected as a 'source_field'. + $page->pressButton('test_field_settings_edit'); + $assert_session->waitForField('fields[test_field][settings_edit_form][settings][source_field]'); + $assert_session->optionNotExists('fields[test_field][settings_edit_form][settings][source_field]', 'test_field_source'); + + // Go to the entity add form and check that the test field is not displayed, + // even when it is enabled in the form display, because of missing settings. + $this->drupalGet('/entity_test/add'); + $assert_session->fieldNotExists('test_field[0][value]'); + $assert_session->fieldNotExists('test_field_source[0][value]'); + + // Enable the 'source' field in the entity form display and configure the + // test field to use it. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->testFieldSource->getName(), [ + 'type' => 'string_textfield', + 'weight' => 10, + ]) + ->save(); + + // Go to the form display and configure the test field to use the newly + // enabled 'source' field as the machine name source. + $this->drupalGet('/entity_test/structure/entity_test/form-display'); + + $page->pressButton('test_field_settings_edit'); + $assert_session->waitForField('fields[test_field][settings_edit_form][settings][source_field]'); + $page->selectFieldOption('fields[test_field][settings_edit_form][settings][source_field]', 'test_field_source'); + $page->pressButton('test_field_plugin_settings_update'); + $assert_session->assertWaitOnAjaxRequest(); + + $assert_session->pageTextContains('Source field: test_field_source'); + $assert_session->pageTextContains('Replace pattern: [^a-z0-9_]+'); + $assert_session->pageTextContains('Replace character: _'); + + $this->submitForm([], 'Save'); + $assert_session->pageTextContains('Your settings have been saved.'); + + // Go to the entity add form and check that the test field is still not + // displayed, even when it has a source field configured, because the weight + // of the source field in the form display is higher than the weight of the + // test field. + $this->drupalGet('/entity_test/add'); + $assert_session->fieldNotExists('test_field[0][value]'); + $assert_session->fieldExists('test_field_source[0][value]'); + + // Configure the source field to have a lower weight than the test field, + // which will finally make it appear in the entity form. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->testFieldSource->getName(), [ + 'type' => 'string_textfield', + 'weight' => 4, + ]) + ->save(); + + $this->drupalGet('/entity_test/add'); + $test_source_field = $page->findField('test_field_source[0][value]'); + $test_field = $page->findField('test_field[0][value]'); + $test_field_machine_name_value = $page->find('css', '#edit-test-field-source-0-value-machine-name-suffix .machine-name-value'); + $this->assertNotEmpty($test_source_field); + $this->assertNotEmpty($test_field); + $this->assertNotEmpty($test_field_machine_name_value, 'Test field with the machine name widget has been initialized.'); + + $test_values = [ + 'input' => 'Test value !0-9@', + 'expected' => 'test_value_0_9_', + ]; + $test_source_field->setValue($test_values['input']); + + // Wait the set timeout for fetching the machine name. + $this->assertJsCondition('jQuery("#edit-test-field-source-0-value-machine-name-suffix .machine-name-value").html() == "' . $test_values['expected'] . '"'); + + // Validate the generated machine name. + $this->assertEquals($test_values['expected'], $test_field_machine_name_value->getHtml()); + + // Submit the entity form. + $this->submitForm([], 'Save'); + + // Load the entity and check that machine name value that was saved is + // correct. + $entity = EntityTest::load(1); + $this->assertSame($test_values['input'], $entity->test_field_source->value); + $this->assertSame($test_values['expected'], $entity->test_field->value); + + // Try changing the 'replace_pattern' and 'replace' settings of the widget. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->testField->getName(), [ + 'type' => 'machine_name', + 'weight' => 5, + 'settings' => [ + 'source_field' => $this->testFieldSource->getName(), + 'replace_pattern' => '[^a-z0-9-]+', + 'replace' => '-', + ], + ]) + ->save(); + + $this->drupalGet('/entity_test/add'); + $test_source_field = $page->findField('test_field_source[0][value]'); + $test_field_machine_name_value = $page->find('css', '#edit-test-field-source-0-value-machine-name-suffix .machine-name-value'); + + $test_values = [ + 'input' => 'Test value2 !0-9@', + 'expected' => 'test-value2-0-9-', + ]; + $test_source_field->setValue($test_values['input']); + + // Wait the set timeout for fetching the machine name. + $this->assertJsCondition('jQuery("#edit-test-field-source-0-value-machine-name-suffix .machine-name-value").html() == "' . $test_values['expected'] . '"'); + + // Validate the generated machine name. + $this->assertEquals($test_values['expected'], $test_field_machine_name_value->getHtml()); + + // Submit the entity form. + $this->submitForm([], 'Save'); + + // Load the entity and check that machine name value that was saved is + // correct. + $entity = EntityTest::load(2); + $this->assertSame($test_values['input'], $entity->test_field_source->value); + $this->assertSame($test_values['expected'], $entity->test_field->value); + } + +} diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php index bf2643d..bf46bb7 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php @@ -5,9 +5,9 @@ use Drupal\FunctionalJavascriptTests\JavascriptTestBase; /** - * Tests for the machine name field. + * Tests for the machine name form element. * - * @group field + * @group Form */ class MachineNameTest extends JavascriptTestBase {