I'm new to Drupal 8 themes and am trying to edit CSS on individual pages. I've read how to do it, and have attempted to implement, but when I clear my sites cache and try to view the page, the new CSS (or JS on some pages) does not seem to get utilized.
What I have done
In my .theme file I have this code
function odyssey_preprocess_node(&$variables) { // find node object $node = \Drupal::routeMatch()->getParameter('node'); if ($node) { //ensure object is a node if ($node->id() == 1 ) { //grab specific node (R&D page) //attach the library $variables['#attached']['library'][] = 'odyssey/research-devel'; } elseif ($node->id() == 3 ) {//CR Calc node $variables['#attached']['library'][] = 'odyssey/cr-calc'; } } }In my .libraries.yml file I have the following code
research-devel: css: theme: css/research-devel.css: {} cr-calc: css: theme: css/cr-calc.css: {} js: js/cr-calc.js: {} dependencies: - core/jqueryI uploaded the named files from .libraries.yml into the appropriate folders (I have my global CSS and JS files working, so I know the locations are correct.)
Does anyone see any glaring errors? Am I going about this the wrong way? Like I said, fairly new to the game and I'm sure that adding specific page stylings to the global CSS is the "Non-Drupal" way to go about it, so any advice would be appreciated.
$nodeis actually a node you can do$node instanceof NodeInterface. The second step is debugging your code to see that it is entering the if statement(s).