You are here

public function ParagraphsLanguagePlugin::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/ParagraphsLanguagePlugin.php, line 78

Class

ParagraphsLanguagePlugin
Provides a way to hide specific paragraphs depending on the current language.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
  if (!$this->languageManager
    ->isMultilingual()) {
    return [];
  }
  foreach ($this->languageManager
    ->getLanguages() as $language_code => $language) {
    $options[$language_code] = $language
      ->getName();
  }
  $form['container'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'paragraphs-plugin-inline-container',
      ],
    ],
  ];
  $form['container']['visibility'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Language visibility'),
    '#options' => [
      'always' => $this
        ->t('- Always visible -'),
      'hide' => $this
        ->t('Hide for'),
      'show' => $this
        ->t('Show for'),
    ],
    '#default_value' => $paragraph
      ->getBehaviorSetting($this
      ->getPluginId(), [
      'container',
      'visibility',
    ]),
    '#multiple' => FALSE,
    '#attributes' => [
      'id' => [
        'paragraphs-behavior-language-behavior-form-visibility-' . $paragraph
          ->id(),
      ],
      'class' => [
        'paragraphs-plugin-form-element',
      ],
    ],
  ];
  $use_select2 = $this->moduleHandler
    ->moduleExists('select2');
  $form['container']['languages'] = [
    '#type' => $use_select2 ? 'select2' : 'select',
    '#options' => $options,
    '#empty_option' => $this
      ->t('- None -'),
    '#empty_value' => 'none',
    '#default_value' => $paragraph
      ->getBehaviorSetting($this
      ->getPluginId(), [
      'container',
      'languages',
    ]),
    '#states' => [
      'invisible' => [
        ':input[id="paragraphs-behavior-language-behavior-form-visibility-' . $paragraph
          ->id() . '"]' => [
          'value' => 'always',
        ],
      ],
    ],
    '#multiple' => TRUE,
    '#attributes' => [
      'class' => [
        'paragraphs-behavior-language-behavior-form-languages',
        'paragraphs-plugin-form-element',
      ],
    ],
  ];
  if ($use_select2) {
    $form['container']['languages']['#select2']['width'] = 'auto';
  }
  $form['#attached']['library'][] = 'paragraphs_collection/plugin_admin';
  $form['container']['#attributes']['class'][] = 'paragraphs-behavior-language-behavior-form';
  return $form;
}