Getting language of page where block is

Permalink
How do I get the language of the page my block is in?

Let's say I have a block 2 times inserted. Once in my English tree, once in the French tree. How do I in the block find out on which tree we are.

I need it to feed the search index the correct texts for each language for a block. That's also why I cannot use Localization::activeLanguage() as this seems not to work correctly in this case. So I figure I either get the page language from the Block or Page ID somehow.

 
linuxoid replied on at Permalink Reply
linuxoid
Kiesel replied on at Permalink Reply
Thanks, I saw that, but I don't need the default language, I need the language of the currently active page, given from a block that is added to this page.
linuxoid replied on at Permalink Reply
linuxoid
I'd guess it should be getLanguage() instead of getDefaultLanguage(). Please have a look here:

https://documentation.concrete5.org/api/8.4.3/Concrete/Core/Multilin...

Check the concrete\blocks\switch_language\ controller
Kiesel replied on at Permalink Reply
Sorry, but I'm not sure how to implement that.

I work from the controller of a block and that code piece seems not to work (5.7).

I know how to get the page object inside the block: $page = \Page::getCurrentPage();
So naturally a page is either in this or that language, so the page object should allow me somehow to read out the language.

How?

$page->.......
Kiesel replied on at Permalink Reply
I figured out a solution with the help of this old thread:
https://www.concrete5.org/community/forums/customizing_c5/get-page-u...

Basically my problem was the automated process that indexes the search and utilizes the method getSearchableContent() in every block.

This process does not work correctly when you feed a block dynamic content depending on the language of the page, as the process is not calling these pages with the set language of the page in question, but just with the default language. That means it's not possible to use anything like Localization::activeLanguage().

So all I had was the Block ID. I had to get the pageID over a direct db call (I'm sure there are methods to avoid that, but c5's documentation didn't got better in the last years and I gave up on that):

$db = \Database::connection();
$sql = "SELECT cID FROM CollectionVersionBlocks WHERE bID=?";
$vars[] = $this->bID;
$cID = $db->getOne($sql, $vars ? $vars : []);

With the help of the pageID I could get the page object and call my data in the right language to give the correct return value for getSearchableContent:

//Get Page object with the help of the cID
$page = Page::getByID($cID);
if(strpos($page->getCollectionPath(), '/en/') === FALSE){
  $faqs = $this->getFaqsByCategoryID(1);
}else{
  $faqs = $this->getFaqsByCategoryID(2);
}
if (sizeof($faqs) > 0) {
  foreach ($faqs as $f) {
    $content .= $f['title'] . ' ' . $f['answer'] .  ' ';
  }
}
return $content;

Maybe that helps someone down the line.
chrisdengso replied on at Permalink Best Answer Reply
Here's my solution to this problem. Using the multilingual module.
Was facing a problem where the Localization is not correct inside the block controller, but this function gets the correct locale.

public function getPageLocale($id = null)
    {
        if (!$id) {
            $id = Page::getCurrentPage()->getCollectionID();
        }
        $c = Page::getById($id); // the page 
        $ml = \Concrete\Core\Multilingual\Page\Section\Section::getList();
        foreach ($ml as $m) {
            $tid = $m->getTranslatedPageID($c);
            if ($tid == $id) {
                return ($m->getLocale());
            }
        }
        // default to english and dk
        return "en_DK";
yrenoue replied on at Permalink Reply
Thx mate, was facing same issue, you made my day ;-)