Jump to content

How to set same title to every language/page


steveooo
 Share

Recommended Posts

Hello,

when using the default multi-language module, you can set different titles for each language on a page.

The logic is: the default language is the source and every non-default language inherits from it if empty.

For a use case I have, I need to update all title's on a page for every language via the API.

I tried a little with this code but it doesn't work (with no errors):

$globalTitle = 'Test';
foreach ($languages as $language) {
    $page->set('title'.$language->id, $globalTitle);
}
Link to comment
Share on other sites


$globalTitle = 'Test';

foreach ($languages as $language) {

        $page->of(false);

        $page->setLanguageValue($language, 'title', $globalTitle);

        $page->save('title');

}

Link to comment
Share on other sites

I am using following code in my module and it still does not work...

    protected function syncLangProperty($page, $property) {
        if ($this->modules->isInstalled('LanguageSupport') &&
            !empty($page->{$property})) {
            $defaultValue = $page->{$property};

            foreach ($this->languages as $language) {
                $page->of(false);
                $page->setLanguageValue($language, $property, $defaultValue);
                $page->save($property);
            }
        }

        return $page;
    }
Link to comment
Share on other sites

I cannot test it but in module scope you maybe cannot use the $languages api. Try with wire('languages'). Do you have debug mode on?

Try this one

    protected function syncLangProperty(Page $page, $property) {
        $languages = $this->wire('languages');
        if ($this->wire('modules')->isInstalled('LanguageSupport') &&
            !empty($page->getLanguageValue($languages->getDefault(), $property))) {
            $defaultValue = $page->getLanguageValue($languages->getDefault(),$property);
            foreach ($languages as $language) {
                $page->of(false);
                $page->setLanguageValue($language, $property, $defaultValue);
                $page->save($property);
            }
        }
        return $page;
    }
Edited by kixe
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...