0

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);
  }

1 Answer 1

0

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);
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.