function TestPageTitleMainContentAndMessagesBlock::build

Same name and namespace in other branches
  1. main core/modules/block/tests/modules/block_test/src/Plugin/Block/TestPageTitleMainContentAndMessagesBlock.php \Drupal\block_test\Plugin\Block\TestPageTitleMainContentAndMessagesBlock::build()

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

File

core/modules/block/tests/modules/block_test/src/Plugin/Block/TestPageTitleMainContentAndMessagesBlock.php, line 71

Class

TestPageTitleMainContentAndMessagesBlock
Provides a block which includes page title, main content & messages together.

Namespace

Drupal\block_test\Plugin\Block

Code

public function build() : array {
  $build = [];
  // Print a message to later verify that messages are rendered in the block.
  $this->messenger()
    ->addStatus('This is a status message.');
  if ($this->isPageTitlePlaced === TRUE) {
    $build['page_title'] = [
      '#type' => 'page_title',
      '#title' => $this->title,
    ];
    // Display text which confirms title was displayed by this block.
    // Content of text is used in corresponding test.
    $build['page_title_confirmation'] = [
      '#markup' => $this->t('Page title has been placed in the block.'),
    ];
  }
  if ($this->isMainContentPlaced === TRUE) {
    $build['main_content'] = $this->mainContent;
    // Display text which confirms main content was displayed by this block.
    // Content of text is used in corresponding test.
    $build['main_content_confirmation'] = [
      '#markup' => $this->t('Main content has been placed in the block.'),
    ];
  }
  $build['content']['messages'] = [
    '#prefix' => '<div id="test-block-messages-wrapper">',
    '#weight' => -1000,
    '#type' => 'status_messages',
    '#include_fallback' => TRUE,
    '#suffix' => '</div>',
  ];
  return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.