Commit 60b8de9e authored by alexpott's avatar alexpott

Issue #2943035 by tedbow, xjm, tim.plunkett:...

Issue #2943035 by tedbow, xjm, tim.plunkett: hook_field_widget_multivalue_form_alter and hook_field_widget_multivalue_WIDGET_TYPE_alter are not invoked for single widget that handle multiple values
parent 678a4c80
......@@ -104,6 +104,19 @@ public function form(FieldItemListInterface $items, array &$form, FormStateInter
$elements = $this->formMultipleElements($items, $form, $form_state);
}
// Allow modules to alter the field multi-value widget form element.
// This hook can also be used for single-value fields.
$context = [
'form' => $form,
'widget' => $this,
'items' => $items,
'default' => $this->isDefaultValueWidget($form_state),
];
\Drupal::moduleHandler()->alter([
'field_widget_multivalue_form',
'field_widget_multivalue_' . $this->getPluginId() . '_form',
], $elements, $form_state, $context);
// Populate the 'array_parents' information in $form_state->get('field')
// after the form is built, so that we catch changes in the form structure
// performed in alter() hooks.
......@@ -241,19 +254,6 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f
],
];
}
// Allow modules to alter the field multi-value widget form element.
// This hook can also be used for single-value fields.
$context = [
'form' => $form,
'widget' => $this,
'items' => $items,
'default' => $this->isDefaultValueWidget($form_state),
];
\Drupal::moduleHandler()->alter([
'field_widget_multivalue_form',
'field_widget_multivalue_' . $this->getPluginId() . '_form',
], $elements, $form_state, $context);
}
return $elements;
......
......@@ -641,23 +641,34 @@ public function testLabelOnMultiValueFields() {
* Tests hook_field_widget_multivalue_form_alter().
*/
public function testFieldFormMultipleWidgetAlter() {
$this->widgetAlterTest('hook_field_widget_multivalue_form_alter');
$this->widgetAlterTest('hook_field_widget_multivalue_form_alter', 'test_field_widget_multiple');
}
/**
* Tests hook_field_widget_multivalue_form_alter() with single value elements.
*/
public function testFieldFormMultipleWidgetAlterSingleValues() {
$this->widgetAlterTest('hook_field_widget_multivalue_form_alter', 'test_field_widget_multiple_single_value');
}
/**
* Tests hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
*/
public function testFieldFormMultipleWidgetTypeAlter() {
$this->widgetAlterTest('hook_field_widget_multivalue_WIDGET_TYPE_form_alter');
$this->widgetAlterTest('hook_field_widget_multivalue_WIDGET_TYPE_form_alter', 'test_field_widget_multiple');
}
/**
* Tests widget alter hooks for a given hook name.
* Tests hook_field_widget_multivalue_WIDGET_TYPE_form_alter() with single value elements.
*/
protected function widgetAlterTest($hook) {
// Set a flag in state so that the hook implementations will run.
\Drupal::state()->set("field_test.$hook", TRUE);
public function testFieldFormMultipleWidgetTypeAlterSingleValues() {
$this->widgetAlterTest('hook_field_widget_multivalue_WIDGET_TYPE_form_alter', 'test_field_widget_multiple_single_value');
}
/**
* Tests widget alter hooks for a given hook name.
*/
protected function widgetAlterTest($hook, $widget) {
// Create a field with fixed cardinality, configure the form to use a
// "multiple" widget.
$field_storage = $this->fieldStorageMultiple;
......@@ -665,16 +676,25 @@ protected function widgetAlterTest($hook) {
$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,
]);
entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
->setComponent($field_name, [
'type' => 'test_field_widget_multiple',
'type' => $widget,
])
->save();
$this->drupalGet('entity_test/add');
$this->assertUniqueText("From $hook(): prefix on field_test_text parent element.");
$this->assertText("From $hook(): description on field_test_text child element.");
$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");
}
}
}
......@@ -55,6 +55,14 @@ field.widget.settings.test_field_widget_multiple:
type: string
label: 'Test setting'
field.widget.settings.test_field_widget_multiple_single_value:
type: mapping
label: 'Test multiple field widget settings: single values'
mapping:
test_widget_setting_multiple:
type: string
label: 'Test setting'
field.widget.third_party.color:
type: mapping
label: 'Field test entity display color module third party settings'
......
......@@ -19,6 +19,13 @@
function field_test_field_widget_info_alter(&$info) {
$info['test_field_widget_multiple']['field_types'][] = 'test_field';
$info['test_field_widget_multiple']['field_types'][] = 'test_field_with_preconfigured_options';
// Add extra widget when needed for tests.
// @see \Drupal\field\Tests\FormTest::widgetAlterTest().
if ($alter_info = \Drupal::state()->get("field_test.widget_alter_test")) {
if ($alter_info['widget'] === 'test_field_widget_multiple_single_value') {
$info['test_field_widget_multiple_single_value']['field_types'][] = 'test_field';
}
}
}
/**
......
......@@ -116,12 +116,20 @@ function field_test_field_widget_multivalue_form_alter(array &$elements, FormSta
}
/**
* Implements hook_field_widget_multivalue_form_alter().
* Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
*/
function field_test_field_widget_multivalue_text_textfield_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
function field_test_field_widget_multivalue_test_field_widget_multiple_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
_field_test_alter_widget("hook_field_widget_multivalue_WIDGET_TYPE_form_alter", $elements, $form_state, $context);
}
/**
* Implements hook_field_widget_multivalue_WIDGET_TYPE_form_alter().
*/
function field_test_field_widget_multivalue_test_field_widget_multiple_single_value_form_alter(array &$elements, FormStateInterface $form_state, array $context) {
_field_test_alter_widget("hook_field_widget_multivalue_WIDGET_TYPE_form_alter", $elements, $form_state, $context);
}
/**
* Sets up alterations for widget alter tests.
*
......@@ -134,13 +142,12 @@ function _field_test_alter_widget($hook, array &$elements, FormStateInterface $f
if ($context['default']) {
drupal_set_message("From $hook(): Default form is true.");
}
if (\Drupal::state()->get("field_test.$hook")) {
$name = $context['items']->getFieldDefinition()->getName();
if ($name === 'field_test_text') {
$elements['#prefix'] = "From $hook(): prefix on $name parent element.";
foreach (Element::children($elements) as $delta => $element) {
$elements[$delta]['#description'] = "From $hook(): description on $name child element.";
}
$alter_info = \Drupal::state()->get("field_test.widget_alter_test");
$name = $context['items']->getFieldDefinition()->getName();
if (!empty($alter_info) && $hook === $alter_info['hook'] && $name === $alter_info['field_name']) {
$elements['#prefix'] = "From $hook(): prefix on $name parent element.";
foreach (Element::children($elements) as $delta => $element) {
$elements[$delta]['#suffix'] = "From $hook(): suffix on $name child element.";
}
}
}
......
<?php
namespace Drupal\field_test\Plugin\Field\FieldWidget;
/**
* Plugin implementation of the 'test_field_widget_multiple' widget.
*
* The 'field_types' entry is left empty, and is populated through
* hook_field_widget_info_alter().
*
* @see field_test_field_widget_info_alter()
*
* @FieldWidget(
* id = "test_field_widget_multiple_single_value",
* label = @Translation("Test widget - multiple - single value"),
* multiple_values = FALSE,
* weight = 10
* )
*/
class TestFieldWidgetMultipleSingleValues extends TestFieldWidgetMultiple {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment