Unverified Commit fb3dc749 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2760659 by lauriii, yogeshmpawar, vmachado, anya_m, alexpott, Fabianx,...

Issue #2760659 by lauriii, yogeshmpawar, vmachado, anya_m, alexpott, Fabianx, joelpittet, xjm: Allow the use of callbacks instead of global functions in preprocess function callbacks
parent 1760fab7
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -281,8 +281,8 @@ public function render($hook, array $variables) {
    }
    if (isset($info['preprocess functions'])) {
      foreach ($info['preprocess functions'] as $preprocessor_function) {
        if (function_exists($preprocessor_function)) {
          $preprocessor_function($variables, $hook, $info);
        if (is_callable($preprocessor_function)) {
          call_user_func_array($preprocessor_function, [&$variables, $hook, $info]);
        }
      }
      // Allow theme preprocess functions to set $variables['#attached'] and
+9 −0
Original line number Diff line number Diff line
@@ -132,6 +132,15 @@ public function preprocessSuggestions() {
    ];
  }

  /**
   * Controller for testing callable preprocess functions.
   */
  public function preprocessCallback() {
    return [
      '#theme' => 'theme_test_preprocess_callback',
    ];
  }

  /**
   * Controller for testing a namespaced class in a theme.
   */
+21 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\theme_test;

/**
 * Class to test preprocess callbacks.
 */
class ThemeTestPreprocess {

  /**
   * Preprocess callback for testing preprocess callbacks.
   *
   * @param $variables
   *   An associative array containing:
   *   - foo: Text for testing preprocess callback.
   */
  public static function preprocess(&$variables) {
    $variables['foo'] = 'Make Drupal full of kittens again!';
  }

}
+12 −0
Original line number Diff line number Diff line
@@ -48,6 +48,11 @@ function theme_test_theme($existing, $type, $theme, $path) {
      'bar' => '',
    ],
  ];
  $items['theme_test_preprocess_callback'] = [
    'variables' => [
      'foo' => '',
    ],
  ];
  $items['theme_test_registered_by_module'] = [
    'render element' => 'content',
    'base hook' => 'container',
@@ -60,6 +65,13 @@ function theme_test_theme($existing, $type, $theme, $path) {
  return $items;
}

/**
 * Implements hook_theme_registry_alter().
 */
function theme_test_theme_registry_alter(&$registry) {
  $registry['theme_test_preprocess_callback']['preprocess functions'][] = ['\Drupal\theme_test\ThemeTestPreprocess', 'preprocess'];
}

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 */
Loading