1

I have built a custom block plugin in Drupal 8. However, I needed to insert some svg elements in the markup, which get all filtered out. Is there a way for me to specify to display the raw output, so that the svg does not get filtered out?

  public function build() {

    return array(
      '#type' => 'markup',
      '#markup' => '<div><svg>.....</svg></div>',
    );
 }
?>

I have tried adding '#template' => '{{ value|raw }}' and played some with it - but could not get it to work. There must be some way to allow me to insert svg in there...

3

You can use an inline template:

  public function build() {

  return array(
    '#type' => 'inline_template',
    '#template' => '<div><svg>{{ var }}</svg></div>',
    '#context' => array(
      'var' => $svg,
    ),
  );

You can use the inline template with an empty context, if you have no vars that need to be escaped:

  return array(
        '#type' => 'inline_template',
        '#template' => '<div><svg>...</svg></div>',
        '#context' => array(),
      );

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.