Drupal 8 自定义模块库
Drupal 8 Custom Module Libraries
我在自定义模块目录的 2 个不同目录中有以下 2 个文件。
modules/custom/calculator
这是我的 info.yml 文件(计算器。info.yml)
name: calculator
type: module
description: Calculator
core: 8.x
package: Custom
我的css在目录modules/custom/calculator/css,js在modules/custom/calculator/js目录
我的库文件是这样的:(calculator.libraries.yml)
base-style:
version: VERSION
css:
component:
css/wunderpage.css: {}
js:
js/wunderpage.js: {}
js/jquery.matchHeight-min.js: {}
我还有一个 calculator.module 文件 :
<?php
/**
* @file
* Contains calculator.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function calculator_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the calculator module.
case 'help.page.calculator':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Calculator') . '</p>';
return $output;
default:
}
function calculator_preprocess_page(&$variables) {
$variables['#attached']['library'][] = 'calculator/base-style';
}
}
我的 css 和 js 等仍然没有加载。
有人知道为什么我的库没有被加载吗?
hook_preprocess_page 也有一些问题。
我使用了 hook_preprocess (https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_preprocess/8) 并且有效。
也许你可以试试。
尝试使用
$variables['#attached']['libraries_load'][] = '';
或
drupal_process_attached():
我在自定义模块目录的 2 个不同目录中有以下 2 个文件。
modules/custom/calculator
这是我的 info.yml 文件(计算器。info.yml)
name: calculator
type: module
description: Calculator
core: 8.x
package: Custom
我的css在目录modules/custom/calculator/css,js在modules/custom/calculator/js目录
我的库文件是这样的:(calculator.libraries.yml)
base-style:
version: VERSION
css:
component:
css/wunderpage.css: {}
js:
js/wunderpage.js: {}
js/jquery.matchHeight-min.js: {}
我还有一个 calculator.module 文件 :
<?php
/**
* @file
* Contains calculator.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function calculator_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the calculator module.
case 'help.page.calculator':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Calculator') . '</p>';
return $output;
default:
}
function calculator_preprocess_page(&$variables) {
$variables['#attached']['library'][] = 'calculator/base-style';
}
}
我的 css 和 js 等仍然没有加载。
有人知道为什么我的库没有被加载吗?
hook_preprocess_page 也有一些问题。
我使用了 hook_preprocess (https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_preprocess/8) 并且有效。
也许你可以试试。
尝试使用
$variables['#attached']['libraries_load'][] = '';
或
drupal_process_attached():