I’m re-starting work on a plug-in to gather a list of all the tags used for each of the categories defined in my blog. The taxonomylist plug-in creates arrays of all the categories and all the tags, but not which goes with which.
rhukster suggested I start with the related pages plug-in, which I am.
Testing my understanding:
For each page load Grav runs through it’s lifecycle
I would want to attach my plug-in to the onPageInitialized event, as does the related pages plug-in
My plug-in would define its own variable in its declaration, perhaps protected $categories_tags = [];
I could check the cache to see if my variable needed to be updated with:
I think the best it’s add a twig function with params for filter by tag/category/etc. For now im busy with other projects, but if is userfully for some ppl i can pause other projects and develop this
It does not seem to be caching the testing array of category_tags because each time through the loop $this->categories_tags = $cache->fetch($cache_id); returns false.
So I’ve messed something up, or misunderstand something else. But what?
asort do not need because if you have a empty array what you need to sorting?
another, the filler, i think the categories_tags have page object, not have sense save in cache only the tagname, try remove this and add pages with category tag and try again
public function onPageInitialized()
{
/** @var Cache $cache */
$cache = $this->grav['cache'];
/** @var Pages $pages */
$pages = $this->grav['pages'];
/** @var Page $page */
$page = $this->grav['page'];
/** @var Debugger $debugger */
$debugger = $this->grav['debugger'];
$this->enable(['onTwigSiteVariables' => ['onTwigSiteVariables', 0]]);
$cache_id = md5('categorymenu' . $page->path() . $cache->getKey());
$this->categories_tags = $cache->fetch($cache_id);
if ($this->categories_tags === false) {
// the array was not in the cache, so reset and rebuild it.
$this->categories_tags = array();
$debugger->addMessage("CategoryMenu Plugin cache miss. Rebuilding...");
// get all the pages in the blog
$blogPages = $page->find('/blog')->children();
if (!empty($blogPages)) {
foreach ($blogPages as $blogPage) {
$thisTaxonomy = $blogPage->taxonomy();
if (!empty($thisTaxonomy['category'])) {
foreach ($thisTaxonomy['category'] as $key => $value) {
if (!array_key_exists($value, $this->categories_tags)) {
$this->categories_tags[$value] = array();
}
if (!empty($thisTaxonomy['tag'])) {
foreach ($thisTaxonomy['tag'] as $key2 => $value2) {
if (!in_array($value2, $this->categories_tags[$value])) {
$this->categories_tags[$value][] = $value2;
}
}
}
}
}
}
}
// Sort the tag lists for each category
if (!empty($this->categories_tags)) {
foreach ($this->categories_tags as $key => $value) {
$theCategories[] = $key;
}
}
if (!empty($theCategories)) {
foreach ($theCategories as $key => $value) {
asort($this->categories_tags[$value]);
}
}
$cache->save($cache_id, $this->categories_tags);
} else {
$debugger->addMessage("CategoryMenu Plugin cache hit.");
}
}
This seems to be doing what I want, except that $this->categories_tags is seemingly never cached. I never get a cache hit after reloading a page several times.
I follow your conversation a while, but I had no time to answer… However I copied your code and tested it. It works fine. On the first request of page the message “CategoryMenu Plugin cache miss. Rebuilding…” comes up and after reloading “CategoryMenu Plugin cache hit.” . Since your code is working, you have a problem with your cache. Have you enabled the cache i.e. set
cache:
enabled: true
in your user/config/system.yaml? Maybe you have to play around with the driver or method, too. After that it is probably a good idea to clear the cache (using GPM or manually delete all contents in the cache folder).
By the way, here is an example output of your code: