You are here

class WorkspaceListBuilder in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/WorkspaceListBuilder.php \Drupal\workspaces\WorkspaceListBuilder
  2. 10 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\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) : '',
          '#markup' => $entity
            ->label(),
        ],
      ],
      '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';
    }
    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['deploy'] = [
        'title' => $this
          ->t('Deploy content'),
        // The 'Deploy' operation should be the default one for the currently
        // active workspace.
        'weight' => $active_workspace && $entity
          ->id() == $active_workspace
          ->id() ? 0 : 20,
        'url' => $entity
          ->toUrl('deploy-form', [
          '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(),
          ],
        ]),
      ];
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();
    if ($this
      ->isAjax()) {
      $this
        ->offCanvasRender($build);
    }
    else {
      $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('edit-form'),
        '#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']['deploy'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Deploy content'),
          '#url' => $active_workspace
            ->toUrl('deploy-form', [
            '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']['#markup']),
          '#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 An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
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. 3
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::getLabel Deprecated protected function Gets the label of an entity.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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