fieldry-1.0.x-dev/src/Plugin/FieldryWrapper/ListWrapper.php

src/Plugin/FieldryWrapper/ListWrapper.php
<?php

namespace Drupal\fieldry\Plugin\FieldryWrapper;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Template\Attribute;
use Drupal\fieldry\Attribute\FieldryWrapper;
use Drupal\fieldry\Plugin\ConfigurableWrapperInterface;
use Drupal\fieldry\Plugin\WrapperBase;

/**
 * A field wrapper which allows field items as a HTML list.
 */
#[FieldryWrapper(
  id: 'list',
  label: new TranslatableMarkup('HTML lists'),
)]
class ListWrapper extends WrapperBase implements ConfigurableWrapperInterface {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration(): array {
    return [
      'list_type' => 'ul',
      'list_classes' => [],
      'item_classes' => [],
    ] + parent::defaultCOnfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function getWrapperTagOptions(): array {
    return [
      '' => $this->t('-- NONE --'),
      'div' => 'DIV',
      'nav' => 'NAV',
      'header' => 'HEADER',
      'footer' => 'FOOTER',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    $elements = parent::buildConfigurationForm($form, $form_state);

    $elements['list_type'] = [
      '#type' => 'select',
      '#title' => $this->t('List type'),
      '#options' => [
        'ul' => $this->t('Unordered list'),
        'ol' => $this->t('Ordered list'),
      ],
      '#default_value' => $this->configuration['list_type'],
    ];

    $elements['list_classes'] = [
      '#type' => 'css_class',
      '#title' => $this->t('List classes'),
      '#default_value' => $this->configuration['list_classes'],
    ];

    $elements['item_classes'] = [
      '#type' => 'css_class',
      '#title' => $this->t('List item classes'),
      '#default_value' => $this->configuration['item_classes'],
    ];

    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function getItemAttributes(FieldItemInterface $item): array {
    $attributes = $item->_attributes ?? [];

    if ($classNames = $this->configuration['item_classes']) {
      $attributes['class'] = empty($attributes['class']) ? $classNames : [...$classNames, ...$attributes['class']];
    }

    return $attributes;
  }

  /**
   * {@inheritdoc}
   */
  public function preprocess(array $element, FieldItemListInterface $items, array &$variables): void {
    parent::preprocess($element, $items, $variables);

    $variables['items'] = [];
    if (!$items->isEmpty()) {
      $config = $this->configuration;
      $variables['items_wrapper_tag'] = $config['list_type'];
      $variables['items_wrapper_attributes'] = new Attribute([
        'class' => $config['list_classes'],
      ]);

      $variables['item_tag'] = 'li';
      foreach (Element::children($element) as $delta) {
        /** @var \Drupal\Core\Field\FieldItemInterface $item */
        $item = $items->get($delta);
        $attrs = $item
          ? $this->getItemAttributes($item)
          : ['class' => $this->configuration['item_classes'] ?? []];

        $variables['items'][$delta] = [
          'content' => $element[$delta],
          'attributes' => new Attribute($attrs),
        ];
      }
    }
  }

}

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

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