theming – How can I load a custom twig template depending on a view mode?


Currently I have the following code in my module file:

function mymodule_theme_suggestions_credit_report(array $variables) {
  return (
    'pdf_report__' . $variables('elements')('#view_mode')
  );
}

function mymodule_theme($existing, $type, $theme, $path) {
  return (
    'pdf_report' => (
      'render element' => 'elements'
    )
  );
}

And I have inside /mymodule/templates/ 2 twig files

  1. pdf-report.html.twig (just added the code from entity-html.twig)
  2. pdf-report–new-style.html.twig

And I have a view mode in my pdf-report called new_style.

I’ve read documentation and it looks like this should work, but it doesn’t.

I tried doing the following:

function mymodule_theme_suggestions_credit_report(array $variables) {
  return (
    'pdf_report__' . $variables('elements')('#view_mode')
  );
}

function mymodule_theme($existing, $type, $theme, $path) {
  return (
    'pdf_report' => (
      'render element' => 'elements',
      'template'  => 'pdf-report--orcutt-style',
      'base hook' => 'pdf_report',
    )
  );
}

But this replaces all of the view modes, not just the one I want.