Tuesday, June 18, 2019

How to include javascript on single pages in Drupal 8





Step 1: Place the JavaScript file in your theme or module inside a folder called js. After this step your folder structure must look like :


Step 2: If your theme already has a my-theme.libraries.yml file open it, if not create it.
In Drupal 8 Libraries are simply collections of CSS or JS files bundled together under a uniquely identified library name.

my-library:
 js:
   js/my-script.js: {}

Now we have an asset library called my-theme/my-library which can be loaded to our site in several ways.

Step 3: Final step is to load the library in our site. This can be done in 4 specific ways:

Way 1: Attach library globally(for all page) via theme-name.info.yml file
Libraries can be specified in the theme-name.info.yml file by adding it to the libraries property, like this:

name: my-theme
type: theme
description: my theme
package: other
core: 8.x
libraries:
 - my-theme/global-effects
 - my-theme/my-library
base theme: classy


Note:- if you not given the base theme classy then core base theme stable default worked in drupal 8..

Any libraries specified here in the themename.info.yml file will be made available on evey page.

Way 2: Using hook_page_attachments_alter in theme-name.theme file.
Following lines of code will attach our library only to the node listing page.

function mytheme_page_attachments_alter(&$page){
 $path = $current_path = \Drupal::service('path.current')->getPath();
 if($path == '/node') {
   $page['#attached']['library'][] = 'my-theme/my-library';
 }
}

Way 3: Another useful way to conditionally add a library is by using a preprocess function like hook_preprocess_page().

Here's another example of restricting our asset libray to the front page
Ex:1
function mytheme_preprocess_page(&$variables){
  if ($variables['is_front'] == TRUE) {
    $variables['#attached']['library'][] = 'my-theme/my-library';
  }
}
function mytheme_preprocess_page(&$variables) {
$variables['page']['#cache']['contexts'][] = 'route';
  $route = "entity.node.preview";
  if (\Drupal::routeMatch()->getRouteName() === $route) {
    $variables['#attached']['library'][] = 'my-theme/my-library';
  }
}

Way 4: Asset libraries can also be attached from within a Twig template using the attach_library function. Anytime that template is used the corresponding library will be attached in accordance with any template-specific conditions.
In order to see this in action, we need to create or modify a template in our theme. In this example let us modify node.html.twig template to attach our library only if node.id == 1 with following lines of code

{% if node.id == 1 %}
 {{ attach_library('my-theme/my-library') }}
{% endif %}

With this in place, visiting node 1 in our site, we will be able to see our library in action.

Attaching a library via a Twig template

You can attach an asset library to a Twig template using the attach_library() function in any *.html.twig, file like so:
{{ attach_library('my-theme/my-library') }}
<div>Some message markup {{ message }}</div>

1 comment:

If you have any problem please let me know.