Skip to main content
deleted 34 characters in body
Source Link
alexej_d
  • 850
  • 1
  • 8
  • 20

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'][] = $page['#attached']['library'][] = 'modulename/libraryname';
  }
}

You can read more about it in the manual.

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'][] = $page['#attached']['library'][] = 'modulename/libraryname';
  }
}

You can read more about it in the manual.

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.

suggesting a hook which would fit
Source Link
alexej_d
  • 850
  • 1
  • 8
  • 20

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'][] = $page['#attached']['library'][] = 'modulename/libraryname';
  }
}

You can read more about it in the manual.

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';
  }
}
?>

You can read more about it in the manual.

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'][] = $page['#attached']['library'][] = 'modulename/libraryname';
  }
}

You can read more about it in the manual.

added 43 characters in body
Source Link
alexej_d
  • 850
  • 1
  • 8
  • 20

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';
  }
}
?>

You can read more about it in the manual.

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: {}

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';
  }
}
?>

You can read more about it in the manual.

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';
  }
}
?>

You can read more about it in the manual.

Source Link
alexej_d
  • 850
  • 1
  • 8
  • 20
Loading