public function ParagraphsStylePlugin::buildConfigurationForm

Overrides ParagraphsBehaviorBase::buildConfigurationForm

File

paragraphs_collection/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;
}