public function ParagraphsGridLayoutPlugin::buildConfigurationForm

Overrides ParagraphsBehaviorBase::buildConfigurationForm

File

paragraphs_collection/src/Plugin/paragraphs/Behavior/ParagraphsGridLayoutPlugin.php, line 83

Class

ParagraphsGridLayoutPlugin
Provides a way to define grid based layouts.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $paragraphs_type = $form_state
    ->getFormObject()
    ->getEntity();
  if ($paragraphs_type
    ->isNew()) {
    return [];
  }

  // The grid gets it's content from referenced entities (ERR or ER).
  $reference_field_options = [];
  $field_definitions = $this->entityFieldManager
    ->getFieldDefinitions('paragraph', $paragraphs_type
    ->id());
  foreach ($field_definitions as $name => $definition) {
    if ($definition instanceof FieldConfigInterface) {
      if ($definition
        ->getFieldStorageDefinition()
        ->getCardinality() != 1 && in_array($definition
        ->getType(), [
        'entity_reference',
        'entity_reference_revisions',
      ])) {
        $reference_field_options[$name] = $definition
          ->getLabel();
      }
    }
  }
  if ($reference_field_options) {
    $form['paragraph_reference_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Grid field'),
      '#description' => $this
        ->t('Field to be used as the grid container.'),
      '#options' => $reference_field_options,
      '#default_value' => $this->configuration['paragraph_reference_field'],
    ];
  }
  else {
    $url = Url::fromRoute('entity.paragraph.field_ui_fields', [
      $paragraphs_type
        ->getEntityTypeId() => $paragraphs_type
        ->id(),
    ]);
    $form['message'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('No paragraph reference field type available. Please add at least one in the <a href=":link">Manage fields</a> page.', [
        ':link' => $url
          ->toString(),
      ]),
      '#attributes' => [
        'class' => [
          'messages messages--error',
        ],
      ],
    ];
  }

  // Select pre-defined grid layouts.
  if ($layout_options = $this->gridLayoutDiscovery
    ->getLayoutOptions()) {
    $form['available_grid_layouts'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Grid layouts'),
      '#description' => $this
        ->t('Layouts that will be available when creating paragraphs. Select none to allow displaying all layouts.'),
      '#options' => $layout_options,
      '#default_value' => $this->configuration['available_grid_layouts'],
      '#empty_value' => [],
    ];
  }
  return $form;
}