Retrieve a given markdown language from plugin

Hi :slight_smile:

From a Grav PHP plugin, I need to retrieve for a given page, let’s say /register, the markdown of a given language.

My current /register page folder has 3 languages:

  • register.en.md
  • register.fr.md
  • register.es.md

I would like to be able to read the markdown of the spanish version, because I need to access the header datas.

How can I achieve that from a Grav plugin in PHP?

Thanks

Should be just $page->language()

Hi Andy, it doesn’t work, this returns the language for the page, I want to force the $page object to return the spanish version, even if my active language is French for instance.

I tried to force the active language to be spanish using https://learn.getgrav.org/api/Grav/Common/Language/Language.html#method_setActive but it doesn’t work, the page object returned is always dependant on the current language found in the URL.

Thanks

Grav automatically falls back to spanish if you don’t have a french version of that page, as long as spanish is listed before french in the array of supported languages.

Thanks Andy,

I have the French version actually, I have 12 languages variations in total.
It’s just that when invoking $this->grav['pages']->all();, it gives an Array of pages object based on the current language.

I need to be able to reach and read the page object, or at least the markdown header for a given language. I need that to create a sitemap for different languages.

I wanted to read the markdown header for all languages variations for a given route to avoid to include this lang variation if a noindex robots was present in the markdown header.

I tried to use $this->grav['language']->setActive(lang); before invoking $this->grav['pages']->all(); but it doesn’t work.

Any solution for that? Thanks

$this->grav['pages']->all();

should work as long as cache is inactive, because the result of all() is cached internally.

Hi Flavio,

Thanks, I’ve tested this solution but it wasn’t working. I’ll try with the cache disabled to see if there’s any difference.

Hi Flavio,

Just tried this and it’s not working unfortunately:

  1. Disabled the cache and twig cache + cleared the cache using bin/grav clearcache.
  2. Navigate to a page in French: localhost/fr/foobar
  3. From my PHP plugin I did: $this->grav['language']->setActive('es');
  4. Then: dump($this->grav['language']->getActive()); // es
  5. Then: $pages = $this->grav['pages']->all();

$pages still returns pages instance in fr, even if the language is properly set to es.

Should I open an issue on GitHub? Thanks

I tried and in theory my approach works but you also need to initialize the pages again, to re-calculate all the routes etc.

Grav by design only initializes one language at a time, to avoid performance issues when run in multilanguage.

So you have to do

$this->grav['language']->setActive('it');
$this->grav['pages']->init();
dump($this->grav['pages']->all());

$this->grav['language']->setActive('en');
$this->grav['pages']->init();
dump($this->grav['pages']->all());

*BUT *

A piece of the puzzle is still missing, because when initializing the pages, Grav sets the page extensions and caches them. So we need a method like


public function resetFallbackPageExtensions() {
    $this->page_extensions = null;    
}

on the Languages class, and call

$this->grav['language']->setActive('it');
$this->grav['language']->resetFallbackPageExtensions();
$this->grav['pages']->init();
dump($this->grav['pages']->all());

$this->grav['language']->setActive('en');
$this->grav['language']->resetFallbackPageExtensions();
$this->grav['pages']->init();
dump($this->grav['pages']->all());

Related PR: https://github.com/getgrav/grav/pull/1276

P.S. after that, the site language needs to be restored to the original one.

Ok, thanks Flavio, I’ll try this when I have a moment and keep you updated.