From 2b4960a1dc50a7efaa554c1251a7f57d6be191c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Tue, 7 Jun 2016 15:30:47 +0200 Subject: [PATCH 1/4] Feature: @self in filters / Extended configuration (#15) * Updated filter handling, especially for '@self' filter This streamlines the filter andling with the Grav simplesearch plugin: - Configuration can be overwritten in page header - @self filter will collect all child pages without specifying a taxonomy on child pages - Multiple taxonomy filters are now respected * Made taxonomy names configurable --- archives.php | 85 ++++++++++++++++++++++++++++++++++++++++----------- archives.yaml | 3 ++ 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/archives.php b/archives.php index 4567e12..13caeeb 100644 --- a/archives.php +++ b/archives.php @@ -11,6 +11,16 @@ class ArchivesPlugin extends Plugin { + /** + * @var string + */ + protected $month_taxonomy_name; + + /** + * @var string + */ + protected $year_taxonomy_name; + /** * @return array */ @@ -31,8 +41,11 @@ public function onPluginsInitialized() return; } + $this->month_taxonomy_name = $this->config->get('plugins.archives.taxonomy_names.month'); + $this->year_taxonomy_name = $this->config->get('plugins.archives.taxonomy_names.year'); + // Dynamically add the needed taxonomy types to the taxonomies config - $taxonomy_config = array_merge((array)$this->config->get('site.taxonomies'), ['archives_month', 'archives_year']); + $taxonomy_config = array_merge((array)$this->config->get('site.taxonomies'), [$this->month_taxonomy_name, $this->year_taxonomy_name]); $this->config->set('site.taxonomies', $taxonomy_config); $this->enable([ @@ -62,13 +75,13 @@ public function onPageProcessed(Event $event) $taxonomy = $page->taxonomy(); // track month taxonomy in "jan_2015" format: - if (!isset($taxonomy['archives_month'])) { - $taxonomy['archives_month'] = array(strtolower(date('M_Y', $page->date()))); + if (!isset($taxonomy[$this->month_taxonomy_name])) { + $taxonomy[$this->month_taxonomy_name] = array(strtolower(date('M_Y', $page->date()))); } // track year taxonomy in "2015" format: - if (!isset($taxonomy['archives_year'])) { - $taxonomy['archives_year'] = array(date('Y', $page->date())); + if (!isset($taxonomy[$this->year_taxonomy_name])) { + $taxonomy[$this->year_taxonomy_name] = array(date('Y', $page->date())); } // set the modified taxonomy back on the page object @@ -80,8 +93,17 @@ public function onPageProcessed(Event $event) */ public function onTwigSiteVariables() { + $page = $this->grav['page']; + // If a page exists merge the configs + if ($page) { + $this->config->set('plugins.archives', $this->mergeConfig($page)); + } + /** @var Taxonomy $taxonomy_map */ $taxonomy_map = $this->grav['taxonomy']; + $taxonomies = []; + $find_taxonomy = []; + $pages = $this->grav['pages']; // Get current datetime @@ -92,22 +114,49 @@ public function onTwigSiteVariables() // get the plugin filters setting $filters = (array) $this->config->get('plugins.archives.filters'); $operator = $this->config->get('plugins.archives.filter_combinator'); + $new_approach = false; + $collection = null; + + if ( ! $filters || (count($filters) == 1 && !reset($filters))){ + $collection = $pages->all(); + } else { + foreach ($filters as $key => $filter) { + // flatten item if it's wrapped in an array + if (is_int($key)) { + if (is_array($filter)) { + $key = key($filter); + $filter = $filter[$key]; + } else { + $key = $filter; + } + } + // see if the filter uses the new 'items-type' syntax + if ($key === '@self' || $key === 'self@') { + $new_approach = true; + } elseif ($key === '@taxonomy' || $key === 'taxonomy@') { + $taxonomies = $filter === false ? false : array_merge($taxonomies, (array) $filter); + } else { + $find_taxonomy[$key] = $filter; + } + } + if ($new_approach) { + $collection = $page->children(); + } else { + $collection = new Collection(); + $collection->append($taxonomy_map->findTaxonomy($find_taxonomy, $operator)->toArray()); + } + } - if (count($filters) > 0) { - $collection = new Collection(); - $collection->append($taxonomy_map->findTaxonomy($filters, $operator)->toArray()); - - // reorder the collection based on settings - $collection = $collection->order($this->config->get('plugins.archives.order.by'), $this->config->get('plugins.archives.order.dir')); - $date_format = $this->config->get('plugins.archives.date_display_format'); + // reorder the collection based on settings + $collection = $collection->order($this->config->get('plugins.archives.order.by'), $this->config->get('plugins.archives.order.dir')); + $date_format = $this->config->get('plugins.archives.date_display_format'); - // loop over new collection of pages that match filters - foreach ($collection as $page) { - // update the start date if the page date is older - $start_date = $page->date() < $start_date ? $page->date() : $start_date; + // loop over new collection of pages that match filters + foreach ($collection as $page) { + // update the start date if the page date is older + $start_date = $page->date() < $start_date ? $page->date() : $start_date; - $archives[date($date_format, $page->date())][] = $page; - } + $archives[date($date_format, $page->date())][] = $page; } // slice the array to the limit you want diff --git a/archives.yaml b/archives.yaml index ba3d898..01215ed 100644 --- a/archives.yaml +++ b/archives.yaml @@ -9,4 +9,7 @@ order: filter_combinator: and filters: category: blog +taxonomy_names: + month: archives_month + year: archives_year From efc82ac418ee2e4d787e661033b4fa42911efbe7 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Tue, 7 Jun 2016 15:31:45 +0200 Subject: [PATCH 2/4] Use the new config.plugins.archives.taxonomy_names.month param --- templates/partials/archives.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/partials/archives.html.twig b/templates/partials/archives.html.twig index b382d37..8346f75 100644 --- a/templates/partials/archives.html.twig +++ b/templates/partials/archives.html.twig @@ -2,7 +2,7 @@ {% for month,items in archives_data %}
  • - + {% if archives_show_count %} {{ items|length }} {% endif %} From 10003742156ebb0547087ee5f6d30524b55d04d9 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Tue, 7 Jun 2016 15:33:04 +0200 Subject: [PATCH 3/4] Update readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index c573ace..49fd668 100644 --- a/README.md +++ b/README.md @@ -43,10 +43,21 @@ order: dir: desc filters: category: blog +taxonomy_names: + month: archives_month + year: archives_year + ``` If you need to change any value, then the best process is to copy the [archives.yaml](archives.yaml) file into your `users/config/plugins/` folder (create it if it doesn't exist), and then modify there. This will override the default settings. +You can also list the current collection, without having to search for a taxonomy term by using + +``` +filters: + - '@self' +``` + # Template Override Something you might want to do is to override the look and feel of the archives, and with Grav it is super easy. From 027106c91dbd07f4c52ee37efed8d94aa8c067b8 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 14 Jul 2016 21:23:36 +0200 Subject: [PATCH 4/4] Prepare release --- CHANGELOG.md | 7 +++++++ blueprints.yaml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f37c302..2f04839 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v1.5.0 +## 07/14/2016 + +1. [](#improved) + * Allow to configure the taxonomy names that form the URL, instead of hardcoding `archives_month` and `archives_year` + * Allow to use @self in the filters, useful when adding the archives into a blog posts listing page + # v1.4.1 ## 05/03/2016 diff --git a/blueprints.yaml b/blueprints.yaml index de5849e..6dbe333 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Archives -version: 1.4.1 +version: 1.5.0 description: The **Archives** plugin creates links for pages grouped by month/year icon: university author: