Render the tag_type label.

This commit is contained in:
paul121 2021-06-04 10:25:41 -07:00 committed by Michael Stenta
parent bda93d4373
commit 68a06cbf01
1 changed files with 54 additions and 3 deletions

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}
*/
@ -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()]),
];
}