You are here

class WorkspaceListBuilder in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder

Defines a class to build a listing of workspace entities.

Hierarchy

Expanded class hierarchy of WorkspaceListBuilder

See also

\Drupal\workspaces\Entity\Workspace

File

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

Namespace

Drupal\workspaces
View source
class WorkspaceListBuilder extends EntityListBuilder {
  use AjaxHelperTrait;

  /**
   * The workspace manager service.
   *
   * @var \Drupal\workspaces\WorkspaceManagerInterface
   */
  protected $workspaceManager;

  /**
   * The workspace repository service.
   *
   * @var \Drupal\workspaces\WorkspaceRepositoryInterface
   */
  protected $workspaceRepository;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new EntityListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
   *   The workspace manager service.
   * @param \Drupal\workspaces\WorkspaceRepositoryInterface $workspace_repository
   *   The workspace repository service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, WorkspaceManagerInterface $workspace_manager, WorkspaceRepositoryInterface $workspace_repository, RendererInterface $renderer) {
    parent::__construct($entity_type, $storage);
    $this->workspaceManager = $workspace_manager;
    $this->workspaceRepository = $workspace_repository;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('workspaces.manager'), $container
      ->get('workspaces.repository'), $container
      ->get('renderer'));
  }

  /**
   * {@inheritdoc}
   */
  public function load() {

    // Get all the workspace entities and sort them in tree order.
    $workspace_tree = $this->workspaceRepository
      ->loadTree();
    $entities = array_replace($workspace_tree, $this->storage
      ->loadMultiple());
    foreach ($entities as $id => $entity) {
      $entity->_depth = $workspace_tree[$id]['depth'];
    }
    return $entities;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this
      ->t('Workspace');
    $header['uid'] = $this
      ->t('Owner');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /** @var \Drupal\workspaces\WorkspaceInterface $entity */
    if (isset($entity->_depth) && $entity->_depth > 0) {
      $indentation = [
        '#theme' => 'indentation',
        '#size' => $entity->_depth,
      ];
    }
    $row['data'] = [
      'label' => [
        'data' => [
          '#prefix' => isset($indentation) ? $this->renderer
            ->render($indentation) : '',
          '#type' => 'link',
          '#title' => $entity
            ->label(),
          '#url' => $entity
            ->toUrl(),
        ],
      ],
      'owner' => $entity
        ->getOwner()
        ->getDisplayname(),
    ];
    $row['data'] = $row['data'] + parent::buildRow($entity);
    $active_workspace = $this->workspaceManager
      ->getActiveWorkspace();
    if ($active_workspace && $entity
      ->id() === $active_workspace
      ->id()) {
      $row['class'] = [
        'active-workspace',
        'active-workspace--not-default',
      ];
    }
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {

    /** @var \Drupal\workspaces\WorkspaceInterface $entity */
    $operations = parent::getDefaultOperations($entity);
    if (isset($operations['edit'])) {
      $operations['edit']['query']['destination'] = $entity
        ->toUrl('collection')
        ->toString();
    }
    $active_workspace = $this->workspaceManager
      ->getActiveWorkspace();
    if (!$active_workspace || $entity
      ->id() != $active_workspace
      ->id()) {
      $operations['activate'] = [
        'title' => $this
          ->t('Switch to @workspace', [
          '@workspace' => $entity
            ->label(),
        ]),
        // Use a weight lower than the one of the 'Edit' operation because we
        // want the 'Activate' operation to be the primary operation.
        'weight' => 0,
        'url' => $entity
          ->toUrl('activate-form', [
          'query' => [
            'destination' => $entity
              ->toUrl('collection')
              ->toString(),
          ],
        ]),
      ];
    }
    if (!$entity
      ->hasParent()) {
      $operations['publish'] = [
        'title' => $this
          ->t('Publish content'),
        // The 'Publish' operation should be the default one for the currently
        // active workspace.
        'weight' => $active_workspace && $entity
          ->id() == $active_workspace
          ->id() ? 0 : 20,
        'url' => Url::fromRoute('entity.workspace.publish_form', [
          'workspace' => $entity
            ->id(),
        ], [
          'query' => [
            'destination' => $entity
              ->toUrl('collection')
              ->toString(),
          ],
        ]),
      ];
    }
    else {

      /** @var \Drupal\workspaces\WorkspaceInterface $parent */
      $parent = $entity->parent->entity;
      $operations['merge'] = [
        'title' => $this
          ->t('Merge into @target_label', [
          '@target_label' => $parent
            ->label(),
        ]),
        'weight' => 5,
        'url' => Url::fromRoute('entity.workspace.merge_form', [
          'source_workspace' => $entity
            ->id(),
          'target_workspace' => $parent
            ->id(),
        ], [
          'query' => [
            'destination' => $entity
              ->toUrl('collection')
              ->toString(),
          ],
        ]),
      ];
    }
    $operations['manage'] = [
      'title' => $this
        ->t('Manage'),
      'weight' => 5,
      'url' => $entity
        ->toUrl(),
    ];
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();
    if ($this
      ->isAjax()) {
      $this
        ->offCanvasRender($build);
    }
    else {

      // Add a row for switching to Live.
      $has_active_workspace = $this->workspaceManager
        ->hasActiveWorkspace();
      $row_live = [
        'data' => [
          'label' => [
            'data' => [
              '#markup' => $this
                ->t('Live'),
            ],
          ],
          'owner' => '',
          'operations' => [
            'data' => [
              '#type' => 'operations',
              '#links' => [
                'activate' => [
                  'title' => 'Switch to Live',
                  'weight' => 0,
                  'url' => Url::fromRoute('workspaces.switch_to_live', [], [
                    'query' => $this
                      ->getDestinationArray(),
                  ]),
                ],
              ],
              '#access' => $has_active_workspace,
            ],
          ],
        ],
      ];
      if (!$has_active_workspace) {
        $row_live['class'] = [
          'active-workspace',
          'active-workspace--default',
        ];
      }
      array_unshift($build['table']['#rows'], $row_live);
      $build['#attached'] = [
        'library' => [
          'workspaces/drupal.workspaces.overview',
        ],
      ];
    }
    return $build;
  }

  /**
   * Renders the off canvas elements.
   *
   * @param array $build
   *   A render array.
   */
  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',
              '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',
              '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']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 5
EntityHandlerBase::moduleHandler protected function Gets the module handler. 5
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities.
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. 4
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkspaceListBuilder::$renderer protected property The renderer service.
WorkspaceListBuilder::$workspaceManager protected property The workspace manager service.
WorkspaceListBuilder::$workspaceRepository protected property The workspace repository service.
WorkspaceListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
WorkspaceListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
WorkspaceListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
WorkspaceListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations
WorkspaceListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilder::load
WorkspaceListBuilder::offCanvasRender protected function Renders the off canvas elements.
WorkspaceListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
WorkspaceListBuilder::__construct public function Constructs a new EntityListBuilder object. Overrides EntityListBuilder::__construct