View source
<?php
namespace Drupal\workflow\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountInterface;
use Drupal\workflow\Entity\WorkflowTransition;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkflowStateHistoryFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $storage;
protected $currentUser;
protected $viewBuilder;
protected $entityTypeManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->viewBuilder = $entity_type_manager
->getViewBuilder('workflow_transition');
$this->storage = $entity_type_manager
->getStorage('workflow_transition');
$this->currentUser = $current_user;
$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('current_user'), $container
->get('entity_type.manager'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$output = [];
$field_name = $this->fieldDefinition
->getName();
$entity = $items
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$limit = 10;
$workflowTransitions = WorkflowTransition::loadMultipleByProperties($entity_type, [
$entity
->id(),
], [], $field_name, '', $limit, 'DESC');
if ($workflowTransitions) {
$workflowTransitionsViews = [];
foreach ($workflowTransitions as $workflowTransition) {
$workflowTransitionsView = $this->viewBuilder
->view($workflowTransition, 'default');
$workflowTransitionsViews[] = $workflowTransitionsView;
}
$output[] = [
'#theme' => 'item_list',
'#items' => $workflowTransitionsViews,
];
}
else {
$output[] = [
'#markup' => Markup::create($this
->t('Empty')),
];
}
return $output;
}
}