Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessing function never called

Tags:

php

drupal

I'm very new Drupal Developer, and I would like to know why some of my preprocess function are never called. At the beginning, I only used one single preprocess function.

// Before
function my_theme_preprocess_paragraph(array &$variables): void {
  if ($variables['paragraph']->bundle() == 'cta') {
    ...
  }
}

// Now
function my_theme_preprocess_paragraph__cta(array &$variables): void { 
  ...
}

I'd like to change the organization, so that I don't have one big function, but several small ones. But the function I use now is never called by Drupal.

I've checked the machine names of my paragraphs as well as my file call, and everything seems to work correctly on this side. I've also cleared the cache several times using the drush cr command.

Anyone knows how fix it ?

Thanks

like image 639
Nicolas Avatar asked Sep 03 '25 04:09

Nicolas


1 Answers

Drupal does not call your new function because it looks for the other's name. However, you can do something as follows (I give detailed explanation later on):

<?php

function my_theme_preprocess_paragraph_f1() {
    echo "f1";
}

function my_theme_preprocess_paragraph_f2() {
    echo "f2";
}

function my_theme_preprocess_paragraph_cta() {
    echo "cta";
}

function my_theme_preprocess_paragraph_f3() {
    echo "f3";
}


function my_theme_preprocess_paragraph(array &$variables) {
    $supported_my_theme_preprocess_paragraphs = ['f1', 'f2', 'cta', 'f3'];
    if (in_array($variables['paragraph']->bundle(), $supported_my_theme_preprocess_paragraphs)) {
        $functionName = "my_theme_preprocess_paragraph_" . $variables['paragraph']->bundle();
        $functionName();
    }       
}

class DummyTest {
    public function bundle() {
        return 'cta';
    }
}

$vars = [
    'paragraph' => new DummyTest
];

my_theme_preprocess_paragraph($vars);

Explanation:

  • I have created several my_theme_preprocess_paragraph_-like functions, whose name differs in their name and each of them are dummy functions, for the space of this exercise they just echo their suffix, you can change the logic to the one you prefer
  • $supported_my_theme_preprocess_paragraphs is a whitelist of expected/supported suffixes that we allow the caller to trigger, so no funky things are allowed, like calling similarly-looking functions by the caller
  • if the suffix is supported
  • then we compute $functionName based on the bundle
  • and call the function by its dynamic name
  • for the purpose of the test I have created a DummyTest class with a dummy bundle function and a $vars array that I pass by address to my_theme_preprocess_paragraph which will handle the desired function

The really good thing is that if you add n additional functions, then from the perspective of my_theme_preprocess_paragraph you only need to adjust the suffixes array and the function will never become more complex than it currently is.

like image 189
Lajos Arpad Avatar answered Sep 05 '25 00:09

Lajos Arpad