8 – How to get drupal to check for a twig template in the active theme folder before checking the module folder?

I’m developing a contrib module for Drupal 8 that sends email notifications to group members when new content has been posted in groups they have subscribed to. The module defines some new view modes for nodes and comments. It then uses these new view modes to render the contents of the emails it sends out. The module defines a theme hook suggestion for each view mode and supplies a twig template for each view mode.

It works fine as is. The twig templates from within the module are utilized without a problem. What I’m not understanding is why copies of these templates placed within the active theme are not utilized. I’m familiar with the process of copying an existing twig template into a theme to be modified. But no matter what I do the template from the module is always used in this case. It was my understanding Drupal searches for suitable twig templates in the theme first and then works backward but perhaps my module is missing something.

Here are code samples for more details.

Define the view mode for comments – in modules/contrib/group_subscription/config/install/core.entity_view_mode.comment.group_subscription_immediate

langcode: en
status: true
dependencies:
  module:
    - comment
id: comment.group_subscription_immediate
label: 'Group Subscription Immediate (comment)'
targetEntityType: comment
cache: true

Define the theme hook suggestion – in modules/contrib/group_subscription/group_subscription.module

/**
 * Implements hook_theme()
 */
function group_subscription_theme($existing, $type, $theme, $path) {
  return (
    'comment__group_subscription_immediate' => (
      'base hook' => 'comment',
    )
  );
}

/**
 * Implements hook_theme_suggestions_comment_alter()
 */
function group_subscription_theme_suggestions_comment_alter(array &$suggestions, array $variables) {
  if ($variables('elements')('#view_mode') == 'group_subscription_immediate') {
    $suggestions() = 'comment__group_subscription_immediate';
  }
}

Please let me know if there is anything else I can provide to help me figure this out. I’ve been working with Drupal for years but am still relatively new to D8 and this is my first attempt at creating a module to give back. Thanks in advance!

8 – Render custom twig in a specific language

I need to specify the language,
which the drupal renderer uses,
when generating a custom render array.

$renderArray = (
    "#theme" => "DNMBE_email",
    "#body" => (
        "#theme" => "DNMBE_emailBody_groupMessage",
        "#author" => $groupMessage->author,
        "#group" => $groupMessage->group,
        "#message" => $groupMessage->message,
    ),
    "#leadin" => $partials->leadin,
    "#leadout" => $partials->leadout,
);

$render = Drupal
    ::service('renderer')
    ->renderRoot($renderArray);
$html = (string)$render;

#theme parameters resolve to twigs, which, among other things, contain {{ "From"|t }} markup which is incorrectly rendered as From.

I already posted a similar question, which was marked as duplicate, with the answer suggesting using Drupal::languageManager()->setConfigOverrideLanguage($language). I do not get the expected result.

I verified that /admin/config/regional/translate has got the translated string, case-sensitive. I also use ('#cache')('max-age') = 0 and I don’t “precalc”-store any Drupal objects.

theming – twig template suggestion for comment reply form

there is no template suggestion for a comment reply form in Drupal 8, how can i create one?
i am not talking about the reply form displayed in the node for which Drupal suggest a template :

field--node--comment--article.html.twig

I’am talking about the form in the redirection page after we click “reply” in a comment. there is no template suggestion, only this template is suggested

form.html.twig

Can you please tell me how can i create a specific template in my .theme file of my theme.

8 – how to display a media description in a twig template?

i want to display the media description available in the file field of a media ( media of type video, audio, image, etc…), it is an option to activate:

Enable Description field

The description field allows users to enter a description about the uploaded file.

i already print other fields that i want in the media twig template, but i can not find the value of that “description” field attribute, because it’s not a field, but some sort of attribute of the file field.

theming – How to add variable (field added in commerce_order) in twig template?

I have created a custom field (selection list) in Commerce Order / Checkout

  • name machine : field_jour
  • extract url : config/order-types/default/edit/fields/commerce_order.default.field_jour

I would like to customize Mail Template when order is completed
(file = modules/commerce/modules/order/templates/commerce-order-receipt.html.twig)

All is OK for me but what is the name of this variable ?
I have tried :

  • {{ order_entity.field_jour.value }}
  • {{ commerce_order.default.field_jour.value }}
  • {{ commerce_order.field_jour.value }}

But no value appear in my Mail…

I have installed modules such as “Value” or “Twig Field Value” which are supposed to be simple to use and useful to know the name of the variable… But I don’t understand how to use them.

I’m lost…

Is someone can help me please ?
Thank you in advance

views – Get the range of a list to display in the html twig node

D8 asks:

I need to show the population range of the country. How can I display the country's own ranking in the country node (for example: 4 for Italy)?

enter the image description here

What I tried:
I tried to get range through the block, but seeing the result counter is not a solution for this approach. Forever show 1 as expected

enter the image description here

Need help:

I think the only workaround left is to show it in the node twig template, but I couldn't know how to achieve it.

Any advice please? Thank you

Drupal 8 Twig Template: how to get the reference entity of a referenced node

I am struggling for a couple of days with my problem now and I have no idea what else I can do to get to a stage of enlightenment.

In Drupal 8 I have a content type called "Launch" and a content type "Article" that, among others, contains Launch Reviews. So in versions I created an entity reference field called "Review" which references the content type of the article.

For index pages, you could create article views and there you could integrate the fields of the reference publication entity through the Relationships section in view management. There, the reverse direction of a relationship is not a problem.

But how do I access the version fields in a node: article.html.twig template?

Theming: how to reference one computed twig to another computed twig

I am creating a web form that collects data on the number of private offices a company wants in its workspace and then performs calculations based on those numbers. I can easily do the basic calculations for those calculations. For example:

    ceo_office_qty:
      '#type': number
      '#title': 'Quantity Needed'
      '#admin_title': 'CEO Office Qty'
      '#min': 0
    ceo_office_total:
      '#type': computed_twig
      '#title': 'Total SF'
      '#admin_title': 'CEO Office Total'
      '#template': '{{(data.ceo_office_qty * 300)}} SF'
      '#store': true
      '#ajax': true

Then I want to combine several of these calculated twigs to get a total. Obviously I can go back to math in a new calculated branch, but it would be simpler and more elegant (also less error prone) to be able to reference data from calculated fields. For example:

  net_usable_sf:
    '#type': computed_twig
    '#title': 'Net Usable SF'
    '#admin_title': 'Net Usable SF'
    '#template': '{{(data.ceo_office_total + data.manager_office_total + data.std_office_total + data.small_office_total)}} SF'
    '#store': true
    '#ajax': true

Any ideas why the above doesn't work? This only gives me the number of fields added together, instead of your data, which is what I want.

Any help is really appreciated. Thank you!

Theming: Twig escapes labels when raw value is used in template

You can use the following code below:

{{content.field_myfield.0|render|striptags|trim}}

render: striptags filter removes SGML / XML tags and replaces them
Adjacent blanks by a space. You can also provide labels that
must not be stripped:

{{ content.field_myfield.0|striptags('

')}}

trim: The trim filter removes whitespace (or other characters) from the beginning and end of a string:

{{ '  I like Twig.  '|trim }}

{# outputs 'I like Twig.' #}

{{ '  I like Twig.'|trim('.') }}

{# outputs '  I like Twig' #}

{{ '  I like Twig.  '|trim(side='left') }}

{# outputs 'I like Twig.  ' #}

{{ '  I like Twig.  '|trim(' ', 'right') }}

{# outputs '  I like Twig.' #}

The example above used from the twig website.