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.

Files: 
CommentFileSizeAuthor
#16 2188721-16.patch4.46 KBbenjf
PASSED: [[SimpleTest]]: [MySQL] 40,967 pass(es). View
#14 2188721-14-fail.patch3.92 KBCottser
FAILED: [[SimpleTest]]: [MySQL] 40,765 pass(es), 1 fail(s), and 0 exception(s). View
#7 2188721-7.patch7.2 KBCottser
PASSED: [[SimpleTest]]: [MySQL] 64,601 pass(es). View
#7 2188721-7-testonly.patch6.07 KBCottser
FAILED: [[SimpleTest]]: [MySQL] 64,600 pass(es), 1 fail(s), and 0 exception(s). View

Comments

aspilicious’s picture

Issue summary: View changes
Cottser’s picture

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

Thanks! Tagging for test coverage.

Cottser’s picture

Issue tags: +Twig, +sprint
Cottser’s picture

Assigned: Unassigned » Cottser

I'm working on this.

Cottser’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.

Cottser’s picture

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

Cottser’s picture

Assigned: Cottser » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
6.07 KB
FAILED: [[SimpleTest]]: [MySQL] 64,600 pass(es), 1 fail(s), and 0 exception(s). View
7.2 KB
PASSED: [[SimpleTest]]: [MySQL] 64,601 pass(es). View

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?

Cottser’s picture

Assigned: Unassigned » Cottser
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!

Cottser’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.

Cottser’s picture

Assigned: Cottser » Unassigned
Status: Patch (to be ported) » Needs review
FileSize
3.92 KB
FAILED: [[SimpleTest]]: [MySQL] 40,765 pass(es), 1 fail(s), and 0 exception(s). View

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

FileSize
4.46 KB
PASSED: [[SimpleTest]]: [MySQL] 40,967 pass(es). View
Cottser’s picture

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

Thanks @benjf! Sending to the bot.

Cottser’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...