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

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

namespace Drupal\fieldry\Plugin\FieldryWrapper;

use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\fieldry\Attribute\FieldryWrapper;
use Drupal\fieldry\Plugin\ConfigurableWrapperInterface;
use Drupal\fieldry\Plugin\WrapperBase;

/**
 * Creates a field wrapper that renders field item content with a separator.
 */
#[FieldryWrapper(
  id: 'inline',
  label: new TranslatableMarkup('Inline field items'),
  fieldTypes: [
    'list_string',
    'list_integer',
    'list_float',
    'integer',
    'float',
    'decimal',
    'string',
    'text',
    'boolean',
    'datetime',
    'timestamp',
    'email',
    'link',
  ]
)]
class InlineWrapper extends WrapperBase implements ConfigurableWrapperInterface {

  /**
   * {@inheritdoc}
   */
  public static function isApplicable($plugin_definition, FormatterInterface $formatter, FieldDefinitionInterface $field_definition): bool {
    if (is_a($field_definition->getClass(), EntityReferenceFieldItemListInterface::class, TRUE)) {
      // If field is an entity reference, only specific formatters are allowed.
      return $formatter->getPluginId() === 'entity_reference_label';
    }

    // For all other field types, fallback to the default handling behavior.
    return parent::isApplicable($plugin_definition, $formatter, $field_definition);
  }

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

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

    $elements['separator'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Separator'),
      '#default_value' => $this->configuration['separator'],
    ];

    return $elements;
  }

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

    foreach (Element::children($element) as $delta) {
      $variables['content'][] = $element[$delta];
      $variables['content'][] = [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#attributes' => [
          'class' => ['field-separator'],
        ],
        '#plain_text' => $this->configuration['separator'],
      ];
    }

    // There is a trailing separator that we can pop here. This should be more
    // efficient than doing an "if" statement in the loop.
    array_pop($variables['content']);
    $variables['content']['#sorted'] = TRUE;
  }

}

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

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