You are here

public function ParagraphsStylePlugin::buildBehaviorForm in Paragraphs Collection 8

Builds a behavior perspective for each paragraph based on its type.

This method is responsible for building the behavior form for each Paragraph so the user can set special attributes and properties.

Parameters

\Drupal\paragraphs\ParagraphInterface $paragraph: The paragraph.

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.

Return value

array The fields build array that the plugin creates.

Overrides ParagraphsBehaviorBase::buildBehaviorForm

File

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

Class

ParagraphsStylePlugin
Provides style selection plugin.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
  $form['#attached']['library'][] = 'paragraphs_collection/plugin_admin';

  // Create a unique id for the wrapper.
  $wrapper_id = Html::getUniqueId('style-wrapper');
  $form['style_wrapper'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'paragraphs-plugin-inline-container',
        'multiple-lines',
        'paragraphs-style-select',
      ],
      'id' => $wrapper_id,
    ],
  ];
  $paragraph_styles = $this
    ->getStyles($paragraph);
  foreach (array_keys($this->configuration['groups']) as $group_id) {
    $group_default = $this->configuration['groups'][$group_id]['default'];
    $default_style = $paragraph_styles[$group_id] ?? NULL;

    // We filter advanced styles if the user has no permission to use them and
    // disable it in case an advanced style is currently selected and enabled.
    $style_options = $this
      ->getStyleOptions($group_id, $group_default, TRUE);
    $disabled = FALSE;
    if ($default_style && !$paragraph
      ->isNew()) {
      $default_style_definition = $this->yamlStyleDiscovery
        ->getStyle($default_style);
      if ($default_style_definition && !$this->yamlStyleDiscovery
        ->isAllowedAccess($default_style_definition)) {
        $style_options[$default_style] = $default_style_definition['title'];
        $disabled = TRUE;
      }
    }

    // Show the styles selection if:
    // - The default style is disabled on an existing paragraph
    // - There is more than one style option
    // - There is exactly one style option and no style group default style.
    if ($disabled && !$paragraph
      ->isNew() || count($style_options) > 1 || count($style_options) === 1 && !$group_default) {
      $form['style_wrapper']['styles'][$group_id] = [
        '#type' => 'select',
        '#title' => $this->yamlStyleDiscovery
          ->getGroupWidgetLabel($group_id),
        '#options' => $style_options,
        '#default_value' => $default_style,
        '#attributes' => [
          'class' => [
            'paragraphs-style',
          ],
        ],
        '#disabled' => $disabled,
        '#attributes' => [
          'class' => [
            'paragraphs-plugin-form-element',
          ],
        ],
      ];

      // Allow empty option in case there is no default style configured.
      if (empty($group_default)) {
        $form['style_wrapper']['styles'][$group_id]['#empty_option'] = $this
          ->t('- Default -');
      }
    }
  }

  // Clean the current plugin form if there are no styles to display.
  if (empty($form['style_wrapper']['styles'])) {

    // @todo: Unset the current plugin form element passed by reference.
    // Remove after https://www.drupal.org/project/paragraphs/issues/2971115.
    $form = [];
    return [];
  }
  return $form;
}