AnswerBun.com

Is it possible to pass variables as arguments to *.libraries.yml?

Drupal Answers Asked by plambre on February 25, 2021

Say I have a foo.libraries.yml defined as so:

external.bc.js:
  version: 1.x
  js:
    //players.brightcove.net/{{variable here}}/{{variable here}}/index.min.js: { type: external, minified: true}

Where {{ variable here }} is where I want to insert values when I attach the library

Then, in a hook_ENTITY_TYPE_view() I am running:

$build['#attached']['library'][] = 'brightcove/external.bc.js';

Which is where I’d also like the pass those variables. Is that possible?

One Answer

Instead of defining the library in the .libraries.yml file, you should add it using hook_library_info_build(), which is the hook to add dynamic library definitions, as in your case.

function mymodule_library_info_build() {
  $libraries = [];

  $js_path = 'the dynamic path to the external JavaScript file';
  $libraries['external.bc.js'] = [
    'version' => '1.x',
    'js' => [
      $js_path => ['type' => 'external', 'minified' => TRUE]
    ],
  ];

  return $libraries;
}

Answered by kiamlaluno on February 25, 2021

Add your own answers!

Related Questions

Make node only accesible by path alias

3  Asked on November 19, 2021 by kevin-ramirez-zavalza

     

Form and gated content

1  Asked on November 17, 2021 by dataguy

 

Calculate Star rating based on webform submissions

1  Asked on November 17, 2021 by brainhax

 

Pager not working when $form[‘table’] is called with AJAX

1  Asked on November 17, 2021 by user3428971

     

Send message notification when license expired

1  Asked on November 15, 2021 by learner123

 

View filtering, Relationship Vs Contextual filter

1  Asked on November 15, 2021 by bassem-mohamed

   

Missing some block fields

1  Asked on November 15, 2021 by metalbearsolid

   

Custom template for each profile2 profile type edit form

1  Asked on November 13, 2021 by stavovei-ciprian

       

How to save an image resource

1  Asked on November 13, 2021 by kosher

 

How to alter node body content through Views pre render hook?

1  Asked on November 11, 2021 by pramodg

 

Set Default Value for Image Field contain Multiple Images

1  Asked on November 11, 2021 by pglatz

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP