Skip to content
Snippets Groups Projects
Commit fc63a963 authored by NGUYEN Bao's avatar NGUYEN Bao
Browse files

add formatter list

parent 4dc39834
Branches
Tags
No related merge requests found
Pipeline #414191 passed with warnings
......@@ -41,36 +41,47 @@ field.widget.settings.input_pattern:
field.formatter.settings.file_attributes:
type: mapping
label: 'Input attributes display format settings'
label: File attributes display format settings
mapping:
attributes:
type: string
label: 'Attributes'
label: Attributes
attached:
type: boolean
label: 'Attached libraries'
type: string
label: Attached libraries
field.formatter.settings.image_attributes:
type: mapping
label: 'Input attributes display format settings'
label: Image attributes display format settings
mapping:
attributes:
type: string
label: 'Attributes'
label: Attributes
attached:
type: boolean
label: 'Attached libraries'
type: string
label: Attached libraries
field.formatter.settings.text_attributes:
type: mapping
label: 'Input attributes display format settings'
label: Text attributes display format settings
mapping:
attributes:
type: string
label: Attributes
attached:
type: string
label: Attached libraries
field.formatter.settings.list_attributes:
type: mapping
label: List attributes display format settings
mapping:
wrapper_attributes:
type: string
label: 'Wrapper attributes'
label: Wrapper attributes
attributes:
type: string
label: 'Attributes'
label: Attributes
attached:
type: boolean
label: 'Attached libraries'
type: string
label: Attached libraries
<?php
namespace Drupal\input_pattern\Plugin\Field\FieldFormatter;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Environment as Twig_Environment;
/**
* Plugin implementation of the 'string_attributes' formatter.
*/
#[FieldFormatter(
id: 'list_attributes',
label: new TranslatableMarkup('List attributes'),
field_types: [
'uri',
'email',
'string',
'string_long',
],
)]
class ListAttributesFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, protected $entityTypeManager, protected EntityDisplayRepositoryInterface $entityDisplayRepository, protected Twig_Environment $twig) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('entity_type.manager'),
$container->get('entity_display.repository'),
$container->get('twig'),
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'list_type' => 'ul',
'wrapper_attributes' => '',
'attributes' => '',
'attached' => '',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['list_type'] = [
'#title' => $this->t('Style options'),
'#type' => 'select',
'#default_value' => $this->getSetting('list_type'),
'#options' => [
'ul' => $this->t('Unordered list'),
'ol' => $this->t('Ordered list'),
],
];
$element['wrapper_attributes'] = [
'#type' => 'textarea',
'#title' => $this->t('Wrapper HTML element'),
'#description' => $this->t('Each variable is a new line. Can use with twig.'),
'#default_value' => $this->getSetting('wrapper_attributes'),
];
$element['attributes'] = [
'#type' => 'textarea',
'#title' => $this->t('Customize field HTML'),
'#description' => $this->t('Each variable is a new line. Can use with twig.'),
'#default_value' => $this->getSetting('attributes'),
];
$element['attached'] = [
'#type' => 'textarea',
'#title' => $this->t('Attached libraries'),
'#description' => $this->t('Each library is a new line.'),
'#default_value' => $this->getSetting('attached'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
if (!empty($this->getSetting('wrapper_attributes'))) {
$summary[] = $this->t('Wrapper attributes: @attribute', [
'@attribute' => $this->getSetting('wrapper_attributes'),
]);
}
if (!empty($this->getSetting('attributes'))) {
$summary[] = $this->t('Attributes: @attribute', [
'@attribute' => $this->getSetting('attributes'),
]);
}
if (!empty($this->getSetting('attached'))) {
$summary[] = $this->t('Libraries: @attached', [
'@attached' => $this->getSetting('attached'),
]);
}
return array_merge($summary, parent::settingsSummary());
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$type = $items->getFieldDefinition()->getType();
$entity = $items->getEntity();
$entity_type = $entity->getEntityType();
$settings = $this->getSettings();
$display = $this->entityDisplayRepository->getViewDisplay($entity->getEntityTypeId(), $entity->bundle(), 'default')->getComponent($items->getName());
$elements = [
'#theme' => 'item_list',
'#list_type' => $this->getSetting('list_type'),
'#items' => [],
];
if (!in_array($display['label'], ['hidden', 'visually_hidden'])) {
$elements['#title'] = $items->getFieldDefinition()->getLabel();
}
$render_as_link = FALSE;
if ($this->getSetting('link_to_entity') && !$entity->isNew() && $entity_type->hasLinkTemplate('canonical')) {
$url = $this->getEntityUrl($entity);
$access = $url->access(return_as_object: TRUE);
(new CacheableMetadata())->addCacheableDependency($access)->applyTo($elements);
$render_as_link = $access->isAllowed();
}
foreach ($items as $delta => $item) {
if ($type == 'email') {
$url = Url::fromUri('mailto:' . $item->value);
}
if ($type == 'uri') {
$url = Url::fromUri($item->value);
}
if ($render_as_link) {
assert(isset($url));
$elements['#items'][$delta] = [
'#type' => 'link',
'#title' => $item->value,
'#url' => $url,
];
}
else {
$elements['#items'][$delta] = $item->value;
}
}
if (!empty($settings['wrapper_attributes'])) {
$attributes = $this->extractAttributes($items, $settings['wrapper_attributes']);
foreach ($attributes as $name => $value) {
if ($value != '') {
if ($name != 'class') {
$elements['#attributes'][$name] = $value;
}
else {
$elements['#attributes'][$name][] = $value;
}
}
}
}
if (!empty($settings['attributes'])) {
$attributes = $this->extractAttributes($items, $settings['attributes']);
foreach ($items as $delta => $item) {
foreach ($attributes as $name => $value) {
if (!empty($value)) {
$elements['#items'][$delta] = [
'#markup' => $item->value,
];
if ($name != 'class') {
$elements['#items'][$delta]['#wrapper_attributes'][$name] = $value;
}
else {
$elements['#items'][$delta]['#wrapper_attributes'][$name][] = $value;
}
}
}
}
}
if (!empty($settings['attached'])) {
$attached = explode(',', str_replace(PHP_EOL, ',', $settings['attached']));
$elements['#attached']['library'] = [...$attached, ...($elements['#attached']['library'] ?? [])];
}
return $elements;
}
/**
* {@inheritdoc}
*/
protected function extractAttributes($items, $settingAttr) {
$attributes = [];
$attrs = explode(PHP_EOL, $settingAttr);
$context = [
'entity' => $items->getEntity() ?? '',
'field_name' => $items->getName(),
];
foreach ($attrs as $attribute) {
if (empty(trim($attribute))) {
continue;
}
$variables = explode('=', $attribute);
$key = trim(str_replace(['"', "'", ' '], ['', '', '-'], $variables[0]));
$val = trim(str_replace('"', '', $variables[1] ?? $variables[0]));
$attributes[$key] = $this->twig->renderInline($val, $context);
}
return $attributes;
}
}
......@@ -2,13 +2,10 @@
namespace Drupal\input_pattern\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -31,9 +28,8 @@ class TextAttributesFormatter extends TextDefaultFormatter {
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $currentUser, EntityStorageInterface $imageStyleStorage, FileUrlGeneratorInterface $fileUrlGenerator, protected Twig_Environment $twig) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $currentUser, $imageStyleStorage, $fileUrlGenerator);
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, protected Twig_Environment $twig) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
}
/**
......@@ -48,9 +44,6 @@ class TextAttributesFormatter extends TextDefaultFormatter {
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('current_user'),
$container->get('entity_type.manager')->getStorage('image_style'),
$container->get('file_url_generator'),
$container->get('twig'),
);
}
......@@ -60,7 +53,6 @@ class TextAttributesFormatter extends TextDefaultFormatter {
*/
public static function defaultSettings() {
return [
'wrapper_attributes' => '',
'attributes' => '',
'attached' => '',
] + parent::defaultSettings();
......@@ -71,13 +63,6 @@ class TextAttributesFormatter extends TextDefaultFormatter {
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['wrapper_attributes'] = [
'#type' => 'textarea',
'#title' => $this->t('Wrapper HTML element'),
'#description' => $this->t('Each variable is a new line. Can use with twig.'),
'#default_value' => $this->getSetting('attributes'),
];
$element['attributes'] = [
'#type' => 'textarea',
'#title' => $this->t('Customize field HTML'),
......@@ -98,11 +83,6 @@ class TextAttributesFormatter extends TextDefaultFormatter {
*/
public function settingsSummary() {
$summary = [];
if (!empty($this->getSetting('wrapper_attributes'))) {
$summary[] = $this->t('Wrapper attributes: @attribute', [
'@attribute' => $this->getSetting('wrapper_attributes'),
]);
}
if (!empty($this->getSetting('attributes'))) {
$summary[] = $this->t('Attributes: @attribute', [
'@attribute' => $this->getSetting('attributes'),
......@@ -122,8 +102,8 @@ class TextAttributesFormatter extends TextDefaultFormatter {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$settings = $this->getSettings();
if (!empty($settings['wrapper_attributes'])) {
$attributes = $this->extractAttributes($items, $settings['wrapper_attributes']);
if (!empty($settings['attributes'])) {
$attributes = $this->extractAttributes($items, $settings['attributes']);
foreach ($attributes as $name => $value) {
if ($value != '') {
if ($name != 'class') {
......@@ -135,23 +115,6 @@ class TextAttributesFormatter extends TextDefaultFormatter {
}
}
}
if (!empty($settings['attributes'])) {
$attributes = $this->extractAttributes($items, $settings['attributes']);
foreach ($items as $delta => $item) {
if (isset($elements[$delta])) {
foreach ($attributes as $name => $value) {
if (!empty($value)) {
if ($name != 'class') {
$elements[$delta]['#attributes'][$name] = $value;
}
else {
$elements[$delta]['#attributes'][$name][] = $value;
}
}
}
}
}
}
if (!empty($settings['attached'])) {
$attached = explode(',', str_replace(PHP_EOL, ',', $settings['attached']));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment