function FieldTestHooks::alterWidget

Same name and namespace in other branches
  1. 11.x core/modules/field/tests/modules/field_test/src/Hook/FieldTestHooks.php \Drupal\field_test\Hook\FieldTestHooks::alterWidget()

Sets up alterations for widget alter tests.

See also

\Drupal\field\Tests\FormTest::widgetAlterTest()

3 calls to FieldTestHooks::alterWidget()
FieldTestHooks::fieldWidgetCompleteFormAlter in core/modules/field/tests/modules/field_test/src/Hook/FieldTestHooks.php
Implements hook_field_widget_complete_form_alter().
FieldTestHooks::fieldWidgetCompleteTestFieldWidgetMultipleFormAlter in core/modules/field/tests/modules/field_test/src/Hook/FieldTestHooks.php
Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
FieldTestHooks::fieldWidgetCompleteTestFieldWidgetMultipleSingleValueFormAlter in core/modules/field/tests/modules/field_test/src/Hook/FieldTestHooks.php
Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().

File

core/modules/field/tests/modules/field_test/src/Hook/FieldTestHooks.php, line 213

Class

FieldTestHooks
Hook implementations for field_test.

Namespace

Drupal\field_test\Hook

Code

public function alterWidget($hook, array &$field_widget_complete_form, FormStateInterface $form_state, array $context) : void {
  $elements =& $field_widget_complete_form['widget'];
  // Set a message if this is for the form displayed to set default value for
  // the field.
  if ($context['default']) {
    \Drupal::messenger()->addStatus("From {$hook}(): Default form is true.");
  }
  $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.";
    }
  }
}

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