You are here

public function WebformElementBase::buildConfigurationForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::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 PluginFormInterface::buildConfigurationForm

3 calls to WebformElementBase::buildConfigurationForm()
WebformActions::buildConfigurationForm in src/Plugin/WebformElement/WebformActions.php
Form constructor.
WebformCompositeBase::buildConfigurationForm in src/Plugin/WebformElement/WebformCompositeBase.php
Form constructor.
WebformElement::buildConfigurationForm in src/Plugin/WebformElement/WebformElement.php
Form constructor.
3 methods override WebformElementBase::buildConfigurationForm()
WebformActions::buildConfigurationForm in src/Plugin/WebformElement/WebformActions.php
Form constructor.
WebformCompositeBase::buildConfigurationForm in src/Plugin/WebformElement/WebformCompositeBase.php
Form constructor.
WebformElement::buildConfigurationForm in src/Plugin/WebformElement/WebformElement.php
Form constructor.

File

src/Plugin/WebformElementBase.php, line 3386

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $default_properties = $this
    ->getDefaultProperties();
  $element_properties = WebformArrayHelper::removePrefix($this->configuration) + $default_properties;

  // Make sure 'format_items' is removed if the element does not
  // support multiple values.
  // @todo Webform 8.x-6.x: Remove and assume custom element are fixed.
  if (!$this
    ->supportsMultipleValues()) {
    unset($default_properties['format_items'], $default_properties['format_items_html'], $default_properties['format_items_text']);
  }

  // Set default and element properties.
  // Note: Storing this information in the webform's state allows modules to view
  // and alter this information using webform alteration hooks.
  $form_state
    ->set('default_properties', $default_properties);
  $form_state
    ->set('element_properties', $element_properties);
  $form = $this
    ->form($form, $form_state);
  \Drupal::moduleHandler()
    ->alter('webform_element_configuration_form', $form, $form_state);

  // Get default and element properties which can be altered by WebformElementHandlers.
  // @see \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::form
  $element_properties = $form_state
    ->get('element_properties');

  // Copy element properties to custom properties which will be determined
  // as the default values are set.
  $custom_properties = $element_properties;

  // Populate the webform.
  $this
    ->setConfigurationFormDefaultValueRecursive($form, $custom_properties);

  // Set fieldset weights so that they appear first.
  foreach ($form as &$element) {
    if (is_array($element) && !isset($element['#weight']) && isset($element['#type']) && $element['#type'] === 'fieldset') {
      $element['#weight'] = -20;
    }
  }

  // Store 'type' as a hardcoded value and make sure it is always first.
  // Also always remove the 'webform_*' prefix from the type name.
  if (isset($custom_properties['type'])) {
    $form['type'] = [
      '#type' => 'value',
      '#value' => $custom_properties['type'],
      '#parents' => [
        'properties',
        'type',
      ],
    ];
    unset($custom_properties['type']);
  }

  // Allow custom properties (i.e. #attributes) to be added to the element.
  $form['custom'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom settings'),
    '#open' => $custom_properties ? TRUE : FALSE,
    '#access' => $this->currentUser
      ->hasPermission('edit webform source'),
  ];
  if ($api_url = $this
    ->getPluginApiUrl()) {
    $t_args = [
      ':href' => $api_url
        ->toString(),
      '%label' => $this
        ->getPluginLabel(),
    ];
    $form['custom']['#description'] = $this
      ->t('Read the %label element\'s <a href=":href">API documentation</a>.', $t_args);
  }
  $form['custom']['properties'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Custom properties'),
    '#description' => $this
      ->t('Properties do not have to be prepended with a hash (#) character, the hash character will be automatically added to the custom properties.') . '<br /><br />' . $this
      ->t('These properties and callbacks are not allowed: @properties', [
      '@properties' => WebformArrayHelper::toString(WebformArrayHelper::addPrefix(WebformElementHelper::$ignoredProperties)),
    ]),
    '#default_value' => $custom_properties,
    '#parents' => [
      'properties',
      'custom',
    ],
  ];
  $this->tokenManager
    ->elementValidate($form);

  // Set custom properties.
  // Note: Storing this information in the webform's state allows modules to
  // view and alter this information using webform alteration hooks.
  $form_state
    ->set('custom_properties', $custom_properties);
  return $this
    ->buildConfigurationFormTabs($form, $form_state);
}