TestPageTitleMainContentAndMessagesBlock.php

Same filename and directory in other branches
  1. 11.x core/modules/block/tests/modules/block_test/src/Plugin/Block/TestPageTitleMainContentAndMessagesBlock.php

Namespace

Drupal\block_test\Plugin\Block

File

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

View source
<?php

declare (strict_types=1);
namespace Drupal\block_test\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\MainContentBlockPluginInterface;
use Drupal\Core\Block\MessagesBlockPluginInterface;
use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides a block which includes page title, main content & messages together.
 */
class TestPageTitleMainContentAndMessagesBlock extends BlockBase implements MainContentBlockPluginInterface, TitleBlockPluginInterface, MessagesBlockPluginInterface {
  
  /**
   * The page title: a string (plain title) or a render array (formatted title).
   *
   * @var string|array
   */
  protected $title = '';
  
  /**
   * The render array representing the main page content.
   *
   * @var array
   */
  protected $mainContent;
  
  /**
   * Whether setMainContent was called.
   *
   * @var bool
   */
  protected $isMainContentPlaced = FALSE;
  
  /**
   * Whether setTitle was called.
   *
   * @var bool
   */
  protected $isPageTitlePlaced = FALSE;
  
  /**
   * {@inheritdoc}
   */
  public function setMainContent(array $main_content) : void {
    $this->mainContent = $main_content;
    $this->isMainContentPlaced = TRUE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function setTitle($title) : static {
    $this->title = $title;
    $this->isPageTitlePlaced = TRUE;
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

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

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