3 namespace Drupal\workspaces;
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;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a class to build a listing of workspace entities.
17 * @see \Drupal\workspaces\Entity\Workspace
19 class WorkspaceListBuilder extends EntityListBuilder {
24 * The workspace manager service.
26 * @var \Drupal\workspaces\WorkspaceManagerInterface
28 protected $workspaceManager;
31 * Constructs a new EntityListBuilder object.
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.
40 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, WorkspaceManagerInterface $workspace_manager) {
41 parent::__construct($entity_type, $storage);
42 $this->workspaceManager = $workspace_manager;
48 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
51 $container->get('entity.manager')->getStorage($entity_type->id()),
52 $container->get('workspaces.manager')
59 public function buildHeader() {
60 $header['label'] = $this->t('Workspace');
61 $header['uid'] = $this->t('Owner');
63 return $header + parent::buildHeader();
69 public function buildRow(EntityInterface $entity) {
70 /** @var \Drupal\workspaces\WorkspaceInterface $entity */
72 'label' => $entity->label(),
73 'owner' => $entity->getOwner()->getDisplayname(),
75 $row['data'] = $row['data'] + parent::buildRow($entity);
77 $active_workspace = $this->workspaceManager->getActiveWorkspace();
78 if ($entity->id() === $active_workspace->id()) {
79 $row['class'] = 'active-workspace';
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();
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.
101 'url' => $entity->toUrl('activate-form', ['query' => ['destination' => $entity->toUrl('collection')->toString()]]),
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
110 'weight' => ($entity->id() == $active_workspace->id()) ? 0 : 20,
111 'url' => $entity->toUrl('deploy-form', ['query' => ['destination' => $entity->toUrl('collection')->toString()]]),
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;
132 public function render() {
133 $build = parent::render();
134 if ($this->isAjax()) {
135 $this->offCanvasRender($build);
138 $build['#attached'] = [
139 'library' => ['workspaces/drupal.workspaces.overview'],
146 * Renders the off canvas elements.
148 * @param array $build
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',
160 $active_workspace->isDefaultWorkspace() ? 'active-workspace--default' : 'active-workspace--not-default',
161 'active-workspace--' . $active_workspace->id(),
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'],
173 '#title' => $this->t('Manage workspaces'),
174 '#url' => $active_workspace->toUrl('collection'),
176 'class' => ['active-workspace__manage'],
180 if (!$active_workspace->isDefaultWorkspace()) {
181 $build['active_workspace']['actions'] = [
182 '#type' => 'container',
185 'class' => ['active-workspace__actions'],
189 '#title' => $this->t('Deploy content'),
190 '#url' => $active_workspace->toUrl('deploy-form', ['query' => ['destination' => $active_workspace->toUrl('collection')->toString()]]),
192 'class' => ['button', 'active-workspace__button'],
197 if ($row_count > 2) {
198 $build['all_workspaces'] = [
200 '#title' => $this->t('View all @count workspaces', ['@count' => $row_count]),
201 '#url' => $active_workspace->toUrl('collection'),
203 'class' => ['all-workspaces'],
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';
215 '#title' => $row['data']['label'],
218 'class' => ['use-ajax', 'workspaces__item', $default_class],
219 'data-dialog-type' => 'modal',
220 'data-dialog-options' => Json::encode([
227 $build['workspaces'] = [
228 '#theme' => 'item_list',
230 '#wrapper_attributes' => ['class' => ['workspaces']],
232 'contexts' => $this->entityType->getListCacheContexts(),
233 'tags' => $this->entityType->getListCacheTags(),
236 unset($build['table']);
237 unset($build['pager']);