I'm trying to attach a library in a custom shortcode but it doesn't show up neither using render array nor using attach_library twig function.

  class MyTimeline extends ShortcodeBase {
  public function process($attributes, $text, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
    $output = [
      '#theme' => 'my_timeline_theme',
      '#attached' => array(
        'library' => array('my_timeline/timeline')
    return $this->render($output);


{{ attach_library('my_timeline/timeline') }}

The only way I can attach it is using this last line in the active theme, ie node.html.twig

  • It is probably because you are rendering the output on your own. Why do you call the render method as part of the process method?
    – Eyal
    Commented Dec 10, 2016 at 15:21
  • If I don't render it (I return the Array) I get a Notice: Array to string conversion in Drupal\shortcode\Shortcode\ShortcodeService->processTag() (line 422 of modules/shortcode/src/Shortcode/ShortcodeService.php). and the word Array is printed instead of the rendered content.
    – Angus
    Commented Dec 10, 2016 at 16:30

1 Answer 1


The ShortcodeBase Class uses renderplain which strips 'the attached assets and cache tags'

In the comments to the source code they say: We use renderplain so that the shortcode's cache tags would not bubble up to the parent and affect cacheability. Shortcode should be part of contentand self-container.

In order to attach the library I then rendered the Array with this code:

$renderer = \Drupal::service('renderer');
return $renderer->render($output);

and it works. Need to understand if and how it affects cache, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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