0

I have a page(node) with a certain ID. On the front page, I want to pull a field called field_image1 and field_link1.

To do that I added the following to my themename.theme:

function themename_preprocess_page(&$variables) {

    $variables['vanilla_spice'] = Node::load(61);

    $variables['variable'] = "tetsing the variable";

}

page--front.html.twig:

{{ kint(vanilla_spice) }}
{{ kint(variable) }}

When I visit the page, the variable is null :(

How can I load this information inside the variable?

Note: I based myself in this answer here, but even if I assign a normal string to the vanilla_spice variable

$vanilla_spice = "tetsing the variable";

I still see a null.

1

First, you are using the wrong hook. peeprocess_node won't send new variables into the page template. You need preprocess_page for that. The page template is not the same as the node template.

Second you are not assigning any new variables. You have to add or alter what is in $variables array or they are not passed to the template.

$variables['vanilla_spice'] = "testing the variable";

| improve this answer | |
  • You are right, its preprocess_page and not a node. But I still see a null. Mind answer with an example? right now this is more of a comment – user agent Feb 3 at 13:36
  • I added one. You have to add it to the variables array. Otherwise the twig template doesn't know what you are talking about. – Kevin Feb 3 at 13:38
  • $variables['variable'] = "tetsing the variable"; when i go to the template (page--front..) {{ kint(variable) }} I still see a null – user agent Feb 3 at 13:41
  • I edited my question so you can see – user agent Feb 3 at 13:42
  • I ended up noticing that my .theme file was in the wrong directory. It now works. – user agent Feb 3 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.