Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / workflows / src / WorkflowListBuilder.php
1 <?php
2
3 namespace Drupal\workflows;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides a listing of Workflow entities.
14  */
15 class WorkflowListBuilder extends ConfigEntityListBuilder {
16
17   /**
18    * The workflow type plugin manager.
19    *
20    * @var \Drupal\Component\Plugin\PluginManagerInterface
21    */
22   protected $workflowTypeManager;
23
24   /**
25    * {@inheritdoc}
26    */
27   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
28     return new static(
29       $entity_type,
30       $container->get('entity_type.manager')->getStorage($entity_type->id()),
31       $container->get('plugin.manager.workflows.type')
32     );
33   }
34
35   /**
36    * Constructs a new WorkflowListBuilder object.
37    *
38    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
39    *   The entity type definition.
40    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
41    *   The entity storage class.
42    * @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_manager
43    *   The workflow type plugin manager.
44    */
45   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, PluginManagerInterface $workflow_type_manager) {
46     parent::__construct($entity_type, $storage);
47     $this->workflowTypeManager = $workflow_type_manager;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getFormId() {
54     return 'workflow_admin_overview_form';
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function buildHeader() {
61     $header['label'] = $this->t('Workflow');
62     $header['type'] = $this->t('Type');
63     $header['states'] = $this->t('States');
64
65     return $header + parent::buildHeader();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function buildRow(EntityInterface $entity) {
72     /** @var \Drupal\workflows\WorkflowInterface $entity */
73     $row['label'] = $entity->label();
74
75     $row['type']['data'] = [
76       '#markup' => $entity->getTypePlugin()->label(),
77     ];
78
79     $items = array_map([State::class, 'labelCallback'], $entity->getTypePlugin()->getStates());
80     $row['states']['data'] = [
81       '#theme' => 'item_list',
82       '#context' => ['list_style' => 'comma-list'],
83       '#items' => $items,
84     ];
85
86     return $row + parent::buildRow($entity);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function render() {
93     $build = parent::render();
94     $workflow_types_count = count($this->workflowTypeManager->getDefinitions());
95     if ($workflow_types_count === 0) {
96       $build['table']['#empty'] = $this->t('There are no workflow types available. In order to create workflows you need to install a module that provides a workflow type. For example, the <a href=":content-moderation">Content Moderation</a> module provides a workflow type that enables workflows for content entities.', [':content-moderation' => '/admin/modules#module-content-moderation']);
97     }
98     return $build;
99   }
100
101 }