fieldry-1.0.x-dev/src/Plugin/FieldryWrapper/StandardWrapper.php
src/Plugin/FieldryWrapper/StandardWrapper.php
<?php
namespace Drupal\fieldry\Plugin\FieldryWrapper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Template\Attribute;
use Drupal\fieldry\Attribute\FieldryWrapper;
use Drupal\fieldry\Plugin\WrapperBase;
use Drupal\fieldry\Plugin\ConfigurableWrapperInterface;
/**
* A field wrapper that wraps fields without additional DIV tags.
*/
#[FieldryWrapper(
id: 'standard',
label: new TranslatableMarkup('Configurable Wrapper'),
)]
class StandardWrapper extends WrapperBase implements ConfigurableWrapperInterface {
/**
* Get a list of HTML tag types allowed for wrapping field items.
*
* @return array<\Stringable|string>
* A list of accepted HTML tags, which can be used to wrapping field items.
*/
public function getItemTagOptions(): array {
return [
'' => $this->t('-- NONE --'),
'div' => 'DIV',
'span' => 'SPAN',
'p' => 'P',
];
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return [
'item_tag' => 'div',
'item_classes' => [],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$elements = parent::buildConfigurationForm($form, $form_state);
$elements['item_tag'] = [
'#type' => 'select',
'#title' => $this->t('Field item HTML element'),
'#options' => $this->getItemTagOptions(),
'#default_value' => $this->configuration['item_tag'],
'#description' => $this->t('HTML tag for wrapping each individual field item.'),
];
$elements['item_classes'] = [
'#type' => 'css_class',
'#title' => $this->t('Field 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 : array_merge($classNames, $attributes['class']);
}
return $attributes;
}
/**
* {@inheritdoc}
*/
public function preprocess(array $element, FieldItemListInterface $items, array &$variables): void {
parent::preprocess($element, $items, $variables);
// Determine what, if any, should be the HTML tag around each field item.
if ($tag = $this->configuration['item_tag']) {
// Ensure that the item tag is an allowed HTML tag or default to "div".
$variables['item_tag'] = isset($this->getItemTagOptions()[$tag]) ? $tag : 'div';
}
else {
$variables['item_tag'] = FALSE;
}
if ($variables['wrapper_tag']) {
$variables['items_wrapper_tag'] = 'div';
$variables['items_wrapper_attributes'] = new Attribute();
}
$variables['items'] = [];
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),
];
}
}
}
