You are here

public function ParagraphsSliderPlugin::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

modules/paragraphs_collection_demo/src/Plugin/paragraphs/Behavior/ParagraphsSliderPlugin.php, line 180

Class

ParagraphsSliderPlugin
Provides Slider plugin.

Namespace

Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior

Code

public function buildBehaviorForm(ParagraphInterface $paragraphs_entity, array &$form, FormStateInterface $form_state) {
  $options = $this
    ->getAllOptionSet();
  $slider_optionset = $this
    ->getConfiguration()['slick_slider'];
  $default_value = $paragraphs_entity
    ->getBehaviorSetting($this
    ->getPluginId(), 'slick_slider');
  if (!empty($slider_optionset)) {

    // Filter optionsets with preselected optionsets.
    $options = array_intersect_key($options, array_flip($slider_optionset));
  }
  elseif (count($options) === 1 && empty($default_value)) {

    // Preselect the only possible option.
    $keys = array_keys($options);
    $default_value = $keys[0];
  }
  $form['slick_slider'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Slider'),
    '#options' => $options,
    '#description' => $this
      ->t('Slider effect used to display the paragraph.'),
    '#default_value' => $default_value,
    '#required' => TRUE,
  ];
  if (!in_array($default_value, array_keys($options))) {
    $form['slick_slider']['#empty_option'] = $this
      ->t('- None -');
  }
  return $form;
}