WebformExampleComposite.php in Webform 6.x
Same filename in this branch
Same filename and directory in other branches
Namespace
Drupal\webform_example_composite\ElementFile
modules/webform_example_composite/src/Element/WebformExampleComposite.phpView source
<?php
namespace Drupal\webform_example_composite\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Element\WebformCompositeBase;
/**
* Provides a 'webform_example_composite'.
*
* Webform composites contain a group of sub-elements.
*
*
* IMPORTANT:
* Webform composite can not contain multiple value elements (i.e. checkboxes)
* or composites (i.e. webform_address)
*
* @FormElement("webform_example_composite")
*
* @see \Drupal\webform\Element\WebformCompositeBase
* @see \Drupal\webform_example_composite\Element\WebformExampleComposite
*/
class WebformExampleComposite extends WebformCompositeBase {
/**
* {@inheritdoc}
*/
public function getInfo() {
return parent::getInfo() + [
'#theme' => 'webform_example_composite',
];
}
/**
* {@inheritdoc}
*/
public static function getCompositeElements(array $element) {
$elements = [];
$elements['first_name'] = [
'#type' => 'textfield',
'#title' => t('First name'),
];
$elements['last_name'] = [
'#type' => 'textfield',
'#title' => t('Last name'),
];
$elements['date_of_birth'] = [
'#type' => 'date',
'#title' => t('Date of birth'),
// Use #after_build to add #states.
'#after_build' => [
[
get_called_class(),
'afterBuild',
],
],
];
$elements['sex'] = [
'#type' => 'select',
'#title' => t('Sex'),
'#options' => 'sex',
// Use #after_build to add #states.
'#after_build' => [
[
get_called_class(),
'afterBuild',
],
],
];
return $elements;
}
/**
* Performs the after_build callback.
*/
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;
}
}
Classes
Name | Description |
---|---|
WebformExampleComposite | Provides a 'webform_example_composite'. |