You are here

public function WorkbenchAccessBlock::build in Workbench Access 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/WorkbenchAccessBlock.php, line 65

Class

WorkbenchAccessBlock
Creates a block to show editorial status.

Namespace

Drupal\workbench_access\Plugin\Block

Code

public function build() {
  $build = [];

  // Using a context definition ensures the cacheability metadata from the
  // node is applied to the block output.
  // @see \Drupal\Core\Plugin\Context\ContextHandler::applyContextMapping
  if (($node = $this
    ->getContextValue('node')) && $node instanceof NodeInterface) {
    $scheme_storage = $this->entityTypeManager
      ->getStorage('access_scheme');
    if ($schemes = $scheme_storage
      ->loadMultiple()) {

      /** @var \Drupal\workbench_access\Entity\AccessSchemeInterface $scheme */
      foreach ($schemes as $id => $scheme) {
        $active = $scheme
          ->getAccessScheme();
        if ($values = $active
          ->getEntityValues($node)) {
          foreach ($values as $value) {
            $element = $active
              ->load($value);

            // @TODO: This needs to be tested better.
            $build['#theme'] = 'item_list';
            $build['#items']['#title'] = $this
              ->t('Editorial sections:');
            $build['#items'][] = $element['label'];
            $build['#plain_text'] = TRUE;
          }
        }
      }
    }
  }
  return $build;
}