Use Config in Block Display

Last updated on
25 April 2017

To make use of the configuration of instances of the block, we can modify the build() method of the HelloBlock class:

  /**
   * {@inheritdoc}
   */  
  public function build() {
    $config = $this->getConfiguration();

    if (!empty($config['hello_block_name'])) {
      $name = $config['hello_block_name'];
    }
    else {
      $name = $this->t('to no one');
    }
    return array(
      '#markup' => $this->t('Hello @name!', array (
          '@name' => $name,
        )
      ),
    );
  }