function FormTest::widgetAlterTest

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::widgetAlterTest()

Tests widget alter hooks for a given hook name.

4 calls to FormTest::widgetAlterTest()
FormTest::testFieldFormMultipleWidgetAlter in core/modules/field/tests/src/Functional/FormTest.php
Tests hook_field_widget_multivalue_form_alter().
FormTest::testFieldFormMultipleWidgetAlterSingleValues in core/modules/field/tests/src/Functional/FormTest.php
Tests hook_field_widget_multivalue_form_alter() with single value elements.
FormTest::testFieldFormMultipleWidgetTypeAlter in core/modules/field/tests/src/Functional/FormTest.php
Tests hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
FormTest::testFieldFormMultipleWidgetTypeAlterSingleValues in core/modules/field/tests/src/Functional/FormTest.php
Tests hook_field_widget_multivalue_WIDGET_TYPE_form_alter() with single value elements.

File

core/modules/field/tests/src/Functional/FormTest.php, line 710

Class

FormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

protected function widgetAlterTest($hook, $widget) {
    // Create a field with fixed cardinality, configure the form to use a
    // "multiple" widget.
    $field_storage = $this->fieldStorageMultiple;
    $field_name = $field_storage['field_name'];
    $this->field['field_name'] = $field_name;
    FieldStorageConfig::create($field_storage)->save();
    FieldConfig::create($this->field)
        ->save();
    // Set a flag in state so that the hook implementations will run.
    \Drupal::state()->set("field_test.widget_alter_test", [
        'hook' => $hook,
        'field_name' => $field_name,
        'widget' => $widget,
    ]);
    \Drupal::service('entity_display.repository')->getFormDisplay($this->field['entity_type'], $this->field['bundle'], 'default')
        ->setComponent($field_name, [
        'type' => $widget,
    ])
        ->save();
    // We need to rebuild hook information after setting the component through
    // the API.
    $this->rebuildAll();
    $this->drupalGet('entity_test/add');
    $this->assertUniqueText("From {$hook}(): prefix on {$field_name} parent element.");
    if ($widget === 'test_field_widget_multiple_single_value') {
        $suffix_text = "From {$hook}(): suffix on {$field_name} child element.";
        $this->assertEqual($field_storage['cardinality'], substr_count($this->getTextContent(), $suffix_text), "'{$suffix_text}' was found {$field_storage['cardinality']} times  using widget {$widget}");
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.