Score:0

How set html 'lang' to match node 'langcode'?

id flag

I would like to change a node's html 'lang' from site's default to the node's 'langcode'.

For example, if I create a node and while creating I select French as the language, I would like the page's html 'lang' to "fr"

So far I have this:

  /** @var \Drupal\Core\Template\Attribute $html_attribute */
  $html_attribute = $variables['html_attributes'];
  $node_langcode = $node->get('langcode')->value;
  if ($html_attribute->hasAttribute("lang")) {
    $html_attribute->removeAttribute('lang');
    $html_attribute->setAttribute('lang', $node_langcode);
  }
Score:0
id flag

This code worked properly

if(!empty($node) && $node instanceof NodeInterface) {
    /** @var \Drupal\Core\Template\Attribute $html_attribute */
    $html_attribute = $variables['html_attributes'];
    $node_langcode = $node->get('langcode')->value;
    if ($html_attribute->hasAttribute("lang")) {
      $html_attribute->removeAttribute('lang');
      $html_attribute->setAttribute('lang', $node_langcode);
    }
  }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.