You are here

public function WebformComputedBase::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformComputedBase.php \Drupal\webform\Plugin\WebformElement\WebformComputedBase::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

1 call to WebformComputedBase::form()
WebformComputedTwig::form in src/Plugin/WebformElement/WebformComputedTwig.php
Gets the actual configuration webform array to be built.
1 method overrides WebformComputedBase::form()
WebformComputedTwig::form in src/Plugin/WebformElement/WebformComputedTwig.php
Gets the actual configuration webform array to be built.

File

src/Plugin/WebformElement/WebformComputedBase.php, line 174

Class

WebformComputedBase
Provides a base class for 'webform_computed' elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['computed'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Computed settings'),
  ];
  $form['computed']['warning'] = [
    '#type' => 'webform_message',
    '#message_message' => $this
      ->t('Computing complex or multiple values with or without Ajax can be resource intensive and may have performance implications. When possible try limiting or combining computations or consider using custom Twig functions, JavaScript, or PHP.'),
    '#message_type' => 'warning',
    '#message_close' => TRUE,
    '#message_storage' => WebformMessageElement::STORAGE_SESSION,
    '#access' => TRUE,
  ];
  $form['computed']['display_on'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Display on'),
    '#options' => $this
      ->getDisplayOnOptions(TRUE),
  ];
  $form['computed']['display_on_message'] = [
    '#type' => 'webform_message',
    '#message_message' => $this
      ->t("This computed element's value will only be available as a token or exported value."),
    '#message_type' => 'warning',
    '#access' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="properties[display_on]"]' => [
          'value' => WebformElementDisplayOnInterface::DISPLAY_ON_NONE,
        ],
      ],
    ],
  ];
  $form['computed']['mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Mode'),
    '#options' => [
      WebformComputedBaseElement::MODE_AUTO => $this
        ->t('Auto-detect'),
      WebformComputedBaseElement::MODE_HTML => $this
        ->t('HTML'),
      WebformComputedBaseElement::MODE_TEXT => $this
        ->t('Plain text'),
    ],
  ];
  $form['computed']['template'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'text',
    '#title' => $this
      ->t('Computed value/markup'),
  ];
  $form['computed']['whitespace'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Remove whitespace around the'),
    '#empty_option' => $this
      ->t('- None -'),
    '#options' => [
      WebformComputedTwigElement::WHITESPACE_TRIM => $this
        ->t('computed value'),
      WebformComputedTwigElement::WHITESPACE_SPACELESS => $this
        ->t('computed value and between HTML tags'),
    ],
  ];
  $form['computed']['hide_empty'] = [
    '#type' => 'checkbox',
    '#return_value' => TRUE,
    '#title' => $this
      ->t('Hide empty'),
    '#description' => $this
      ->t('If checked the computed elements will be hidden from display when the value is an empty string.'),
  ];
  $form['computed']['store'] = [
    '#type' => 'checkbox',
    '#return_value' => TRUE,
    '#title' => $this
      ->t('Store value in the database'),
    '#description' => $this
      ->t('The value will be stored in the database. As a result, it will only be recalculated when the submission is updated. This option is required when accessing the computed element through Views.'),
  ];
  $form['computed']['ajax'] = [
    '#type' => 'checkbox',
    '#return_value' => TRUE,
    '#title' => $this
      ->t('Automatically update the computed value using Ajax'),
    '#description' => $this
      ->t('If checked, any element used within the computed value/markup will trigger any automatic update.'),
  ];
  $form['computed']['tokens'] = [
    '#access' => TRUE,
    '#weight' => 10,
  ] + $this->tokenManager
    ->buildTreeElement();
  return $form;
}