Skip to main content
edited tags; edited title
Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

Values of node id Node values not retrieving properly in .theme and twigTwig files

Source Link

Values of node id not retrieving properly in .theme and twig files

I created a content type and have a couple of fields: a heading, links, and images. After creating a node out that content type, I want to pull parts of the node into my homepage. And for that, I used the following in my .themename.theme.

use Drupal\node\Entity\Node;

function amarula_preprocess_page(&$variables) {
    
    $nid = 62; 
    $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid); 
    
    $variables['field_vanilla_heading'] = $node->field_vanilla_heading->value;
    $variables['field_vanilla_main_content'] = $node->field_vanilla_main_content->value;
    $variables['field_vanilla_home_link'] = $node->field_vanilla_home_link->value;
    $variables['field_vanilla_home_thumbnail'] = $node->field_vanilla_home_thumbnail->value;
}

I get the value of the field_vanilla_heading in my page--front.html.twig

<h1>{{ field_vanilla_heading }}</h1>

But, the rest of the fields are giving problems.

For the main content {{ field_vanilla_main_content }}, which is long text with summary, I am seeing all the HTML in the page. How can I escape this in twig?

For the rest of the fields, kint tells me that they are null, which is not true:

{{ kint(field_vanilla_home_link) }}
{{ kint(field_vanilla_home_thumbnail) }}

Below is the list of all the field settings:

enter image description here

I can already get the node ID. But how can I get all the values of the node accordingly?