You are here

function hook_webform_element_ELEMENT_TYPE_alter in Webform 8.5

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

Alter webform elements for a specific type.

Modules can implement hook_webform_element_ELEMENT_TYPE_alter() to modify a specific webform element, rather than using hook_webform_element_alter() and checking the element type.

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. See hook_field_widget_alter() for the structure and content of the array.

See also

\Drupal\webform\WebformSubmissionForm::prepareElements()

hook_webform_element_alter(()

9 functions implement hook_webform_element_ELEMENT_TYPE_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_webform_element_webform_likert_alter in modules/webform_bootstrap/webform_bootstrap.module
Implements hook_webform_element_ELEMENT_TYPE_alter().
webform_bootstrap_webform_element_webform_terms_of_service_alter in modules/webform_bootstrap/webform_bootstrap.module
Implements hook_webform_element_ELEMENT_TYPE_alter().
webform_example_element_properties_webform_element_configuration_form_alter in modules/webform_example_element_properties/webform_example_element_properties.module
Implements hook_webform_element_configuration_form_alter().
webform_example_element_properties_webform_element_default_properties_alter in modules/webform_example_element_properties/webform_example_element_properties.module
Implements hook_webform_element_default_properties_alter().
webform_example_element_properties_webform_element_translatable_properties_alter in modules/webform_example_element_properties/webform_example_element_properties.module
Implements hook_webform_element_translatable_properties_alter().

... See full list

File

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

Code

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

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

  // Attach a custom library to the element type.
  $element['#attached']['library'][] = 'MODULE/MODULE.element.ELEMENT_TYPE';
}