0

I'm listing some files for download on a commerce product page and I want to customize the output. I'm trying to get a theme suggestion for file-link.html.twig but I'm failing to get them to work. I've previously used https://www.dannyenglander.com/code/drupal-8-theming-file-link-by-content-type/ for nodes, but it causes "Call to a member function getType() on null" error, I assume because its a product page.

This is the function:

function MYTHEME_theme_suggestions_file_link_alter(&$suggestions, $vars, $hook) {

  // Get the node type.
  $node = \Drupal::routeMatch()->getParameter('node');
  $type = $node->getType();

  // Create the theme hook suggestion.
  $suggestions[] = 'file__link__' . 'type_' . $type;
}

Though ideally I'd like to name the file after the field name and not the content type. Is this possible?

  • You're getting that error because the route is missing a value for or doesn't have a node parameter, so your assumption is valid. Drupal::routeMatch will tell you about the route you are currently on, as opposed to the link that is being generated. Try looking in $vars to find the node. – Charles Bamford Jan 16 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.