Verified Commit d2080999 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #3391912 by lauriii: Allow field types to override the cardinality container

parent c9eae18a
Loading
Loading
Loading
Loading
Loading
+5 −0
Changes for core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldType/TestItem.php: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -75,6 +75,11 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
   * {@inheritdoc}
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    $form['cardinality_container'][] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => 'Greetings from ' . __METHOD__,
    ];
    $element = [];
    $element['test_field_storage_setting'] = [
      '#type' => 'textfield',
+3 −3
Changes for core/modules/field_ui/src/Form/FieldStorageConfigEditForm.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -94,6 +94,9 @@ public function form(array $form, FormStateInterface $form_state) {
    $field_label = $form_state->get('field_config')->label();
    $form['#prefix'] = '<p>' . $this->t('These settings apply to the %field field everywhere it is used. Some also impact the way that data is stored and cannot be changed once data has been created.', ['%field' => $field_label]) . '</p>';

    // Add the cardinality sub-form.
    $form['cardinality_container'] = $this->getCardinalityForm();

    // Add settings provided by the field module. The field module is
    // responsible for not returning settings that cannot be changed if
    // the field already has data.
@@ -119,9 +122,6 @@ public function form(array $form, FormStateInterface $form_state) {
    $item = $items->first() ?: $items->appendItem();
    $form['settings'] += $item->storageSettingsForm($form, $form_state, $this->entity->hasData());

    // Add the cardinality sub-form.
    $form['cardinality_container'] = $this->getCardinalityForm();

    return $form;
  }

+32 −0
Changes for core/modules/field_ui/tests/src/Functional/ManageFieldsTest.php: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -313,4 +313,36 @@ public function testFieldStorageFormAlter() {
    $this->assertSession()->elementTextContains('css', '#edit-field-storage', 'Greetings from the field_storage_config_edit_form() alter.');
  }

  /**
   * Tests hook_form_field_storage_config_form_edit_alter().
   *
   * @group legacy
   */
  public function testFieldTypeCardinalityAlter() {
    $node_type = $this->drupalCreateContentType();
    $bundle = $node_type->id();

    /** @var \Drupal\field\FieldStorageConfigInterface $storage */
    $storage = $this->container->get('entity_type.manager')
      ->getStorage('field_storage_config')
      ->create([
        'type' => 'test_field',
        'field_name' => 'field_test_field',
        'entity_type' => 'node',
      ]);
    $storage->save();

    $this->container->get('entity_type.manager')
      ->getStorage('field_config')
      ->create([
        'field_storage' => $storage,
        'bundle' => $bundle,
        'entity_type' => 'node',
      ])
      ->save();

    $this->drupalGet("/admin/structure/types/manage/$bundle/fields/node.$bundle.field_test_field");
    $this->assertSession()->elementTextContains('css', '#edit-field-storage', 'Greetings from Drupal\field_test\Plugin\Field\FieldType\TestItem::storageSettingsForm');
  }

}