You are here

protected function TocFilterBlock::blockAccess in TOC filter 8.2

Indicates whether the block should be shown.

Blocks with specific access checking should override this method rather than access(), in order to avoid repeating the handling of the $return_as_object argument.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user session for which to check access.

Return value

\Drupal\Core\Access\AccessResult The access result.

Overrides TocBlockBase::blockAccess

See also

self::access()

File

src/Plugin/Block/TocFilterBlock.php, line 27
Contains \Drupal\toc_filter\Plugin\Block\TocFilterBlock.

Class

TocFilterBlock
Provides a 'TOC filter' block.

Namespace

Drupal\toc_filter\Plugin\Block

Code

protected function blockAccess(AccountInterface $account) {
  $node = $this
    ->getCurrentNode();

  // If current page is not a node or does not contain a [toc] token return
  // forbidden access result.
  if (!$node || !$node
    ->hasField('body') || stripos($node->body->value, '[toc') === FALSE) {
    return AccessResult::forbidden();
  }

  // Since entities (ie node) are cached we need to pass the current node's
  // body through it's filters and see if a TOC is being generated and
  // displayed in this block.

  /** @var \Drupal\toc_api\TocManagerInterface $toc_manager */
  $toc_manager = \Drupal::service('toc_api.manager');

  // Reset removes any stored references to a current toc.
  $toc_manager
    ->reset($this
    ->getCurrentTocId());

  // Reprocess the node's body since the processed result is typically
  // cached via entity render caching.
  // This will create an identical TOC instance stored in the TocManager.
  check_markup($node->body->value, $node->body->format, $node->body
    ->getLangCode());
  return parent::blockAccess($account);
}