class ParagraphsAccordionPlugin

Provides a way to define accordion effect.

Plugin annotation


@ParagraphsBehavior(
  id = "accordion",
  label = @Translation("Accordion"),
  description = @Translation("Accordion effect for paragraphs."),
  weight = 0
)

Hierarchy

Expanded class hierarchy of ParagraphsAccordionPlugin

File

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

Namespace

Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior
View source
class ParagraphsAccordionPlugin extends ParagraphsBehaviorBase {

  /**
   * {@inheritdoc}
   */
  public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
    foreach (Element::children($build) as $field) {
      if ($field == $this
        ->getConfiguration()['paragraph_accordion_field']) {
        $build[$field]['#attributes']['class'][] = 'accordion';
        break;
      }
    }
    $build['#attached']['library'] = [
      'paragraphs_collection_demo/accordion',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getValue('paragraph_accordion_field')) {
      $form_state
        ->setErrorByName('message', $this
        ->t('The Accordion plugin cannot be enabled if the accordion field is missing.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['paragraph_accordion_field'] = $form_state
      ->getValue('paragraph_accordion_field');
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'paragraph_accordion_field' => '',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphsAccordionPlugin::buildConfigurationForm public function Overrides ParagraphsBehaviorBase::buildConfigurationForm
ParagraphsAccordionPlugin::defaultConfiguration public function Overrides ParagraphsBehaviorBase::defaultConfiguration
ParagraphsAccordionPlugin::submitConfigurationForm public function Overrides ParagraphsBehaviorBase::submitConfigurationForm
ParagraphsAccordionPlugin::validateConfigurationForm public function Overrides ParagraphsBehaviorBase::validateConfigurationForm
ParagraphsAccordionPlugin::view public function Extends the paragraph render array with behavior. Overrides ParagraphsBehaviorInterface::view
ParagraphsBehaviorBase::$entityFieldManager protected property The entity field manager.
ParagraphsBehaviorBase::buildBehaviorForm public function Builds a behavior perspective for each paragraph based on its type. Overrides ParagraphsBehaviorInterface::buildBehaviorForm 9
ParagraphsBehaviorBase::calculateDependencies public function
ParagraphsBehaviorBase::create public static function 6
ParagraphsBehaviorBase::filterBehaviorFormSubmitValues protected function Removes default behavior form values before storing the user-set ones.
ParagraphsBehaviorBase::getConfiguration public function
ParagraphsBehaviorBase::getFieldNameOptions public function Returns list of field names for the given paragraph type and field type. Overrides ParagraphsBehaviorInterface::getFieldNameOptions
ParagraphsBehaviorBase::isApplicable public static function Returns if the plugin can be used for the provided Paragraphs type. Overrides ParagraphsBehaviorInterface::isApplicable 1
ParagraphsBehaviorBase::preprocess public function Adds a default set of helper variables for preprocessors and templates. Overrides ParagraphsBehaviorInterface::preprocess 1
ParagraphsBehaviorBase::setConfiguration public function
ParagraphsBehaviorBase::settingsIcon public function Returns a short info icon for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsIcon 1
ParagraphsBehaviorBase::settingsSummary public function Returns a short summary for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsSummary 8
ParagraphsBehaviorBase::submitBehaviorForm public function Submit the values taken from the form to store the values. Overrides ParagraphsBehaviorInterface::submitBehaviorForm 3
ParagraphsBehaviorBase::validateBehaviorForm public function Validates the behavior fields form. Overrides ParagraphsBehaviorInterface::validateBehaviorForm 1
ParagraphsBehaviorBase::__construct public function Constructs a ParagraphsBehaviorBase object. 6