hook_theme provides a way to place theme functions in a different location using the 'file' key. But when that theme function is used in context of theme_suggestions_alter the path defined in 'file' doesn't get included. Attached patch fixes it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

aspilicious’s picture

Issue summary: View changes
star-szr’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks! Tagging for test coverage.

star-szr’s picture

Issue tags: +Twig, +sprint
star-szr’s picture

Assigned: Unassigned » star-szr

I'm working on this.

star-szr’s picture

@aspilicious - I actually can't seem to reproduce this in a test - can you point me to or post a concrete example of this happening? Is this a module or a theme or a combination of the two?

Basically in my test I have two hook_theme() implementations pointing to theme functions, both are in modules that are installed. Theme function A is in the .module, theme function B is in a .inc file specified in 'file' in the hook_theme(). I call theme hook A then use hook_theme_suggestions_HOOK_alter() to redirect to B, but the include file gets loaded just fine.

I'm thinking we should actually just move the existing "include" code down below the suggestion hooks since I can't see where it's useful to have that until just before theme functions and preprocess functions are called anyway.

star-szr’s picture

@aspilicious got in touch with me on IRC and provided some sample code, so I will continue to dig in here.

star-szr’s picture

Assigned: star-szr » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
6.07 KB
7.2 KB

Turns out I just needed to drupalGet() twice because the theme registry is being rebuilt during the first request :)

Failing test + fix attached.

The last submitted patch, 7: 2188721-7-testonly.patch, failed testing.

aspilicious’s picture

Status: Needs review » Reviewed & tested by the community

Works for me :)

catch’s picture

Code looks the same in 7.x as well, should this be tagged for backport?

star-szr’s picture

Assigned: Unassigned » star-szr
Issue tags: +Needs backport to D7

Possibly, I can try to backport the test after commit and see what happens, at the very least we can add more test coverage to 7.x.

catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed/pushed to 8.x, thanks!

star-szr’s picture

Having lack of internet problems but I am working on a test for 7.x and it fails, so as far as I can tell this wasn't a regression. Will have at least the failing test up within a few days.

star-szr’s picture

Assigned: star-szr » Unassigned
Status: Patch (to be ported) » Needs review
FileSize
3.92 KB

Here's the failing test for 7.x as promised. Unassigning in case I don't have time to come back to this soon.

Status: Needs review » Needs work

The last submitted patch, 14: 2188721-14-fail.patch, failed testing.

benjf’s picture

star-szr’s picture

Status: Needs work » Needs review
Issue tags: -sprint

Thanks @benjf! Sending to the bot.

star-szr’s picture

Status: Needs review » Needs work

Cool, it's green! Since there's already this code further up in theme():

  // Include a file if the theme function or variable processor is held
  // elsewhere.
  if (!empty($info['includes'])) {
    foreach ($info['includes'] as $include_file) {
      include_once DRUPAL_ROOT . '/' . $include_file;
    }
  }

The second include_once would only be used for theme suggestions in D7 so a couple things:

  1. We should only call this code if $info changes after going through the suggestions-processing code. Possibly if $hook != $suggestion (well, something along those lines anyway).
  2. We should document why this code is there.

  • catch committed 07cd3c9 on 8.3.x
    Issue #2188721 by Cottser, aspilicious: Theme suggestions don't load...

  • catch committed 07cd3c9 on 8.3.x
    Issue #2188721 by Cottser, aspilicious: Theme suggestions don't load...

  • catch committed 07cd3c9 on 8.4.x
    Issue #2188721 by Cottser, aspilicious: Theme suggestions don't load...

  • catch committed 07cd3c9 on 8.4.x
    Issue #2188721 by Cottser, aspilicious: Theme suggestions don't load...