0

I've been trying to look for a way on how to optimize theme suggestions for different languages. The code below is a snippet of what I have been working on.

One is for english and one is for Spanish. Is there a way I can minimize this into a single code instead of breaking it down for multiple languages?

// Template based on current url
// News Press Room

if ($current_url == '/iom/news-press-room') {
    $suggestions[] = 'page__news_press_room';
}

// News Press Room(Spanish)

if ($current_url == '/iom/es/news-press-room') {
    $suggestions[] = 'page__news_press_room';
}
  • Is this Drupal 8 or 7? – Ismail Cherri Sep 17 '18 at 7:39
  • @IsmailCherri yes, this is Drupal 8 – clestcruz Sep 17 '18 at 7:43
  • 1
    I think the question title could be misunderstood. If your code should not depend on the language you could try to get the information from the current route instead of the current path. Which is preferable in most cases anyway, because normally a page is based on a resolved route, not on a path. – 4k4 Sep 17 '18 at 12:10
  • 1
    You didn't provide any details about the content you display. For example if this is a node, then you can get the node id from the raw route parameter node, independently from the alias or language prefix in the path. – 4k4 Sep 18 '18 at 11:12
  • 1
    Yes, because there may be dozens of paths leading to one node. – 4k4 Sep 18 '18 at 11:37
1

Check the example in hook_theme_suggestions_alter() for language (second one below)
You can add more specific suggestions by appending the content type.

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.