You are here

function hook_webform_element_alter in Webform 8.5

Same name and namespace in other branches
  1. 6.x webform.api.php \hook_webform_element_alter()

Alter webform elements.

Parameters

array $element: Webform specific element properties include:

  • #webform: The element's parent webform ID.
  • #webform_submission: The element's related webform submission ID.
  • #webform_id: The element's unique webform key.
  • #webform_key: The element's webform key/name.
  • #webform_parent_key: The element's parent key/name.
  • #webform_parent_flexbox: TRUE if the element's parent is a flexbox container.
  • #webform_depth: The depth level of the element in the form's tree hierarchy.
  • #webform_children: An array of child element keys/names.
  • #webform_multiple: TRUE if element stores multiple values.
  • #webform_composite: TRUE if element stores composite values.
  • #webform_parents: An array containing the element's parent keys/names.

Webform specific composite sub-element properties include:

  • #webform_composite_id: The composite sub-element's ID.
  • #webform_composite_key: The composite sub-element's parent key and element key.
  • #webform_composite_parent_key: The composite sub-element's parent key.

Sub-element's can have properties defined using #SUB_ELEMENT__PROPERTY. For example, an other element's placeholder can be defined using the #other__placeholder property.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $context: An associative array containing the following key-value pairs:

  • form: The form structure to which elements is being attached.

See also

\Drupal\webform\WebformSubmissionForm::prepareElements()

hook_webform_element_ELEMENT_TYPE_alter()

5 functions implement hook_webform_element_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

webform_bootstrap_test_module_webform_element_alter in modules/webform_bootstrap/tests/modules/webform_bootstrap_test_module/webform_bootstrap_test_module.inc
Implements hook_webform_element_alter().
webform_bootstrap_webform_element_alter in modules/webform_bootstrap/webform_bootstrap.module
Implements hook_webform_element_alter().
webform_example_element_properties_webform_element_alter in modules/webform_example_element_properties/webform_example_element_properties.module
Implements hook_webform_element_alter().
webform_icheck_webform_element_alter in modules/webform_icheck/webform_icheck.module
Implements hook_webform_element_alter().
webform_test_alter_hooks_webform_element_alter in tests/modules/webform_test_alter_hooks/webform_test_alter_hooks.module
Implements hook_webform_element_alter().

File

./webform.api.php, line 163
Hooks related to Webform module.

Code

function hook_webform_element_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {

  // Code here acts on all elements included in a webform.

  /** @var \Drupal\webform\WebformSubmissionForm $form_object */
  $form_object = $form_state
    ->getFormObject();

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $form_object
    ->getEntity();

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $webform_submission
    ->getWebform();

  // Add custom data attributes to all elements.
  $element['#attributes']['data-custom'] = '{custom data goes here}';
}