protected function FormTest::widgetAlterTest in Drupal 10
Same name and namespace in other branches
- 8 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::widgetAlterTest()
- 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::widgetAlterTest()
Tests widget alter hooks for a given hook name.
File
- core/
modules/ field/ tests/ src/ Functional/ FormTest.php, line 720
Class
- FormTest
- Tests field form handling.
Namespace
Drupal\Tests\field\FunctionalCode
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}");
}
}