View source
<?php
namespace Drupal\gdpr_consent\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;
class ConsentFormatter extends FormatterBase {
protected $entityTypeManager;
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->entityTypeManager = $entity_type_manager;
}
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'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$output = [];
$storage = $this->entityTypeManager
->getStorage('gdpr_consent_agreement');
foreach ($items as $delta => $item) {
$agreement = $storage
->loadRevision($item->target_revision_id);
$link = $this
->t('Removed');
if ($agreement) {
$link = $agreement
->toLink($agreement->title->value, 'revision')
->toString();
}
$output[$delta] = [
'name' => [
'#markup' => $this
->t('@title on @date', [
'@title' => $link,
'@date' => $item->date,
]),
],
];
}
return $output;
}
}