0

I'm developing a custom block in Drupal 8. In my build() method I've got:

$progress_markup = array(
  '#theme' => 'progress_bar',
  '#percentage' => $registration_percentage,
  '#message' => array('#markup' => $registration_node_count . ' of ' . $this->configuration['capacity']),
);
$markup = \Drupal::theme()->render('progress_bar', $progress_markup);

This generates the following HTML:

<div class="progress" data-drupal-progress>
  <div class="progress__track"><div class="progress__bar" style="width: %"></div></div>
  <div class="progress__percentage">%</div>
  <div class="progress__description">30 of 120</div>
</div>

However, when the block renders on the page, the style attribute has been removed. Here's the relevant D8 issue.

What I don't understand is: if I create a custom block and use Full HTML, and paste in the generated markup that contains the style attribute, Drupal 8 will happily render it. But when I generate this markup in the build() method, it gets removed.

Is there a way to specify, in the block plugin definition, that the build() method should not filter out any HTML? Any other thoughts on how ot get around this issue?

  • The build method should return a render array and not markup. – Eyal Nov 28 '15 at 1:28
  • @Eyal thanks, but that doesn't really answer my question. If I just returned the $progress_markup variable in the build() method, the style attribute is still filtered out. – kostajh Nov 29 '15 at 1:14
  • How are you trying to pass the style attribute? – Eyal Nov 29 '15 at 5:30
  • @Eyal not sure if this helps, but here is the code github.com/durhamatletico/durhamatletico-cms/blob/master/… and the block is on the homepage of durhamatletico.com – kostajh Nov 29 '15 at 10:06
  • Try to pass the render array without rendering it. – Eyal Nov 29 '15 at 10:11
5

This is a working example of adding the style attribute to the render array of a block via the build() method.

<?php
/**
 * @file
 * Contains Drupal\foo\Plugin\Block\FooBlock
 */


namespace Drupal\foo\Plugin\Block;
use Drupal\Core\Block\BlockBase;


/**
 * @block(
 *   id = "foo",
 *   admin_label = @Translation("Foo block")
 * )
 */
class FooBlock extends BlockBase {

  /**
   * Builds and returns the renderable array for this block plugin.
   *
   * If a block should not be rendered because it has no content, then this
   * method must also ensure to return no content: it must then only return an
   * empty array, or an empty array with #cache set (with cacheability metadata
   * indicating the circumstances for it being empty).
   *
   * @return array
   *   A renderable array representing the content of the block.
   *
   * @see \Drupal\block\BlockViewBuilder
   */
  public function build() {
    return [
      'inside' => [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => 'Hello world',
        '#attributes' => [
          'class' => ['some-class'],
          'style' => ['color:red']
        ]
      ]
    ];
  }
}

Working example

  • This is nice but how to you send attributes to the wrapper DIV of a custom block. I know it can be done with preprocess hooks but I was wondering if it can be done from here ... – GiorgosK Feb 24 '17 at 19:13
  • 1
    Try #wrapper_attributes – Eyal Feb 26 '17 at 11:25
  • thanks for trying but it does not work ... I will try to open up a proper question ... – GiorgosK Mar 1 '17 at 6:50
0

You need to put a #attributes property in your render array, but at the root of the render array. Here we add block-class class to the block :

class FooBlock extends BlockBase {

  public function build() {
    return [
      'inside' => [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => 'Hello world',
        '#attributes' => [
          'class' => ['inside-class'],
        ]
      ],
      '#attributes' => [
        'class' => ['block-class']
      ]
    ];
  }
}

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.