Jump to content

Counting active languages for a page


creativejay
 Share

Recommended Posts

Hello all. I have a question that I know must have a simple answer but I've been unable to find it in the forums or documentation.

I want to throw a styled bit of markup around the links to switch language on a given page, but I don't want to output the markup if there is only the default language available and therefor no links to output.

I was approaching this by trying to count the languages for the page which are marked in the settings tab as active, but I can't seem to find the correct syntax for that.

I'm using the basic recommended method to generate the links but nothing in these lines helped me find the answer.

foreach($languages as $language) {
	
	  // if user is already viewing the page in this language, skip it
	  if($language->id == $savedLanguage->id) continue;
	
	  // if this page isn't viewable (active) for the language, skip it
	  if(!$page->viewable($language)) continue;
	
	  // set the user's language, so that the $page->url and any other
	  // fields we access from it will be reflective of the $language
	  $user->language = $language;
	
	  // output a link to this page in the other language
	  $language .= "<li class='language' style='float: left; display: inline; padding: 1em;'><a href='$page->url'>$language->title</a></li>";
	}

I tried count($languages) but that includes inactive languages in the count. I tried count($page->viewable($language)) and a few other inventive lines with no results until I decided to finally just ask here. ;)

I'm of course open to using some other calculation to determine whether to output my code.

Appreciate the help!

Link to comment
Share on other sites

$markup = '';
foreach($languages as $language) {
	if($language->id == $savedLanguage->id) continue;
	if(!$page->viewable($language)) continue;
	$user->language = $language;
	// do not use $language here again
	$markup .= "<li class='language' style='float: left; display: inline; padding: 1em;'><a href='$page->url'>$language->title</a></li>";
}
if(strlen($markup)) $markup = "<something>$markup</something>";

 

  • Like 2
Link to comment
Share on other sites

Since PW 3.0.21 (2.8.21) you can use:

echo count($page->getLanguages());

in your case:

if (count($page->getLanguages()) > 1) {
    $markup = '';
    foreach($page->getLanguages() as $language) {
        if ($language->isCurrent()) continue;
        $markup .= "<li><a href='".$page->localUrl($language)."'>".$language->getLanguageValue($language, 'title')."</a></li>";
    }
    echo "<ul>$markup</ul>";
}

 

Edited by kixe
Requirement of PW Version
  • Like 4
Link to comment
Share on other sites

1 hour ago, kixe said:

echo count($page->getLanguages());

 

 

That's the first thing I tried but kept getting the error:

Error: Exception: Method Page::getLanguages does not exist or is not callable in this context (in /wire/core/Wire.php line 358)

Link to comment
Share on other sites

Ah, thank you! I was trying to find what version that was created for, and wasn't able to track that info down. I'm working on the latest stable because I need to launch this site the very minute it's ready. Which bugs me because there are SO many features I want to use that are in PW 3.0.x.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...