Merge branch '2.x-id_tag_template' into 2.x

This commit is contained in:
Michael Stenta 2021-06-07 12:28:58 -04:00
commit 15d584885d
5 changed files with 168 additions and 3 deletions

View File

@ -0,0 +1,4 @@
/* Add padding between id-tag field items. */
.field--type-id-tag .field__item:not(:last-child) {
padding-bottom: 1em;
}

View File

@ -0,0 +1,4 @@
id_tag_field:
css:
theme:
css/id_tag_field.css: { }

View File

@ -26,3 +26,15 @@ function farm_id_tag_type_allowed_values($bundle) {
}
return $allowed_values;
}
/**
* Implements hook_theme().
*/
function farm_id_tag_theme() {
return [
'field__id_tag' => [
'template' => 'field--id-tag',
'base hook' => 'field',
],
];
}

View File

@ -2,8 +2,11 @@
namespace Drupal\farm_id_tag\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'id tag' formatter.
@ -18,6 +21,54 @@ use Drupal\Core\Field\FormatterBase;
*/
class IdTagFormatter extends FormatterBase {
/**
* The tag_type entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $tagTypeStorage;
/**
* Constructs an IdTagFormatter object.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->tagTypeStorage = $entity_type_manager->getStorage('tag_type');
}
/**
* {@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'),
);
}
/**
* {@inheritdoc}
*/
@ -25,11 +76,30 @@ class IdTagFormatter extends FormatterBase {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta]['id'] = ['#markup' => $item->id];
$elements[$delta]['type'] = ['#markup' => $item->type];
$elements[$delta]['location'] = ['#markup' => $item->location];
// Render the ID if it exists.
if (!empty($item->id)) {
$elements[$delta]['id'] = [
'#markup' => $this->t('ID: @value', ['@value' => $item->id]),
];
}
// Render the type if it exists. Use the tag_type label.
if (!empty($item->type) && $tag_type = $this->tagTypeStorage->load($item->type)) {
$elements[$delta]['type'] = [
'#markup' => $this->t('Type: @value', ['@value' => $tag_type->label()]),
];
}
// Render the location if it exists.
if (!empty($item->location)) {
$elements[$delta]['location'] = [
'#markup' => $this->t('Location: @value', ['@value' => $item->location]),
];
}
}
$elements['#attached']['library'][] = 'farm_id_tag/id_tag_field';
return $elements;
}

View File

@ -0,0 +1,75 @@
{#
/**
* @file
* Theme override for the id-tag field type.
*
* Available variables:
* - attributes: HTML attributes for the containing element.
* - label_hidden: Whether to show the field label or not.
* - title_attributes: HTML attributes for the title.
* - label: The label for the field.
* - multiple: TRUE if a field can contain multiple items.
* - items: List of all the field items. Each item contains:
* - attributes: List of HTML attributes for each item.
* - content: The field item's content.
* - id: The ID of the tag (optional).
* - type: The type the tag (optional).
* - location: The location of the tag (optional).
* - entity_type: The entity type to which the field belongs.
* - field_name: The name of the field.
* - field_type: The type of the field.
* - label_display: The display settings for the label.
*
*
* @see template_preprocess_field()
*/
#}
{%
set classes = [
'field',
'field--name-' ~ field_name|clean_class,
'field--type-' ~ field_type|clean_class,
'field--label-' ~ label_display,
label_display == 'inline' ? 'clearfix',
]
%}
{%
set title_classes = [
'field__label',
label_display == 'visually_hidden' ? 'visually-hidden',
]
%}
{# Use a macro to render each id-tag item. Each property is wrapped in its own div. #}
{% macro id_tag(item) %}
{% for property in item.content %}
<div class="id-tag__{{ _key }}">{{ property }}</div>
{% endfor %}
{% endmacro %}
{% if label_hidden %}
{% if multiple %}
<div{{ attributes.addClass(classes, 'field__items') }}>
{% for item in items %}
<div{{ item.attributes.addClass('field__item') }}>{{ _self.id_tag(item) }}</div>
{% endfor %}
</div>
{% else %}
{% for item in items %}
<div{{ attributes.addClass(classes, 'field__item') }}>{{ _self.id_tag(item) }}</div>
{% endfor %}
{% endif %}
{% else %}
<div{{ attributes.addClass(classes) }}>
<div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
{% if multiple %}
<div class="field__items">
{% endif %}
{% for item in items %}
<div{{ item.attributes.addClass('field__item') }}>{{ _self.id_tag(item) }}</div>
{% endfor %}
{% if multiple %}
</div>
{% endif %}
</div>
{% endif %}