0

I am learning to build custom blocks through a module. I am trying to get the build() function to work. I have the code and it works. However, if I edit the configuration of the block as a user it does not print the text in the text formatter.

'#markup' =>
    '<div>' . $this->t('Body: @body', ['@body' => $this->configuration['body']]) . '</div>',

In the rendered block on the page it prints... Body: But not the text in the wysiwyg.

Any ideas how to get that to work. The tutorials and links I am finding are not relavant.

4
  • So I figure out that - $this->configuration['body']['value'] -- prints the text from the wysiwyg in the configuration but it prints the html. Now I need to figure out how not to ... Anyone?
    – timwhelan
    Sep 20, 2020 at 20:10
  • 2
    Does this answer your question? How to safely render node body on a custom variable?
    – No Sssweat
    Sep 20, 2020 at 22:09
  • ☝️ replace node with block, same procedure. I suggest you go with the 2nd approach of Render element in php solution
    – No Sssweat
    Sep 20, 2020 at 22:11
  • Thank you for you response. However, 4k4's answer more fits what I am looking for. I was not trying to integrate twig.
    – timwhelan
    Sep 22, 2020 at 0:24

1 Answer 1

2

Assuming you store the block configuration via a text_format form element as in https://drupal.stackexchange.com/a/278811/47547

src/Plugin/Block/ExampleBlock.php

<?php

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides an example block.
 *
 * @Block(
 *   id = "mymodule_example",
 *   admin_label = @Translation("Example"),
 *   category = @Translation("mymodule")
 * )
 */
class ExampleBlock extends BlockBase {

  public function blockForm($form, FormStateInterface $form_state) {
    $body = !empty($this->configuration['body']) ? $this->configuration['body'] : [];
    $form['body'] = [
      '#type' => 'text_format',
      '#title' => 'Body',
      '#format' => isset($body['format']) ? $body['format'] : 'full_html',
      '#default_value' => isset($body['value']) ? $body['value'] : '',
    ];
    return $form;
  }

  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['body'] = $form_state->getValue('body');
  }

You would output the wysiwyg content via processed_text:

  public function build() {
    $build = [
      '#type' => 'container',
      'label' => ['#markup' => 'Body: '],
    ];
    if (!empty($this->configuration['body'])) {
      $build['body'] = [
        '#type' => 'processed_text',
        '#text' => $this->configuration['body']['value'],
        '#format' => $this->configuration['body']['format'],
      ];
    }
    return $build;
  }

}
1
  • Thank you very much for this. Honestly, this is so much more than I was expecting. I did not see anything like this while I was looking. I would love a link to a Drupal API or something page as well if you can. Trying hard to learn but it is taking longer then I thought. I appreciate the complete code share. I tried it and it worked perfectly. Now I need to learn this and keep going. Thank you!!!
    – timwhelan
    Sep 22, 2020 at 0:29

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.