O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Attaching libraries

Speaking of using a library, let's see what are some of the more important ways you as a module developer can make use of a library in your code.

The most common way you'll be using libraries is attaching them to your render arrays. Typically, this means that the library is needed for the rendering of that component so that if the said component is missing from the page, the library assets are no longer included.

Here is what a render array would look like with the preceding library we defined attached to it:

    return [      '#theme' => 'some_theme_hook',      '#some_variable' => $some_variable,      '#attached' => [        'library' => [          'my_module/my-library',        ],      ],    ];

The #attached property is important here, and it signifies that we are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required