<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Punya Ruchal</title>
    <description>The latest articles on DEV Community by Punya Ruchal (@punyaruchal).</description>
    <link>https://dev.to/punyaruchal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F611310%2Fd4f0b203-538f-448d-b003-ba7e5ffeb797.jpg</url>
      <title>DEV Community: Punya Ruchal</title>
      <link>https://dev.to/punyaruchal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/punyaruchal"/>
    <language>en</language>
    <item>
      <title>Drupal 8^9: Make a node's field accessible in page.html.twig</title>
      <dc:creator>Punya Ruchal</dc:creator>
      <pubDate>Thu, 27 May 2021 11:36:46 +0000</pubDate>
      <link>https://dev.to/punyaruchal/drupal-8-9-make-a-node-s-field-accessible-in-page-html-twig-1mjf</link>
      <guid>https://dev.to/punyaruchal/drupal-8-9-make-a-node-s-field-accessible-in-page-html-twig-1mjf</guid>
      <description>&lt;p&gt;We can render the node fields value in &lt;code&gt;page.html.twig&lt;/code&gt; on two different ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  First method
&lt;/h2&gt;

&lt;p&gt;Without defining node pre-processing in &lt;code&gt;mytheme.theme&lt;/code&gt; file. We can render a field's default value inside page.html.twig, using the following Twig variable as in &lt;code&gt;{{ node.field_some_name.value }}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you have an image field, and want to render the URL to the image stored in that field inside the page.html.twig template, you would use &lt;code&gt;{{ file_url(node.field_some_image.entity.fileuri) }}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, if you want to render the title of a node inside the page.html.twig template, you would use &lt;code&gt;{{ node.label }}&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Second method
&lt;/h2&gt;

&lt;p&gt;We had a use case where an image field needed to be used as a background image in the scope of &lt;code&gt;page.html.twig&lt;/code&gt;. This is pretty simple with the use of &lt;code&gt;template_preprocess_page()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To get the image field into the page variable use this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Implements template_preprocess_page().
 */

function MYTHEME_preprocess_page(&amp;amp;$variables) {
  // Define {{ field_image }} field value available in the template.  
  if (isset($variables['node'])) {
    $variables['page']['image_in_page'] = $variables['node']-&amp;gt;field_image-&amp;gt;view();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, print the image in page.html.twig with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ page.image_in_page }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic above isn't checking whether the field_image exists, but should get you close.&lt;/p&gt;

&lt;p&gt;Thanks for the assist: &lt;br&gt;
&lt;a href="https://www.computerminds.co.uk/articles/rendering-drupal-8-fields-right-way"&gt;https://www.computerminds.co.uk/articles/rendering-drupal-8-fields-righ…&lt;/a&gt;&lt;/p&gt;

</description>
      <category>drupal</category>
      <category>twig</category>
      <category>template</category>
    </item>
  </channel>
</rss>
