From 3bc1871e392a623d83fa223c4c8712b6db255e5f Mon Sep 17 00:00:00 2001 From: paul121 Date: Fri, 4 Jun 2021 10:18:56 -0700 Subject: [PATCH 1/4] Render each ID tag property on a separate line. --- modules/core/id_tag/farm_id_tag.module | 12 +++ .../Field/FieldFormatter/IdTagFormatter.php | 24 +++++- .../id_tag/templates/field--id-tag.html.twig | 75 +++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 modules/core/id_tag/templates/field--id-tag.html.twig diff --git a/modules/core/id_tag/farm_id_tag.module b/modules/core/id_tag/farm_id_tag.module index 0e3717ee..2c3a3224 100644 --- a/modules/core/id_tag/farm_id_tag.module +++ b/modules/core/id_tag/farm_id_tag.module @@ -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', + ], + ]; +} diff --git a/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php b/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php index ce95f240..830e32ae 100644 --- a/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php +++ b/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php @@ -25,9 +25,27 @@ 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. + if (!empty($item->type)) { + $elements[$delta]['type'] = [ + '#markup' => $this->t('Type: @value', ['@value' => $item->type]), + ]; + } + + // Render the location if it exists. + if (!empty($item->location)) { + $elements[$delta]['location'] = [ + '#markup' => $this->t('Location: @value', ['@value' => $item->location]), + ]; + } } return $elements; diff --git a/modules/core/id_tag/templates/field--id-tag.html.twig b/modules/core/id_tag/templates/field--id-tag.html.twig new file mode 100644 index 00000000..d8680cde --- /dev/null +++ b/modules/core/id_tag/templates/field--id-tag.html.twig @@ -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 %} +
{{ property }}
+ {% endfor %} +{% endmacro %} + +{% if label_hidden %} + {% if multiple %} + + {% for item in items %} + {{ _self.id_tag(item) }} + {% endfor %} + + {% else %} + {% for item in items %} + {{ _self.id_tag(item) }} + {% endfor %} + {% endif %} +{% else %} + + {{ label }} + {% if multiple %} +
+ {% endif %} + {% for item in items %} + {{ _self.id_tag(item) }}
+ {% endfor %} + {% if multiple %} + + {% endif %} + +{% endif %} From bda93d43732de08db4200b2d3488a037240a1e7d Mon Sep 17 00:00:00 2001 From: paul121 Date: Fri, 4 Jun 2021 10:20:16 -0700 Subject: [PATCH 2/4] Add "id-tag__{property}" class to each div. --- modules/core/id_tag/templates/field--id-tag.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/id_tag/templates/field--id-tag.html.twig b/modules/core/id_tag/templates/field--id-tag.html.twig index d8680cde..4af519f9 100644 --- a/modules/core/id_tag/templates/field--id-tag.html.twig +++ b/modules/core/id_tag/templates/field--id-tag.html.twig @@ -43,7 +43,7 @@ {# 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 %} -
{{ property }}
+
{{ property }}
{% endfor %} {% endmacro %} From 68a06cbf01c28d73ccb0314259e81db32356e3fb Mon Sep 17 00:00:00 2001 From: paul121 Date: Fri, 4 Jun 2021 10:25:41 -0700 Subject: [PATCH 3/4] Render the tag_type label. --- .../Field/FieldFormatter/IdTagFormatter.php | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php b/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php index 830e32ae..dec9043a 100644 --- a/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php +++ b/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php @@ -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} */ @@ -33,10 +84,10 @@ class IdTagFormatter extends FormatterBase { ]; } - // Render the type if it exists. - if (!empty($item->type)) { + // 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' => $item->type]), + '#markup' => $this->t('Type: @value', ['@value' => $tag_type->label()]), ]; } From 0809f644d4352689149f6051ad27d35624645519 Mon Sep 17 00:00:00 2001 From: paul121 Date: Fri, 4 Jun 2021 10:33:49 -0700 Subject: [PATCH 4/4] Add padding between id-tag field items. --- modules/core/id_tag/css/id_tag_field.css | 4 ++++ modules/core/id_tag/farm_id_tag.libraries.yml | 4 ++++ .../id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php | 1 + 3 files changed, 9 insertions(+) create mode 100644 modules/core/id_tag/css/id_tag_field.css create mode 100644 modules/core/id_tag/farm_id_tag.libraries.yml diff --git a/modules/core/id_tag/css/id_tag_field.css b/modules/core/id_tag/css/id_tag_field.css new file mode 100644 index 00000000..fe0d5211 --- /dev/null +++ b/modules/core/id_tag/css/id_tag_field.css @@ -0,0 +1,4 @@ +/* Add padding between id-tag field items. */ +.field--type-id-tag .field__item:not(:last-child) { + padding-bottom: 1em; +} diff --git a/modules/core/id_tag/farm_id_tag.libraries.yml b/modules/core/id_tag/farm_id_tag.libraries.yml new file mode 100644 index 00000000..d30519d6 --- /dev/null +++ b/modules/core/id_tag/farm_id_tag.libraries.yml @@ -0,0 +1,4 @@ +id_tag_field: + css: + theme: + css/id_tag_field.css: { } diff --git a/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php b/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php index dec9043a..2872d7d6 100644 --- a/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php +++ b/modules/core/id_tag/src/Plugin/Field/FieldFormatter/IdTagFormatter.php @@ -99,6 +99,7 @@ class IdTagFormatter extends FormatterBase { } } + $elements['#attached']['library'][] = 'farm_id_tag/id_tag_field'; return $elements; }