You are here

WorkspaceBlock.php in Workspace 8

File

src/Plugin/Block/WorkspaceBlock.php
View source
<?php

namespace Drupal\workspace\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\multiversion\Workspace\WorkspaceManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * @Block(
 *   id = "workspace_switcher_block",
 *   admin_label = @Translation("Workspace switcher"),
 *   category = @Translation("Workspace"),
 * )
 */
class WorkspaceBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\multiversion\Workspace\WorkspaceManagerInterface
   */
  protected $workspaceManager;

  /**
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   * @param \Drupal\multiversion\Workspace\WorkspaceManagerInterface $workspace_manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, WorkspaceManagerInterface $workspace_manager, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->workspaceManager = $workspace_manager;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('workspace.manager'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [
      // @todo the block depending on the toolbar is obscure; find a better way to generate this form
      '#pre_render' => [
        'workspace.toolbar:preRenderWorkspaceSwitcherForms',
      ],
      // This wil get filled in via pre-render.
      'workspace_forms' => [],
      '#attached' => [
        'library' => [
          'workspace/drupal.workspace.switcher',
        ],
      ],
      '#cache' => [
        'contexts' => $this->entityTypeManager
          ->getDefinition('workspace')
          ->getListCacheContexts(),
        'tags' => $this->entityTypeManager
          ->getDefinition('workspace')
          ->getListCacheTags(),
      ],
    ];
    return $build;
  }

}

Classes

Namesort descending Description
WorkspaceBlock Plugin annotation @Block( id = "workspace_switcher_block", admin_label = @Translation("Workspace switcher"), category = @Translation("Workspace"), )