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/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..2872d7d6 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} */ @@ -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; } 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..4af519f9 --- /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 %}