You are here

public function WebformTestElementProperties::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/modules/webform_test_element/src/Plugin/WebformElement/WebformTestElementProperties.php \Drupal\webform_test_element\Plugin\WebformElement\WebformTestElementProperties::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformElementBase::form

File

tests/modules/webform_test_element/src/Plugin/WebformElement/WebformTestElementProperties.php, line 44

Class

WebformTestElementProperties
Provides a 'webform_test_element_properties' element.

Namespace

Drupal\webform_test_element\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Get element properties.
  $element_properties = $form_state
    ->get('element_properties');
  $element_properties['property_c'] = date('c');
  $form_state
    ->set('element_properties', $element_properties);
  $form['test'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Test'),
  ];
  $form['test']['property_a'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Property A'),
    '#description' => $this
      ->t('Changing this value will update B and C'),
    '#options' => [
      'One' => $this
        ->t('One'),
      'Two' => $this
        ->t('Two'),
      'Three' => $this
        ->t('Three'),
    ],
  ];
  $form['test']['property_b'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Property B'),
    '#description' => $this
      ->t('Property C is always equal to Property A.'),
    '#attributes' => [
      'readonly' => TRUE,
      'style' => 'background-color: #eee',
    ],
  ];
  $form['test']['property_c'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Property C'),
    '#description' => $this
      ->t('Property C is always updated to reflect the current timestamp.'),
    '#attributes' => [
      'readonly' => TRUE,
      'style' => 'background-color: #eee',
    ],
  ];
  $this
    ->buildAjaxElement('test-element', $form['test'], $form['test']['property_a']);

  // Must explicitly set properties to be included in $form_state.
  $form['test']['update']['#limit_validation_errors'] = [
    [
      'properties',
      'property_a',
    ],
    [
      'properties',
      'property_b',
    ],
    [
      'properties',
      'property_c',
    ],
  ];
  return $form;
}