public function ParagraphsAccordionPlugin::buildConfigurationForm

Overrides ParagraphsBehaviorBase::buildConfigurationForm

File

paragraphs_collection/modules/paragraphs_collection_demo/src/Plugin/paragraphs/Behavior/ParagraphsAccordionPlugin.php, line 41

Class

ParagraphsAccordionPlugin
Provides a way to define accordion effect.

Namespace

Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $paragraphs_type = $form_state
    ->getFormObject()
    ->getEntity();
  if ($paragraphs_type
    ->isNew()) {
    return [];
  }
  $field_definitions = $this->entityFieldManager
    ->getFieldDefinitions('paragraph', $paragraphs_type
    ->id());
  $fields = array_filter($field_definitions, function ($definition) {
    return $definition instanceof FieldConfigInterface && $definition
      ->getFieldStorageDefinition()
      ->getCardinality() !== 1;
  });
  $options = array_map(function ($definition) {
    return $definition
      ->getLabel();
  }, $fields);
  if (!empty($options)) {
    $form['paragraph_accordion_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Accordion field'),
      '#description' => $this
        ->t('Choose a field to be used as the accordion container.'),
      '#options' => $options,
      '#default_value' => $this->configuration['paragraph_accordion_field'],
    ];
  }
  else {
    $form['message'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('There are no fields available with the cardinality greater than one. Please add at least one in the <a href=":link">Manage fields</a> page.', [
        ':link' => Url::fromRoute("entity.{$paragraphs_type->getEntityType()->getBundleOf()}.field_ui_fields", [
          $paragraphs_type
            ->getEntityTypeId() => $paragraphs_type
            ->id(),
        ])
          ->toString(),
      ]),
      '#attributes' => [
        'class' => [
          'messages messages--error',
        ],
      ],
    ];
  }
  return $form;
}