class TestBlockInstantiation

Same name and namespace in other branches
  1. 11.x core/modules/block/tests/modules/block_test/src/Plugin/Block/TestBlockInstantiation.php \Drupal\block_test\Plugin\Block\TestBlockInstantiation
  2. 10 core/modules/block/tests/modules/block_test/src/Plugin/Block/TestBlockInstantiation.php \Drupal\block_test\Plugin\Block\TestBlockInstantiation
  3. 8.9.x core/modules/block/tests/modules/block_test/src/Plugin/Block/TestBlockInstantiation.php \Drupal\block_test\Plugin\Block\TestBlockInstantiation

Provides a basic block for testing block instantiation and configuration.

Plugin annotation


@Block(
  id = "test_block_instantiation",
  admin_label = @Translation("Display message")
)

Hierarchy

Expanded class hierarchy of TestBlockInstantiation

1 file declares its use of TestBlockInstantiation
BlockBaseTest.php in core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php

File

core/modules/block/tests/modules/block_test/src/Plugin/Block/TestBlockInstantiation.php, line 18

Namespace

Drupal\block_test\Plugin\Block
View source
class TestBlockInstantiation extends BlockBase {
  
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'display_message' => 'no message set',
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    return AccessResult::allowedIfHasPermission($account, 'access content');
  }
  
  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form['display_message'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Display message'),
      '#default_value' => $this->configuration['display_message'],
    ];
    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['display_message'] = $form_state->getValue('display_message');
  }
  
  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#children' => $this->configuration['display_message'],
    ];
  }

}

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