简体   繁体   中英

Include CSS file in Drupal 8 custom module with multiple routings

I would like to include a CSS file into my custom module in Drupal 8. I already have another module with only one routing path and one controller function and my CSS works fine there, but not so in the new module. Do I miss something, or is there anything different with multiple routing paths?

module.routing.yml :

module.support_bugs:
  path: '/support/bugs'
  defaults:
    _controller: '\Drupal\module\Controller\moduleController::bugsShow'
    _title: ''
  requirements:
    _permission: 'support_bugs'

module.support_requests:
  path: '/support/requests/{param1}'
  defaults:
    _controller: '\Drupal\module\Controller\moduleController::requestsShow'
    _title: ''
    param1: null
  requirements:
    _permission: 'support_requests'

module.support_docs:
  path: '/support/docs'
  defaults:
    _controller: '\Drupal\module\Controller\moduleController::docsShow'
    _title: ''
  requirements:
    _permission: 'support_docs'

I want to include the CSS for the output of support_changes .

module.library.yml :

module.support_requests:
  css:
    theme:
       src/css/modulestyle.css: {}

And in the end, I include it in my controller.

moduleController.php :

namespace Drupal\module\Controller;

class moduleController {

    //other functions

    public static function requestsShow($filter=null){

    //some code inhere

    $build['content'] = array(
      '#markup' => $output);
    $build['#attached']['library'][] = 'module/module.support_requests';
    return $build;
    }

    //other functions

}

May be because of the conflict in same key "module.support_requests" in routing.yml and library.yml files. Try changing the key in module.library.yml into " support_requests " and add the library as $build['#attached']['library'][] = 'module/ support_requests ';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM