1

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

}

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the effort but found the solution some time ago. it was a typo: it has to be libraries.yml. drupal needs this static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.