Twig https://purushotam.in/ en Accessing Translated Entity in Views Twig | Drupal 8 https://purushotam.in/notes/accessing-translated-entity-views-twig-drupal-8 <span property="schema:name">Accessing Translated Entity in Views Twig | Drupal 8</span> <span rel="schema:author"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">purushotam</span></span> <span property="schema:dateCreated" content="2021-07-12T23:49:18+00:00">Tue, 07/13/2021 - 01:49</span> <div property="schema:text" class="text-content field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Though ideally if we use the views correctly and override them well, Drupal Views itself take care of getting the translated version as per configurations of the view. For details check this: <a href="https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%21views%21field%21EntityField.php/function/EntityField%3A%3AgetValue/8.8.x">EntityField::getValue</a></p> <p>But if somehow we wanna get translated version of the entity in a views twig, we will have to get the current language and then call <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21ContentEntityBase.php/function/ContentEntityBase%3A%3AgetTranslation/8.8.x">ContentEntityBase::getTranslation</a> on the entity of the views row. And there are various ways of getting the current language in the views-view.html.twig:</p> <p>#1 Use preprocess to add current language</p></div> Mon, 12 Jul 2021 23:49:18 +0000 purushotam 50 at https://purushotam.in Making a variable available (as global) in all twig files in Drupal https://purushotam.in/notes/making-variable-available-global-all-twig-files-drupal <span property="schema:name">Making a variable available (as global) in all twig files in Drupal</span> <span rel="schema:author"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">purushotam</span></span> <span property="schema:dateCreated" content="2021-03-30T23:28:39+00:00">Wed, 03/31/2021 - 01:28</span> <div property="schema:text" class="text-content field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Most of us know, the ideal way of making a variable available in a specific twig file is to use hook_preprocess_HOOK. But what if we have a requirement that we need a kind of global variable that should be accessible in any twig file. So, that we are able to use it wherever we want to, without additional pain.</p> <p>The best part of Drupal is, everything is possible. But at the same time, there is a trick, you always have more than one way of doing things. And now it's in your hands to pick up the best suitable performant way for your solution.</p> <p>So, over here you have the following solutions (which I can instantly think for this problem statement):</p></div> Tue, 30 Mar 2021 23:28:39 +0000 purushotam 48 at https://purushotam.in PHP function in twig - EASY! Ever tried TWIG in PHP function? https://purushotam.in/notes/php-function-twig-easy-ever-tried-twig-php-function <span property="schema:name">PHP function in twig - EASY! Ever tried TWIG in PHP function?</span> <span rel="schema:author"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">purushotam</span></span> <span property="schema:dateCreated" content="2021-02-27T22:20:56+00:00">Sat, 02/27/2021 - 23:20</span> <div property="schema:text" class="text-content field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>I was working on issue queue of <a href="https://www.drupal.org/project/views_extras">views extras</a> module and came across a requirement: Add twig support: <a href="https://www.drupal.org/project/views_extras/issues/2948514">https://www.drupal.org/project/views_extras/issues/2948514</a>. For a moment I was helpless, I mean I always had the requirement to use PHP function in twig but never thought in the reverse direction and to resolve that issue I need to know how we can support twig variables in PHP.</p> <p>Now, <em>this was interesting</em>, so I began my search with finding out how Drupal handles output using twig how it replaces variables and all. I went into the code to find that but soon I changed my mind and found the solution another way.</p></div> Sat, 27 Feb 2021 22:20:56 +0000 purushotam 26 at https://purushotam.in