You are here

public function OverrideWebformVariant::buildConfigurationForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformVariant/OverrideWebformVariant.php \Drupal\webform\Plugin\WebformVariant\OverrideWebformVariant::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides WebformVariantBase::buildConfigurationForm

File

src/Plugin/WebformVariant/OverrideWebformVariant.php, line 56

Class

OverrideWebformVariant
Webform override variant.

Namespace

Drupal\webform\Plugin\WebformVariant

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $webform = $this
    ->getWebform();
  $form['overrides'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Overrides'),
    '#open' => TRUE,
    '#access' => $this->currentUser
      ->hasPermission('edit webform source'),
  ];

  // Settings.
  $form['overrides']['settings'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Settings (YAML)'),
    '#description' => $this
      ->t('Enter the setting name and value as YAML.'),
    '#more_title' => $this
      ->t('Default settings'),
    '#more' => [
      '#theme' => 'webform_codemirror',
      '#type' => 'yaml',
      '#code' => WebformYaml::encode($webform
        ->getSettings()),
    ],
    '#parents' => [
      'settings',
      'settings',
    ],
    '#default_value' => $this->configuration['settings'],
  ];

  // Elements.
  $form['overrides']['elements'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Elements (YAML)'),
    '#description' => $this
      ->t('Enter the element name and properties as YAML.'),
    '#more_title' => $this
      ->t('Default elements'),
    '#more' => [
      '#theme' => 'webform_codemirror',
      '#type' => 'yaml',
      '#code' => WebformYaml::encode($webform
        ->getElementsDecodedAndFlattened()),
    ],
    '#parents' => [
      'settings',
      'elements',
    ],
    '#default_value' => $this->configuration['elements'],
  ];

  // Handlers.
  $handlers = $webform
    ->get('handlers');
  foreach ($handlers as &$handler) {
    unset($handler['id'], $handler['handler_id']);
  }
  $form['overrides']['handlers'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Handlers (YAML)'),
    '#description' => $this
      ->t('Enter the handler id and settings as YAML.'),
    '#more_title' => $this
      ->t('Default handlers'),
    '#more' => [
      '#theme' => 'webform_codemirror',
      '#type' => 'yaml',
      '#code' => WebformYaml::encode($handlers),
    ],
    '#parents' => [
      'settings',
      'handlers',
    ],
    '#default_value' => $this->configuration['handlers'],
  ];

  // Development.
  $form['development'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Development settings'),
  ];
  $form['development']['debug'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable debugging'),
    '#description' => $this
      ->t('If checked, settings will be displayed onscreen to all users.'),
    '#return_value' => TRUE,
    '#parents' => [
      'settings',
      'debug',
    ],
    '#default_value' => $this->configuration['debug'],
  ];
  return $form;
}