I’m trying to get the value of the node title when a page loads to pass that string to a module. I’m trying to follow the recent release documentation, and my build function contains the following code:
// get the page title $ request = \Drupal::request(); $ route_match = \Drupal::routeMatch(); $ title = \Drupal::service('title_resolver')->getTitle($ request, $ route_match->getRouteObject()); if ($ node = \Drupal::routeMatch()->getParameter('node')) { $ build['#attached']['library'][] = 'mycustomblock/custom'; $ build['#attached']['drupalSettings']['mycustomblock']['title']; }
In the console I’m getting an uncaught reference error because ‘title’ is undefined, maybe something wrong with the routing?
How can I get the title of the current node and pass it to my module?