devel_wizard-2.x-dev/templates/spell/paragraph_behavior/class.php.twig

templates/spell/paragraph_behavior/class.php.twig
<?php

declare(strict_types=1);

namespace {{ class.namespace }};

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;

/**
 * @ParagraphsBehavior(
 *   id = "{{ plugin.id }}",
 *   label = @Translation("{{ plugin.label }}"),
 *   description = @Translation("{{ plugin.description }}"),
 * )
 */
class {{ class.name }} extends ParagraphsBehaviorBase implements ConfigurableInterface {

  /**
   * {@inheritdoc}
   */
  public static function isApplicable($paragraphs_type): bool {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    $conf = $this->getConfiguration();

    $form = parent::buildConfigurationForm($form, $form_state);
    $form['allowed_options'] = [
      '#type' => 'checkboxes',
      '#required' => FALSE,
      '#title' => $this->t('Allowed options'),
      '#default_value' => $conf['allowed_options'] ?? [],
      '#options' => [
        'a' => $this->t('A'),
        'b' => $this->t('B'),
        'c' => $this->t('C'),
      ],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    parent::submitConfigurationForm($form, $form_state);
    $configuration = $this->getConfiguration();
    $configuration += $form_state->getValues();
    $this->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state): array {
    $conf = $this->getConfiguration();
    $values = [
      'a' => $paragraph->getBehaviorSetting($this->getPluginId(), 'a'),
      'b' => $paragraph->getBehaviorSetting($this->getPluginId(), 'b'),
      'c' => $paragraph->getBehaviorSetting($this->getPluginId(), 'c'),
    ];

    $form['a'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('A'),
      '#default_value' => $values['a'],
      '#access' => !empty($conf['allowed_options']['a']),
    ];

    $form['b'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('B'),
      '#default_value' => $values['b'],
      '#access' => !empty($conf['allowed_options']['b']),
    ];

    $form['c'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('C'),
      '#default_value' => $values['c'],
      '#access' => !empty($conf['allowed_options']['c']),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary(Paragraph $paragraph): array {
    $summary = parent::settingsSummary($paragraph);
    $values = $paragraph->getBehaviorSetting($this->getPluginId(), []);

    $summary[] = $this->t('A = @value', ['@value' => $values['a'] ?? '']);
    $summary[] = $this->t('B = @value', ['@value' => $values['b'] ?? '']);
    $summary[] = $this->t('C = @value', ['@value' => $values['c'] ?? '']);

    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function view(
    array &$build,
    Paragraph $paragraph,
    EntityViewDisplayInterface $display,
    $view_mode,
  ): void {
    $conf = $this->getConfiguration();
    $values = $paragraph->getBehaviorSetting($this->getPluginId(), []);

    foreach (array_keys($conf['allowed_options'], TRUE) as $key) {
      if (empty($values[$key])) {
        continue;
      }

      $build['#attributes']['class'][] = sprintf(
        '%s--%s',
        Html::getClass($this->getPluginId()),
        $key,
      );
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc