2

In the module I am developing I need to add script which require jQuery.The will be added depending on configuration settings. Hence I am using template_preprocess_module(&$variables) where I am trying to add script by doing something like this:

function template_preprocess_module(&$variables) {
  if (TRUE) {
    $variables['jquery'] = ?;
    $variables['script'] = 'path to script in module';
  }
}

Then in the template I will simply be rendering these variable in the head section. But I am facing trouble in adding jQuery, because I don't know its source (obviously I know its source in core but I need something over here which automates path). Can the source be known by using something like drupal_get_path() ? I am also not sure if this is the best way to add script conditionally, any suggestion regarding this? Thanks.

3
  • The question is not quite clear; what exactly are you trying to do? Maybe an example would help. Commented Jun 10, 2015 at 10:01
  • @Topsitemakers I have updated the question.Does this help? Commented Jun 10, 2015 at 10:21
  • which drupal version you are using? Commented Jun 10, 2015 at 11:24

2 Answers 2

5

To attach a script in Drupal 8 you need to define a library first in your modules *.libraries.yml (replace * with your modules machine name). The file should look as follows:

libraryname:
  version: 1.x
  js:
    js/script.js: {}
  dependencies:
    - core/jquery

After that you have the possibilities to attach a script through a preprocess function or directly in the template with a twig function. I believe you are trying to achieve the first. In that case you need to modify the render array, like in this example:

<?php
function modulename_element_info_alter(array &$types) {
  if (isset($types['table'])) {
    $types['table']['#attached']['library'][] = 'modulename/libraryname';
  }
}
?>

If you want to append a script to every page depending if a custom setting is activated in your module, you should do as follows:

function modulename_page_attachments_alter(array &$page) {
  if ($settingIsTrue) {
    $page['#attached']['library'][] = 'modulename/libraryname';
  }
}

You can read more about it in the manual.

6
  • thanks for your answer but I have already seen this in the documentation and it didn't suited my requirements.Anyways do u have some ideas related to manner I have suggested above? Commented Jun 11, 2015 at 7:04
  • What is the actual problem you are facing? Do you have problems choosing the right hook? Or is jquery your problem? Adding jquery as a dependency to your script is really easy, see my edit.
    – alexej_d
    Commented Jun 11, 2015 at 8:22
  • I am trying to add both jquery and script in twig template hence I am needing to prepare variable by doing pre-processing.But then I am not able to add jQuery present in the core becoz I am having trouble in knowing its source. Any ideas? Thanks. Commented Jun 11, 2015 at 9:57
  • You should do it as in my last edit. This hook allows you to attach a script to every page depending on your special conditions. If you add jquery as a dependency in your *.libraries.yml it will be attached automatically before your script.
    – alexej_d
    Commented Jun 11, 2015 at 11:51
  • thanks for showing patience in answering.One last thing please do tell me if u know a way to find path of jQuery present in core(using some function of Drupal 8).Thanks. Commented Jun 11, 2015 at 12:42
1

You can add jQuery directly in a twig template using the new Drupal 8 attach_library() twig function:

{# my_template.html.twig #}
<div>
{# This will add jQuery to this template only #}
{{ attach_library('core/jquery') }}
</div>

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.