Comments

Fabianx created an issue. See original summary.

David_Rothstein’s picture

Status: Postponed » Active

Don't think there's a reason for this one to be postponed (currently the Drupal 7 issue is marked postponed on it).

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.

vmachado’s picture

Status: Active » Needs review
StatusFileSize
new1.47 KB

Patch attached.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs tests

RTBC - Though committers very likely want tests for this, so setting to RTBC, so they can confirm that assumption.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Assumption confirmed :)

joelpittet’s picture

@Fabianx, I was trying to write tests but ran to a question. Should these callables support suggestions some how?

Example in core tests that I was looking to add too is test_theme_theme_test_function_suggestions__theme_override

fabianx’s picture

I think you would only add those manually to the theme registry via hook_theme_registry_alter() as we only support auto-detection for functions really.

lauriii’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.85 KB
new5.32 KB

Created some tests for this

lauriii’s picture

StatusFileSize
new5.31 KB
new484 bytes

The last submitted patch, 9: allow_the_use_of-2760659-9-test-only.patch, failed testing.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Cool thanks @lauriii besides an extra linebreak that works to demonstrate the feature's functionality.

lauriii’s picture

StatusFileSize
new4.62 KB
new942 bytes

Removed the change for the theme functions since they are deprecated.

lauriii’s picture

StatusFileSize
new4.65 KB
new1.06 KB
fabianx’s picture

RTBC + 1

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: allow_the_use_of-2760659-14.patch, failed testing.

manuel garcia’s picture

+++ b/core/modules/system/tests/modules/theme_test/src/ThemeTestPreprocess.php
@@ -0,0 +1,19 @@
+    $variables['foo'] = 'Make Drupal full of kittens again!';

+1

fabianx’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC, the test fail was unrelated.

alexpott’s picture

StatusFileSize
new607 bytes
new720 bytes
+++ b/core/lib/Drupal/Core/Theme/ThemeManager.php
@@ -283,8 +283,8 @@ public function render($hook, array $variables) {
-        if (function_exists($preprocessor_function)) {
-          $preprocessor_function($variables, $hook, $info);
+        if (is_callable($preprocessor_function)) {
+          call_user_func_array($preprocessor_function, array(&$variables, $hook, $info));

Do we care about the performance difference here?

I did some quick profiling with the script attached.

PHP 7.0.7 (cli) (built: May 26 2016 16:01:05) ( NTS )
CUFA: 0.30433106422424
Magic call: 0.30942106246948

PHP 5.6.23 (cli) (built: Jun 26 2016 13:17:47)
CUFA: 1.8177201747894
Magic call: 0.77650499343872

PHP 5.5.18 (cli) (built: Nov  3 2014 09:22:08)
CUFA: 1.9406440258026
Magic call: 0.77947521209717

The good thing is this change will have little difference in PHP7 afaics. I guess the question is how many times do we call a preprocess function on a page? Definitely not the 3,000,000 calls of the script. On perhaps a more realistic check on PHP5.5 where $iterations is 100 the script results in:

PHP 5.5.18 (cli) (built: Nov  3 2014 09:22:08)
CUFA: 0.00021886825561523
Magic call: 0.00011205673217773

So double not very much.

fabianx’s picture

+++ b/core/lib/Drupal/Core/Theme/ThemeManager.php
@@ -283,8 +283,8 @@ public function render($hook, array $variables) {
+        if (is_callable($preprocessor_function)) {
+          call_user_func_array($preprocessor_function, array(&$variables, $hook, $info));
         }

The important part is the is_callable(), which is also already a little bit slower.

We could also do:

if (function_exists()) {
  $function();
}
elseif (is_callable()) {
  c_u_f_a(...);
}

The only advantage of c_u_f_a is that also 'Class::method' works consistently on PHP 5.5, too, but I am not sure we need / want to support that.

['Class', 'method'] works also for $function() and Drupal 7 will have only this callable variant and not use c_u_f_a at all (for BC reasons).

xjm’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs change record
CUFA: 0.00021886825561523
Magic call: 0.00011205673217773

To me this doesn't look worth micro-optimization, but the proposed code flow in #20 also looks fine see below. I pinged catch for a second opinion and he said if it's negligible on PHP7 he doesn't mind (but also added that he's looking forward to deprecating preprocess functions as well as theme functions).

@cilefen pointed out that we support callables elsewhere as well, e.g. in Form API. I looked up how we do that. FormStateInterface::prepareCallback() accepts a callback specified as ::methodName(). The implementation:

  public function prepareCallback($callback) {
    if (is_string($callback) && substr($callback, 0, 2) == '::') {
      $callback = [$this->getFormObject(), substr($callback, 2)];
    }
    return $callback;
  }

So that is a bit more hardcoded than what we want to support here.

@cilefen and I also agreed that it would be good to add a change record for this, especially if we are not adding it for deprecated theme functions.

xjm’s picture

@catch also said:

I don't like the elseif idea because then if a site actually uses the feature there'll be a load of missed function_exists() on top.

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

markhalliwell’s picture

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

Re: perf

Considering that PHP 5 support will be officially removed in March, perhaps this should wait for 8.7.x? That way we don't have to worry about it. I mean, we've already waited many years for this very annoying issue to get fixed, what's a few more months :D

I would also argue that that is actually now a "bug" of 8.x. Almost every callback in 8.x allows for various types of callables (including services) due to its heavy OO nature. This limitation/drupalism was simply inherited from the previous 7.x theme system and never properly fixed when migrated from procedural code to classes.

markhalliwell’s picture

Oh and #2869859: [PP-1] Refactor theme hooks/registry into plugin managers will likely completely remove the need for any arbitrary functions like this anyway, so this particular issue probably won't be much of one in the future anyway (except in BC cases).

markhalliwell’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

anya_m’s picture

Issue tags: -Needs reroll
StatusFileSize
new4.94 KB

Rerolled

alexpott’s picture

Still needs a change record - needs work for this. I'm +1 on the change - brings things into line.

I had a quick think about if this widens any security concerns because theme callbacks have been involved in recents security advisories but that's when they can be part of the render array and these cannot.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lemming’s picture

Added change record for the issue - https://www.drupal.org/node/3266641

yogeshmpawar’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new5.03 KB
new1.43 KB

Resolved CSpell errors & removed change record tag from this issue.

Status: Needs review » Needs work

The last submitted patch, 39: 2760659-39.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new5.05 KB
new740 bytes

Updated patch will fix test failures.

Status: Needs review » Needs work

The last submitted patch, 41: 2760659-41.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new5.01 KB
new718 bytes

Missed one test failure in previous patch so resolved in this patch.

lemming’s picture

Patch #43 is working for me on Core 9.3.

+1

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

As noted in #31 this was only waiting on a CR, which was added in #38. Moving back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed fb3dc749ac to 10.0.x and 9a265629be to 9.4.x. Thanks!

  • alexpott committed fb3dc74 on 10.0.x
    Issue #2760659 by lauriii, yogeshmpawar, vmachado, anya_m, alexpott,...

  • alexpott committed 9a26562 on 9.4.x
    Issue #2760659 by lauriii, yogeshmpawar, vmachado, anya_m, alexpott,...
joachim’s picture

Does this really allow all PHP callables? Even anonymous functions?

Status: Fixed » Closed (fixed)

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