Problem/Motivation

Theme preprocessor callbacks are still stuck with using global functions:

  $function = $form['whatever callback'];
  $result = $function($form, $form_state);

This inhibits the usage of proper callbacks and makes mixing OOP with D7 a complete mess.

Proposed resolution

Change the way these callbacks are invoked.

Remaining tasks

Review

User interface changes

None

API changes

None.

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

david_garcia created an issue. See original summary.

david_garcia’s picture

Status: Active » Needs review
FileSize
777 bytes

That should do it.

izaaksom’s picture

Status: Needs review » Reviewed & tested by the community

A step further into OOP, Thanks. :D! Fix worked perfectly.

Fabianx’s picture

Issue tags: +Modern PHP, +Pending Drupal 7 commit

Nice work, pending commit!

stefan.r’s picture

Issue tags: +Needs change record
David_Rothstein’s picture

Fabianx’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Pending Drupal 7 commit +Drupal 7.50 target
Parent issue: » #2652814: Allow the use of callbacks for element value callbacks and batch API redirect callbacks

The is_callable() part we definitely can do, the $function => call_user_func_array() we should remove for now.

Fabianx’s picture

Issue tags: +Novice, +php-novice

Adding Novice tag as it should be simple to just leave is_callable().

  • Fabianx committed d73dc6b on 7.x
    Issue #2652834 by david_garcia, Fabianx: Allow the use of callbacks...
Fabianx’s picture

Status: Needs work » Fixed
Issue tags: -Needs change record, -Novice, -php-novice

Committed and pushed to 7.x! Thanks!

I fixed the removal of the call_user_func_array() on commit and also made it possible to use any callable for theme functions.

diff --git a/includes/theme.inc b/includes/theme.inc
index ff54d6e..991beea 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -1119,7 +1119,7 @@ function theme($hook, $variables = array()) {
     foreach (array('preprocess functions', 'process functions') as $phase) {
       if (!empty($info[$phase])) {
         foreach ($info[$phase] as $processor_function) {
-          if (function_exists($processor_function)) {
+          if (is_callable($processor_function)) {
             // We don't want a poorly behaved process function changing $hook.
             $hook_clone = $hook;
             $processor_function($variables, $hook_clone);
@@ -1157,7 +1157,7 @@ function theme($hook, $variables = array()) {
   // Generate the output using either a function or a template.
   $output = '';
   if (isset($info['function'])) {
-    if (function_exists($info['function'])) {
+    if (is_callable($info['function'])) {
       $output = $info['function']($variables);
     }
   }

  • Fabianx committed ed845cd on 7.x
    Revert "Issue #2652834 by david_garcia, Fabianx: Allow the use of...
Fabianx’s picture

Status: Fixed » Postponed

This is still present in D8 and needs to be fixed there first.

Fabianx’s picture

Title: Allow the use of callbacks instead of global functions in theme processor functions » [D7] Allow the use of callbacks instead of global functions in theme processor functions
Fabianx’s picture

Fabianx’s picture

Issue tags: -Drupal 7.50 target +Drupal 7.60 target
Fabianx’s picture

Category: Feature request » Task

I think this is small enough to be a task.

Ayesh’s picture

Adding the issue that we collectively add tests for these changes (correct me if I'm wrong please).

Ayesh’s picture

MustangGB’s picture

Issue tags: -Drupal 7.60 target +Drupal 7.70 target

Moving to 7.70 for now as waiting for D8 commit.