56ab1c5e43d37707748e547494bec76bf64626d7
[yaffs-website] / web / core / modules / workspaces / src / WorkspaceListBuilder.php
1 <?php
2
3 namespace Drupal\workspaces;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Ajax\AjaxHelperTrait;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityListBuilder;
9 use Drupal\Core\Entity\EntityStorageInterface;
10 use Drupal\Core\Entity\EntityTypeInterface;
11 use Drupal\Core\Url;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a class to build a listing of workspace entities.
16  *
17  * @see \Drupal\workspaces\Entity\Workspace
18  */
19 class WorkspaceListBuilder extends EntityListBuilder {
20
21   use AjaxHelperTrait;
22
23   /**
24    * The workspace manager service.
25    *
26    * @var \Drupal\workspaces\WorkspaceManagerInterface
27    */
28   protected $workspaceManager;
29
30   /**
31    * Constructs a new EntityListBuilder object.
32    *
33    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
34    *   The entity type definition.
35    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
36    *   The entity storage class.
37    * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
38    *   The workspace manager service.
39    */
40   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, WorkspaceManagerInterface $workspace_manager) {
41     parent::__construct($entity_type, $storage);
42     $this->workspaceManager = $workspace_manager;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
49     return new static(
50       $entity_type,
51       $container->get('entity.manager')->getStorage($entity_type->id()),
52       $container->get('workspaces.manager')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function buildHeader() {
60     $header['label'] = $this->t('Workspace');
61     $header['uid'] = $this->t('Owner');
62
63     return $header + parent::buildHeader();
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function buildRow(EntityInterface $entity) {
70     /** @var \Drupal\workspaces\WorkspaceInterface $entity */
71     $row['data'] = [
72       'label' => $entity->label(),
73       'owner' => $entity->getOwner()->getDisplayname(),
74     ];
75     $row['data'] = $row['data'] + parent::buildRow($entity);
76
77     $active_workspace = $this->workspaceManager->getActiveWorkspace();
78     if ($entity->id() === $active_workspace->id()) {
79       $row['class'] = 'active-workspace';
80     }
81     return $row;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getDefaultOperations(EntityInterface $entity) {
88     /** @var \Drupal\workspaces\WorkspaceInterface $entity */
89     $operations = parent::getDefaultOperations($entity);
90     if (isset($operations['edit'])) {
91       $operations['edit']['query']['destination'] = $entity->toUrl('collection')->toString();
92     }
93
94     $active_workspace = $this->workspaceManager->getActiveWorkspace();
95     if ($entity->id() != $active_workspace->id()) {
96       $operations['activate'] = [
97         'title' => $this->t('Switch to @workspace', ['@workspace' => $entity->label()]),
98         // Use a weight lower than the one of the 'Edit' operation because we
99         // want the 'Activate' operation to be the primary operation.
100         'weight' => 0,
101         'url' => $entity->toUrl('activate-form', ['query' => ['destination' => $entity->toUrl('collection')->toString()]]),
102       ];
103     }
104
105     if (!$entity->isDefaultWorkspace()) {
106       $operations['deploy'] = [
107         'title' => $this->t('Deploy content'),
108         // The 'Deploy' operation should be the default one for the currently
109         // active workspace.
110         'weight' => ($entity->id() == $active_workspace->id()) ? 0 : 20,
111         'url' => $entity->toUrl('deploy-form', ['query' => ['destination' => $entity->toUrl('collection')->toString()]]),
112       ];
113     }
114
115     return $operations;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function load() {
122     $entities = parent::load();
123     // Make the active workspace more visible by moving it first in the list.
124     $active_workspace = $this->workspaceManager->getActiveWorkspace();
125     $entities = [$active_workspace->id() => $entities[$active_workspace->id()]] + $entities;
126     return $entities;
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function render() {
133     $build = parent::render();
134     if ($this->isAjax()) {
135       $this->offCanvasRender($build);
136     }
137     else {
138       $build['#attached'] = [
139         'library' => ['workspaces/drupal.workspaces.overview'],
140       ];
141     }
142     return $build;
143   }
144
145   /**
146    * Renders the off canvas elements.
147    *
148    * @param array $build
149    *   A render array.
150    */
151   protected function offCanvasRender(array &$build) {
152     $active_workspace = $this->workspaceManager->getActiveWorkspace();
153     $row_count = count($build['table']['#rows']);
154     $build['active_workspace'] = [
155       '#type' => 'container',
156       '#weight' => -20,
157       '#attributes' => [
158         'class' => [
159           'active-workspace',
160           $active_workspace->isDefaultWorkspace() ? 'active-workspace--default' : 'active-workspace--not-default',
161           'active-workspace--' . $active_workspace->id(),
162         ],
163       ],
164       'label' => [
165         '#type' => 'label',
166         '#prefix' => '<div class="active-workspace__title">' . $this->t('Current workspace:') . '</div>',
167         '#title' => $active_workspace->label(),
168         '#title_display' => '',
169         '#attributes' => ['class' => 'active-workspace__label'],
170       ],
171       'manage' => [
172         '#type' => 'link',
173         '#title' => $this->t('Manage workspaces'),
174         '#url' => $active_workspace->toUrl('collection'),
175         '#attributes' => [
176           'class' => ['active-workspace__manage'],
177         ],
178       ],
179     ];
180     if (!$active_workspace->isDefaultWorkspace()) {
181       $build['active_workspace']['actions'] = [
182         '#type' => 'container',
183         '#weight' => 20,
184         '#attributes' => [
185           'class' => ['active-workspace__actions'],
186         ],
187         'deploy' => [
188           '#type' => 'link',
189           '#title' => $this->t('Deploy content'),
190           '#url' => $active_workspace->toUrl('deploy-form', ['query' => ['destination' => $active_workspace->toUrl('collection')->toString()]]),
191           '#attributes' => [
192             'class' => ['button', 'active-workspace__button'],
193           ],
194         ],
195       ];
196     }
197     if ($row_count > 2) {
198       $build['all_workspaces'] = [
199         '#type' => 'link',
200         '#title' => $this->t('View all @count workspaces', ['@count' => $row_count]),
201         '#url' => $active_workspace->toUrl('collection'),
202         '#attributes' => [
203           'class' => ['all-workspaces'],
204         ],
205       ];
206     }
207     $items = [];
208     $rows = array_slice($build['table']['#rows'], 0, 5, TRUE);
209     foreach ($rows as $id => $row) {
210       if ($active_workspace->id() !== $id) {
211         $url = Url::fromRoute('entity.workspace.activate_form', ['workspace' => $id]);
212         $default_class = $id === WorkspaceInterface::DEFAULT_WORKSPACE ? 'workspaces__item--default' : 'workspaces__item--not-default';
213         $items[] = [
214           '#type' => 'link',
215           '#title' => $row['data']['label'],
216           '#url' => $url,
217           '#attributes' => [
218             'class' => ['use-ajax', 'workspaces__item', $default_class],
219             'data-dialog-type' => 'modal',
220             'data-dialog-options' => Json::encode([
221               'width' => 500,
222             ]),
223           ],
224         ];
225       }
226     }
227     $build['workspaces'] = [
228       '#theme' => 'item_list',
229       '#items' => $items,
230       '#wrapper_attributes' => ['class' => ['workspaces']],
231       '#cache' => [
232         'contexts' => $this->entityType->getListCacheContexts(),
233         'tags' => $this->entityType->getListCacheTags(),
234       ],
235     ];
236     unset($build['table']);
237     unset($build['pager']);
238   }
239
240 }