Problem/Motivation

Theme suggestions of search result suggestions (theme hook: search_result) and views ui preview section suggestions (theme hook: views_ui_view_preview_section) are not always found.

That's because they are in a theme include file, which is not allowed for implementations of hook_theme_suggestions_HOOK() and related hooks. If the file happens to be included previously, it will get picked up but this is coincidental.

Proposed resolution

  1. Move search_theme_suggestions_search_result() to search.module
  2. Move views_ui_theme_suggestions_views_ui_view_preview_section() to views_ui.module
  3. Add docs to hook_theme_suggestions_HOOK(), hook_theme_suggestions_alter(), and hook_theme_suggestions_HOOK_alter() to mention that implementations must be placed in *.module files.

Note that because code cannot be unloaded, it is not possible to write tests for this.

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tstoeckler created an issue. See original summary.

tstoeckler’s picture

A note on the coincidental part in the issue summary: I would consistently have the include files loaded on time on the first request after a cache clear but not on subsequent requests. So for people trying to reproduce this you can add a template such as search-result--node-search.html.twig to your theme and it will get loaded on the first request after a cache clear, and afterwards the default template will get picked up again.

star-szr’s picture

Thanks for this! For the docs piece *.theme as well :)

afi13’s picture

Assigned: Unassigned » afi13
tstoeckler’s picture

For the docs piece *.theme as well :)

Why yes, for theme suggestions, we should not forget those, thanks!

(As if I would remember that those things even exist... ;-) )

afi13’s picture

For review

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

AjitS’s picture

Assigned: afi13 » AjitS
Issue tags: +Needs reroll

Patch doesn't apply anymore. Assigning it to me for a reroll.

AjitS’s picture

Assigned: AjitS » Unassigned
Issue tags: -Needs reroll
FileSize
2.11 KB

Plain reroll.

Status: Needs review » Needs work

The last submitted patch, 11: theme_suggestions_may-2662574-11.patch, failed testing.

rajeevk’s picture

Status: Needs work » Needs review
FileSize
3.27 KB
2.18 KB
960 bytes

Re-rolling patch for 8.3.x

dawehner’s picture

+++ b/core/lib/Drupal/Core/Render/theme.api.php
@@ -618,6 +618,9 @@ function hook_preprocess_HOOK(&$variables) {
+ * Implementations of this hook must be placed in *.module files.
+ * Or make sure that hook implementation available at any given time.
+ *

@@ -699,6 +702,9 @@ function hook_theme_suggestions_alter(array &$suggestions, array $variables, $ho
+ * Implementations of this hook must be placed in *.module files.
+ * Or make sure that hook implementation available at any given time.
+ *

These hooks are also available in themes, see

    // Invoke hook_theme_suggestions_alter() and
    // hook_theme_suggestions_HOOK_alter().
    $hooks = [
      'theme_suggestions',
      'theme_suggestions_' . $base_theme_hook,
    ];
    $this->moduleHandler->alter($hooks, $suggestions, $variables, $base_theme_hook);
    $this->alter($hooks, $suggestions, $variables, $base_theme_hook);

given that I'm wondering whether someone could misread this documentation. It says it has to be placed in .module files, maybe change it to "must be placed in *.module or *.theme files".

artis’s picture

Version: 8.3.x-dev » 8.4.x-dev
artis’s picture

Version: 8.4.x-dev » 8.3.x-dev
greyghost’s picture

Rerolled patch 13 against current 8.3 branch w/ comment change to add "*.module or *.theme files"

pritish.kumar’s picture

Coding Standard Resolved "Short array syntax"

vegantriathlete’s picture

Issue tags: +dcco2017

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

afi13’s picture

Status: Needs review » Reviewed & tested by the community

Ok for me.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for working on this and for providing better documentation so we don't make this mistake again in the future.

+++ b/core/lib/Drupal/Core/Render/theme.api.php
@@ -613,6 +613,9 @@ function hook_preprocess_HOOK(&$variables) {
+ * Implementations of this hook must be placed in *.module or *.theme files.
+ * Or make sure that hook implementation available at any given time.

@@ -694,6 +697,9 @@ function hook_theme_suggestions_alter(array &$suggestions, array $variables, $ho
+ * Implementations of this hook must be placed in *.module or *.theme files.

There's a small grammar error here -- "or" should not start a new sentence because it's a conjunction. We can fix this by simply joining the sentences and rewording it a little:
Implementations of this hook must be placed in *.module or *.theme files, or othewise ensure that the hook implementation is always available.

rajeevk’s picture

Attaching another patch with xjm suggestion and re-rolling for 8.4.x

rajeevk’s picture

Status: Needs work » Needs review
tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Perfect, thank you!

xjm’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Render/theme.api.php
@@ -613,6 +613,9 @@ function hook_preprocess_HOOK(&$variables) {
+ * Implementations of this hook must be placed in *.module or *.theme files, or
+ * make sure that hook implementation available at any given time.

This still needs some work on the grammar. "...or must otherwise make sure that the hook implementation is available at any given time."

Thanks!

Ada Hernandez’s picture

Ada Hernandez’s picture

Status: Needs work » Needs review
Ada Hernandez’s picture

FileSize
648 bytes
3.3 KB

there were 2 comments with that (#26) correction

rajeevk’s picture

Status: Needs review » Reviewed & tested by the community

This should be fine now. Thanks.

  • xjm committed 81d1735 on 8.5.x
    Issue #2662574 by RajeevK, Adita, pritish.kumar, afi13, AjitS, greyghost...

  • xjm committed 275e571 on 8.4.x
    Issue #2662574 by RajeevK, Adita, pritish.kumar, afi13, AjitS, greyghost...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

Looks good now, thanks!

Committed and pushed to 8.5.x.

I briefly considered not backporting it to 8.4.x -- while the file location of procedural code is not considered API (https://www.drupal.org/core/d8-bc-policy#code-locations) we have accidentally disrupted things in the past by moving code between files (so we avoid changes like that in patch releases. However, since these are hook implementations, the .module file that provides them will always be available when the module is enabled -- and if the module is not enabled, the extending code would have to both be relying on a procedural code location and calling a hook implementation directly. That's two very unlikely things, neither of which are supported. So fixing the bug is definitely worth not protecting such a scenario.

So, long story short, I backported this to 8.4.x as well.

Thanks everyone!

Edit: Forgot about the issue crediting crosspost, so reverting and recommitting with the correct issue credit.

  • xjm committed acaf93a on 8.5.x
    Revert "Issue #2662574 by RajeevK, Adita, pritish.kumar, afi13, AjitS,...

  • xjm committed e314e01 on 8.4.x
    Revert "Issue #2662574 by RajeevK, Adita, pritish.kumar, afi13, AjitS,...

  • xjm committed d6fb4a5 on 8.5.x
    Issue #2662574 by RajeevK, Adita, pritish.kumar, afi13, greyghost,...

  • xjm committed 99be739 on 8.4.x
    Issue #2662574 by RajeevK, Adita, pritish.kumar, afi13, greyghost,...
xjm’s picture

There we go, added credit for substantive reviews. :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

vegantriathlete’s picture

Issue tags: -dcco2017