You are here

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

Class

ParagraphsGridLayoutPlugin
Provides a way to define grid based layouts.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
  $config = $this
    ->getConfiguration();
  $layout_options = [];
  $defined_layouts = $this->gridLayoutDiscovery
    ->getLayoutOptions();
  if (!empty($config['available_grid_layouts'])) {
    foreach ($config['available_grid_layouts'] as $layout) {
      $layout_options[$layout] = $defined_layouts[$layout];
    }
  }
  else {
    $layout_options = $defined_layouts;
  }
  if ($layout_options) {
    $form['#attached']['library'][] = 'paragraphs_collection/plugin_admin';

    // Create a unique id for the wrapper.
    $wrapper_id = Html::getUniqueId('layout-wrapper');
    $form['layout_wrapper'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'paragraphs-plugin-inline-container',
          'paragraphs-layout-select',
        ],
        'id' => $wrapper_id,
      ],
    ];
    $form['layout_wrapper']['layout'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Layout'),
      // @todo Add the description about what happens if there are more items
      // than slots.
      // @todo Add a nice icon representing the layout.
      '#empty_option' => $this
        ->t('- None -'),
      '#options' => $layout_options,
      '#default_value' => $paragraph
        ->getBehaviorSetting($this
        ->getPluginId(), 'layout'),
      '#attributes' => [
        'class' => [
          'paragraphs-plugin-form-element',
        ],
      ],
    ];
  }
  else {
    $form['message'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('No layouts available. See paragraphs_collection.api.php for more information on how to add them.'),
      '#attributes' => [
        'class' => [
          'messages messages--warning',
        ],
      ],
    ];
  }
  return $form;
}