function MultipleWidgetFormTest::widgetAlterTest

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

Tests widget alter hooks for a given hook name.

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

File

core/modules/field/tests/src/Functional/MultipleWidgetFormTest.php, line 212

Class

MultipleWidgetFormTest
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->assertSession()
        ->pageTextMatchesCount(1, '/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->assertEquals($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.