You are here

public function YamlFormElementBase::buildConfigurationForm in YAML Form 8

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

1 call to YamlFormElementBase::buildConfigurationForm()
YamlFormElement::buildConfigurationForm in src/Plugin/YamlFormElement/YamlFormElement.php
Form constructor.
1 method overrides YamlFormElementBase::buildConfigurationForm()
YamlFormElement::buildConfigurationForm in src/Plugin/YamlFormElement/YamlFormElement.php
Form constructor.

File

src/YamlFormElementBase.php, line 1201

Class

YamlFormElementBase
Provides a base class for a form element.

Namespace

Drupal\yamlform

Code

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

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

  // Get element properties which can be altered by YamlFormElementHandlers.
  // @see \Drupal\yamlform\Plugin\YamlFormElement\YamlFormEntityReferenceTrait::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 form.
  $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 'yamlform_*' prefix from the type name.
  if (isset($custom_properties['type'])) {
    $form['type'] = [
      '#type' => 'value',
      '#value' => preg_replace('/^yamlform_/', '', $custom_properties['type']),
      '#parents' => [
        'properties',
        'type',
      ],
    ];
    unset($custom_properties['type']);
  }

  // Allow custom properties (ie #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 yamlform 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' => 'yamlform_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 upon submission.') . '<br/>' . $this
      ->t('These properties and callbacks are not allowed: @properties', [
      '@properties' => YamlFormArrayHelper::toString(YamlFormArrayHelper::addPrefix(YamlFormElementHelper::$ignoredProperties)),
    ]),
    '#default_value' => $custom_properties,
    '#parents' => [
      'properties',
      'custom',
    ],
  ];
  $form['token_tree_link'] = $this->tokenManager
    ->buildTreeLink();

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