You are here

public function ParagraphsStylePlugin::buildConfigurationForm in Paragraphs Collection 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 ParagraphsBehaviorBase::buildConfigurationForm

File

src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php, line 180

Class

ParagraphsStylePlugin
Provides style selection plugin.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form['groups'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Style groups'),
    '#description' => $this
      ->t('Restrict available styles to certain style groups.'),
    '#options' => $this->yamlStyleDiscovery
      ->getStyleGroupsLabel(),
    '#default_value' => array_keys($this->configuration['groups']),
    '#ajax' => [
      'callback' => [
        $this,
        'updateDefaultStyle',
      ],
      'wrapper' => 'style-wrapper',
    ],
  ];

  // @todo: Remove getCompleteFormState() after https://www.drupal.org/project/drupal/issues/2798261.
  $group_key = [
    'behavior_plugins',
    $this
      ->getPluginId(),
    'settings',
    'groups',
  ];
  $groups = $form_state
    ->getCompleteFormState()
    ->getValue($group_key, $this->configuration['groups']);
  $form['groups_defaults'] = [
    '#type' => 'container',
    '#prefix' => '<div id="style-wrapper">',
    '#suffix' => '</div>',
  ];
  foreach (array_keys(array_filter($groups)) as $group_id) {
    $default = '';
    if (!empty($this->configuration['groups'][$group_id]['default'])) {
      $default = $this->configuration['groups'][$group_id]['default'];
    }
    $group_label = $this->yamlStyleDiscovery
      ->getGroupLabel($group_id);
    $form['groups_defaults'][$group_id]['default'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('@label default style', [
        '@label' => $group_label,
      ]),
      '#empty_option' => $this
        ->t('- None -'),
      '#options' => $this->yamlStyleDiscovery
        ->getStyleOptions($group_id),
      '#description' => $this
        ->t('Default option for the @label group on a behavior form.', [
        '@label' => $group_label,
      ]),
      '#default_value' => $default,
    ];
  }
  return $form;
}