function WorkspaceListBuilder::offCanvasRender

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder::offCanvasRender()
  2. 8.9.x core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder::offCanvasRender()
  3. 10 core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder::offCanvasRender()

Renders the off canvas elements.

Parameters

array $build: A render array.

1 call to WorkspaceListBuilder::offCanvasRender()
WorkspaceListBuilder::render in core/modules/workspaces/src/WorkspaceListBuilder.php
Builds the entity listing as renderable array for table.html.twig.

File

core/modules/workspaces/src/WorkspaceListBuilder.php, line 253

Class

WorkspaceListBuilder
Defines a class to build a listing of workspace entities.

Namespace

Drupal\workspaces

Code

protected function offCanvasRender(array &$build) {
    $active_workspace = $this->workspaceManager
        ->getActiveWorkspace();
    if ($active_workspace) {
        $active_workspace_classes = [
            'active-workspace--not-default',
            'active-workspace--' . $active_workspace->id(),
        ];
    }
    else {
        $active_workspace_classes = [
            'active-workspace--default',
        ];
    }
    $build['active_workspace'] = [
        '#type' => 'container',
        '#weight' => -20,
        '#attributes' => [
            'class' => array_merge([
                'active-workspace',
            ], $active_workspace_classes),
        ],
        'title' => [
            '#type' => 'html_tag',
            '#tag' => 'div',
            '#value' => $this->t('Current workspace:'),
            '#attributes' => [
                'class' => 'active-workspace__title',
            ],
        ],
        'label' => [
            '#type' => 'container',
            '#attributes' => [
                'class' => 'active-workspace__label',
            ],
            'value' => [
                '#type' => 'html_tag',
                '#tag' => 'span',
                '#value' => $active_workspace ? $active_workspace->label() : $this->t('Live'),
            ],
        ],
    ];
    if ($active_workspace) {
        $build['active_workspace']['label']['manage'] = [
            '#type' => 'link',
            '#title' => $this->t('Manage workspace'),
            '#url' => $active_workspace->toUrl('canonical'),
            '#attributes' => [
                'class' => [
                    'active-workspace__manage',
                ],
            ],
        ];
        $build['active_workspace']['actions'] = [
            '#type' => 'container',
            '#weight' => 20,
            '#attributes' => [
                'class' => [
                    'active-workspace__actions',
                ],
            ],
        ];
        if (!$active_workspace->hasParent()) {
            $build['active_workspace']['actions']['publish'] = [
                '#type' => 'link',
                '#title' => $this->t('Publish content'),
                '#url' => Url::fromRoute('entity.workspace.publish_form', [
                    'workspace' => $active_workspace->id(),
                ], [
                    'query' => [
                        'destination' => $active_workspace->toUrl('collection')
                            ->toString(),
                    ],
                ]),
                '#attributes' => [
                    'class' => [
                        'button',
                        'button--primary',
                        'active-workspace__button',
                    ],
                ],
            ];
        }
        else {
            $build['active_workspace']['actions']['merge'] = [
                '#type' => 'link',
                '#title' => $this->t('Merge content'),
                '#url' => Url::fromRoute('entity.workspace.merge_form', [
                    'source_workspace' => $active_workspace->id(),
                    'target_workspace' => $active_workspace->parent->target_id,
                ], [
                    'query' => [
                        'destination' => $active_workspace->toUrl('collection')
                            ->toString(),
                    ],
                ]),
                '#attributes' => [
                    'class' => [
                        'button',
                        'button--primary',
                        'active-workspace__button',
                    ],
                ],
            ];
        }
    }
    $items = [];
    $rows = array_slice($build['table']['#rows'], 0, 5, TRUE);
    foreach ($rows as $id => $row) {
        if (!$active_workspace || $active_workspace->id() !== $id) {
            $url = Url::fromRoute('entity.workspace.activate_form', [
                'workspace' => $id,
            ], [
                'query' => $this->getDestinationArray(),
            ]);
            $items[] = [
                '#type' => 'link',
                '#title' => ltrim($row['data']['label']['data']['#title']),
                '#url' => $url,
                '#attributes' => [
                    'class' => [
                        'use-ajax',
                        'workspaces__item',
                        'workspaces__item--not-default',
                    ],
                    'data-dialog-type' => 'modal',
                    'data-dialog-options' => Json::encode([
                        'width' => 500,
                    ]),
                ],
            ];
        }
    }
    // Add an item for switching to Live.
    if ($active_workspace) {
        $items[] = [
            '#type' => 'link',
            '#title' => $this->t('Live'),
            '#url' => Url::fromRoute('workspaces.switch_to_live', [], [
                'query' => $this->getDestinationArray(),
            ]),
            '#attributes' => [
                'class' => [
                    'use-ajax',
                    'workspaces__item',
                    'workspaces__item--default',
                ],
                'data-dialog-type' => 'modal',
                'data-dialog-options' => Json::encode([
                    'width' => 500,
                ]),
            ],
        ];
    }
    $build['workspaces_list'] = [
        '#type' => 'container',
        '#attributes' => [
            'class' => 'workspaces',
        ],
    ];
    $build['workspaces_list']['workspaces'] = [
        '#theme' => 'item_list',
        '#title' => $this->t('Other workspaces:'),
        '#items' => $items,
        '#wrapper_attributes' => [
            'class' => [
                'workspaces__list',
            ],
        ],
        '#cache' => [
            'contexts' => $this->entityType
                ->getListCacheContexts(),
            'tags' => $this->entityType
                ->getListCacheTags(),
        ],
    ];
    $build['workspaces_list']['all_workspaces'] = [
        '#type' => 'link',
        '#title' => $this->t('View all workspaces'),
        '#url' => Url::fromRoute('entity.workspace.collection'),
        '#attributes' => [
            'class' => [
                'all-workspaces',
            ],
        ],
    ];
    unset($build['table']);
    unset($build['pager']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.