1

(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 :

grefsearch:
  path: 'solr-search/content'
  defaults:
    _controller: '\Drupal\gref\Controller\GrefSearchController::search'
  requirements:
    _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
dependencies:
...
page_1:
    display_plugin: page
    id: page_1
    display_title: Page
    position: 1
    display_options:
      display_extenders: {  }
      path: solr-search/content
      cache:
        type: none
      defaults:
        cache: false
    cache_metadata:
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - url.query_args
      cacheable: false
      max-age: -1
      tags:
        - '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 !

6
  • 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
    Commented Oct 9, 2018 at 6:41
  • How do I attach a CSS file do the page ? Is it like @Cesar Moore said below ? Commented Oct 9, 2018 at 7:08
  • Yes, or hook_page_attachments
    – 4uk4
    Commented 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 ? Commented Oct 9, 2018 at 9:18
  • Disable CSS file aggregation in the site performance settings and check the HTML source in the browser.
    – 4uk4
    Commented Oct 9, 2018 at 9:23

1 Answer 1

0

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';
  }
}
4
  • 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 ? Commented Oct 8, 2018 at 22:23
  • If your module name if gref, then the full library name should be: gref/gref-lib Commented 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 Commented 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 ! Commented Oct 10, 2018 at 19:21

Your Answer

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

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