You are here

public function ContextInspector::build in Context 8.4

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

modules/context_ui/src/Plugin/Block/ContextInspector.php, line 28

Class

ContextInspector
Provides a 'context inspector' block.

Namespace

Drupal\context_ui\Plugin\Block

Code

public function build() {

  /** @var \Drupal\Core\Extension\ModuleHandler $moduleHandler */
  $moduleHandler = \Drupal::service('module_handler');
  $module = $moduleHandler
    ->moduleExists('devel');
  $permission = \Drupal::currentUser()
    ->hasPermission('access devel information');
  if ($module && $permission) {

    /** @var \Drupal\context\ContextManager $context_manager */
    $context_manager = \Drupal::service('context.manager');

    /** @codingStandardsIgnoreStart * */
    $output = kpr($context_manager
      ->getActiveContexts(), TRUE);

    /** @codingStandardsIgnoreEnd * */
  }
  elseif ($module && !$permission) {
    $output = $this
      ->t('You do not have permissions to view debug content.');
  }
  elseif (!$module) {
    $output = $this
      ->t('Please enable the devel module to use the context inspector.');
  }
  $build = [
    '#type' => 'markup',
    '#markup' => $output,
  ];
  return isset($output) ? $build : [];
}