Chris@0: label() === 'some condition') { Chris@0: $build['#cache']['contexts'][] = 'user'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provide a block plugin specific block_build alteration. Chris@0: * Chris@0: * In this hook name, BASE_BLOCK_ID refers to the block implementation's plugin Chris@0: * id, regardless of whether the plugin supports derivatives. For example, for Chris@0: * the \Drupal\system\Plugin\Block\SystemPoweredByBlock block, this would be Chris@0: * 'system_powered_by_block' as per that class's annotation. And for the Chris@0: * \Drupal\system\Plugin\Block\SystemMenuBlock block, it would be Chris@0: * 'system_menu_block' as per that class's annotation, regardless of which menu Chris@0: * the derived block is for. Chris@0: * Chris@0: * @param array $build Chris@0: * A renderable array of data, only containing #cache. Chris@0: * @param \Drupal\Core\Block\BlockPluginInterface $block Chris@0: * The block plugin instance. Chris@0: * Chris@0: * @see hook_block_build_alter() Chris@0: * @see entity_crud Chris@0: * Chris@0: * @ingroup block_api Chris@0: */ Chris@0: function hook_block_build_BASE_BLOCK_ID_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) { Chris@0: // Explicitly enable placeholdering of the specific block. Chris@0: $build['#create_placeholder'] = TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Control access to a block instance. Chris@0: * Chris@0: * Modules may implement this hook if they want to have a say in whether or not Chris@0: * a given user has access to perform a given operation on a block instance. Chris@0: * Chris@0: * @param \Drupal\block\Entity\Block $block Chris@0: * The block instance. Chris@0: * @param string $operation Chris@0: * The operation to be performed; for instance, 'view', 'create', 'delete', or Chris@0: * 'update'. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The user object to perform the access check operation on. Chris@0: * Chris@0: * @return \Drupal\Core\Access\AccessResultInterface Chris@0: * The access result. If all implementations of this hook return Chris@0: * AccessResultInterface objects whose value is !isAllowed() and Chris@0: * !isForbidden(), then default access rules from Chris@0: * \Drupal\block\BlockAccessControlHandler::checkAccess() are used. Chris@0: * Chris@0: * @see \Drupal\Core\Entity\EntityAccessControlHandler::access() Chris@0: * @see \Drupal\block\BlockAccessControlHandler::checkAccess() Chris@0: * @ingroup block_api Chris@0: */ Chris@0: function hook_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\Core\Session\AccountInterface $account) { Chris@0: // Example code that would prevent displaying the 'Powered by Drupal' block in Chris@0: // a region different than the footer. Chris@0: if ($operation == 'view' && $block->getPluginId() == 'system_powered_by_block') { Chris@0: return AccessResult::forbiddenIf($block->getRegion() != 'footer')->addCacheableDependency($block); Chris@0: } Chris@0: Chris@0: // No opinion. Chris@0: return AccessResult::neutral(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @} End of "addtogroup hooks". Chris@0: */