1

I'm attempting to print the value of the title of the block in a block plugin.

public function build() {
    $build ='
        <div class="header-text">
            <'.$this->configuration['header-size-submit'].'>'.$this->title.'</'.$this->configuration['header-size-submit'].'>
        </div>
            ';
    return array(
        '#type' => 'text',
        '#markup' => $build,
        );
}

I attempted to get the value of the title by using

$this->title

But that did not work.

  • Why are you writing markup like that? It is a very bad practice. You should use render arrays and twig templates. – Eyal Jul 6 '16 at 16:10
  • It's probably in the configuration array. – Eyal Jul 6 '16 at 16:11
  • And '#type'=> 'text' doesn't exist. The type of markup is markup – Eyal Jul 6 '16 at 16:13
  • I tried $this->configuration['title'] but it was empty. I needed to use '#type' => 'text' because 'markup' was escaping on my html. I was creating the markup like that so users can edit settings like the header tags used within the panel pane editor. – Matt Jul 6 '16 at 16:17
  • The best practice is to offer your users to choose from a set of options. – Eyal Jul 6 '16 at 19:45
2

If your block plugin derives from the BlockBase class, BlockBase::label() returns the default title for the block, but the block title is set in BlockBase::buildConfigurationForm().

  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this->t('Title'),
    '#maxlength' => 255,
    '#default_value' => $this->label(),
    '#required' => TRUE,
  );
  $form['label_display'] = array(
    '#type' => 'checkbox',
    '#title' => $this->t('Display title'),
    '#default_value' => ($this->configuration['label_display'] === BlockInterface::BLOCK_LABEL_VISIBLE),
    '#return_value' => BlockInterface::BLOCK_LABEL_VISIBLE,
  );

So, your block class should check if $this->configuration['label_display'] is set to BlockInterface::BLOCK_LABEL_VISIBLE; if it is, then use $this->configuration['label'] for the title.

That said, I feel like the code you want to implement is actually not necessary, since the BlockBase class already takes care of showing the block title.

1

I was able to access the title by using

$this->configuration['label'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.