(Sorry if that's Drupal 101, but I'm still at the very bottom of the learning curve ..)

I have a custom module, with a routing file that redirects to a path that I have chosen arbitrarily :

  path: 'solr-search/content'
    _controller: '\Drupal\gref\Controller\GrefSearchController::search'
    _permission: 'use gref search'

I have another YAML config which describes the View that must be rendered when accessing to solr-search/content :

langcode: en
status: true
    display_plugin: page
    id: page_1
    display_title: Page
    position: 1
      display_extenders: {  }
      path: solr-search/content
        type: none
        cache: false
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - url.query_args
      cacheable: false
      max-age: -1
        - 'config:field.storage.node.body'

Since the page is completely YAML-based, I do not have a Twig template that is linked to it, and because of that, I cannot use hooks like template_preprocess_page because I cannot name the function properly.

I'm a bit lost. Should I change the structure I have, and render the page in a Twig template anyway ? Thanks !

  • You don't need a route and a custom controller, the View defined in the yaml file, once imported, generates this automatically and then you can attach CSS in theming.
    – 4uk4
    Oct 9, 2018 at 6:41
  • How do I attach a CSS file do the page ? Is it like @Cesar Moore said below ? Oct 9, 2018 at 7:08
  • Yes, or hook_page_attachments
    – 4uk4
    Oct 9, 2018 at 7:17
  • I implemented hook_page_attachments() in .module to attach my library but still no sign of CSS. Is there a trick to see if the css file is properly loaded / attached to the page ? Oct 9, 2018 at 9:18
  • Disable CSS file aggregation in the site performance settings and check the HTML source in the browser.
    – 4uk4
    Oct 9, 2018 at 9:23

1 Answer 1


You can still use the hook_preprocess_page for a specific path like this:

 * Implements hook_preprocess_HOOK().
 * Implements hook_preprocess_page().
 * @param $variables
function mymodule_preprocess_page(&$variables) {
  $current_path = \Drupal::service('path.current')->getPath();
  if ($current_path === '/solr-search/content') {
    $variables['#attached']['library'][] = 'my_module/my-css-library';
  • This doesn't seem to work. Maybe I made a mistake with the name of the library. My .libraries file is : gref-lib: version: 1.x css: theme: css/grefcss.css: {} and I added 'gref/grefcss' in the .module is that wrong ? Oct 8, 2018 at 22:23
  • If your module name if gref, then the full library name should be: gref/gref-lib Oct 8, 2018 at 22:38
  • Mmmh, still no sign of CSS in the result. I'm really starting to think the hook is not triggered. Any ways I could ensure the hook is executed ? I even removed the condition, to ensure it is not skipped Oct 8, 2018 at 22:58
  • 1
    Okay, I managed to make the CSS show up. I removed the square brackets around 'my_module/my-css-library' and it finally worked. Thanks ! Oct 10, 2018 at 19:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.