You are here

public static function WebformExampleComposite::afterBuild in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_example_composite/src/Element/WebformExampleComposite.php \Drupal\webform_example_composite\Element\WebformExampleComposite::afterBuild()

Performs the after_build callback.

File

modules/webform_example_composite/src/Element/WebformExampleComposite.php, line 65

Class

WebformExampleComposite
Provides a 'webform_example_composite'.

Namespace

Drupal\webform_example_composite\Element

Code

public static function afterBuild(array $element, FormStateInterface $form_state) {

  // Add #states targeting the specific element and table row.
  preg_match('/^(.+)\\[[^]]+]$/', $element['#name'], $match);
  $composite_name = $match[1];
  $element['#states']['disabled'] = [
    [
      ':input[name="' . $composite_name . '[first_name]"]' => [
        'empty' => TRUE,
      ],
    ],
    [
      ':input[name="' . $composite_name . '[last_name]"]' => [
        'empty' => TRUE,
      ],
    ],
  ];

  // Add .js-form-wrapper to wrapper (ie td) to prevent #states API from
  // disabling the entire table row when this element is disabled.
  $element['#wrapper_attributes']['class'][] = 'js-form-wrapper';
  return $element;
}