Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Retrieve a given markdown language from plugin

Started by Muut Archive 9 years ago · 10 replies · 564 views
9 years ago

Hi :)

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

9 years ago

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

9 years ago

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.

9 years ago

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

9 years ago
PHP
$this->grav['pages']->all();

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

9 years ago

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.

9 years ago

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

9 years ago

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

PHP
$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

PHP

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

on the Languages class, and call

PHP
$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());
9 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1344 9 years ago
Archive · by Muut Archive, 9 years ago
2 930 9 years ago
Archive · by Muut Archive, 9 years ago
2 4058 9 years ago
Archive · by Muut Archive, 9 years ago
1 2943 9 years ago
Archive · by Muut Archive, 9 years ago
3 1116 9 years ago