diff --git a/CHANGELOG.md b/CHANGELOG.md index 7572988..8fdca56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ +# v2.0.0 +## 04/06/2020 + +1. [](#new) + * New per-page configuration to allow for multiple 'archives' in a single site + * Added new `page@` filter support to allow configuration from page collection [#20](https://github.com/getgrav/grav-plugin-archives/pull/20) +1. [](#improved) + * Added more sort-by options + # v1.6.1 -## 2/24/2020 +## 02/24/2020 1. [](#new) * Pass phpstan level 1 tests diff --git a/README.md b/README.md index 49fd668..5c06139 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ You should now have all the plugin files under # Usage -The `archives` plugin comes with some sensible default configuration, that are pretty self explanatory: +The `archives` plugin comes with some sensible default configuration, that are pretty self-explanatory: # Config Defaults @@ -38,24 +38,72 @@ built_in_css: true date_display_format: 'F Y' show_count: true limit: 12 +taxonomy_names: + month: archives_month + year: archives_year +#Defaults order: by: date dir: desc filters: category: blog -taxonomy_names: - month: archives_month - year: archives_year - +filter_combinator: and +#New Page-Specific Configurations +page_specific_config: + - route: '/blog' + order: + by: date + dir: desc + filters: + page@: '/blog' + filter_combinator: and ``` 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 +## Filter Types + +#### category + +The legacy approach is to provide a specific category taxonomy filter, or multiple categories: + +``` +filters: + category 'blog-post' +``` + +#### taxonomy@ + +You can use sophisticated taxonomy filtering with the same mechanism as page taxonomy filtering: + +``` +filters: + taxonomy@.tag: photography # taxonomy called tag is set to photography +``` + +or: + +``` +filters: + taxonomy@: {tag: birds, category: blog} # taxonomy with tag=birds && category=blog +``` + +#### page@ + +You can reference a specific page's collection via the page@ filter: + +``` +filters: + page@: '/blog' # Use the collection defined in the header of `/blog` page +``` + +#### self@ + +You can also list the current children, without having to search for a taxonomy term by using ``` filters: - - '@self' + - self@ # use the children defined in the current page ``` # Template Override diff --git a/archives.php b/archives.php index 4040b92..9a7e1f4 100644 --- a/archives.php +++ b/archives.php @@ -3,9 +3,12 @@ use Composer\Autoload\ClassLoader; use Grav\Common\Page\Interfaces\PageInterface; +use Grav\Common\Page\Pages; use Grav\Common\Plugin; use Grav\Common\Page\Collection; use Grav\Common\Taxonomy; +use Grav\Common\Utils; +use Grav\Common\Yaml; use RocketTheme\Toolbox\Event\Event; class ArchivesPlugin extends Plugin @@ -112,28 +115,72 @@ public function onTwigSiteVariables() /** @var PageInterface $page */ $page = $this->grav['page']; - // If a page exists merge the configs + // If a page exists merge the archive config if set if ($page) { $this->config->set('plugins.archives', $this->mergeConfig($page)); } + // See if there is page-specific configuration set (new in Archives 2.0) + $page_specific_config = $this->config->get('plugins.archives.page_specific_config'); + $archives = $archives_url = null; + + if ($page && is_array($page_specific_config)) { + foreach ($page_specific_config as $page_config) { + // Does the page config match route of this current page + if (isset($page_config['route']) && Utils::startsWith($page->route(), $page_config['route'], false)) { + $filters = $page_config['filters'] ?? (array) $this->config->get('plugins.archives.filters'); + + // get around limitation of no YAML filtering support in list field + if (is_string($filters)) { + $filters = Yaml::parse($filters); + } + + $operator = $page_config['filter_combinator'] ?? $this->config->get('plugins.archives.filter_combinator'); + $order = [ + 'by' => $page_config['order_by'] ?? $this->config->get('plugins.archives.order.by'), + 'dir' => $page_config['order_dir'] ?? $this->config->get('plugins.archives.order.dir') + ]; + $archives = $this->getArchives((array)$filters, $operator, $order); + $archives_url = $this->grav['base_url_absolute'] . $page_config['route']; + break; + } + } + } else { + // get the plugin filters setting + $filters = (array) $this->config->get('plugins.archives.filters'); + $operator = $this->config->get('plugins.archives.filter_combinator'); + $order = $this->config->get('plugins.archives.order'); + $archives = $this->getArchives((array)$filters, $operator, $order); + } + + // add the archives_start date to the twig variables + $this->grav['twig']->twig_vars['archives_show_count'] = $this->config->get('plugins.archives.show_count'); + $this->grav['twig']->twig_vars['archives_data'] = $archives; + $this->grav['twig']->twig_vars['archives_url'] = $archives_url; + } + + protected function getArchives($filters, $operator, $order) + { + $order_by = $order['by'] ?? 'date'; + $order_dir = $order['dir'] ?? 'desc'; + + /** @var Pages $pages */ + $pages = $this->grav['pages']; + + /** @var PageInterface $page */ + $page = $this->grav['page']; + /** @var Taxonomy $taxonomy_map */ $taxonomy_map = $this->grav['taxonomy']; $taxonomies = []; $find_taxonomy = []; - - $pages = $this->grav['pages']; - - // Get current datetime + $archives = []; $start_date = time(); - $archives = []; - // 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; + $page_filter = null; if (!$filters || (count($filters) === 1 && !reset($filters))){ $collection = $pages->all(); @@ -151,6 +198,8 @@ public function onTwigSiteVariables() // see if the filter uses the new 'items-type' syntax if ($key === '@self' || $key === 'self@') { $new_approach = true; + } elseif ($key === '@page' || $key === 'page@') { + $page_filter = $filter; } elseif ($key === '@taxonomy' || $key === 'taxonomy@') { $taxonomies = $filter === false ? false : array_merge($taxonomies, (array) $filter); } else { @@ -159,6 +208,8 @@ public function onTwigSiteVariables() } if ($new_approach) { $collection = $page->children(); + } elseif ($page_filter) { + $collection = $pages->find($page_filter)->children(); } else { $collection = new Collection(); $collection->append($taxonomy_map->findTaxonomy($find_taxonomy, $operator)->toArray()); @@ -166,22 +217,21 @@ public function onTwigSiteVariables() } // reorder the collection based on settings - $collection = $collection->order($this->config->get('plugins.archives.order.by'), $this->config->get('plugins.archives.order.dir'))->published(); + $collection = $collection->order($order_by, $order_dir)->published(); $date_format = $this->config->get('plugins.archives.date_display_format'); + // drop unpublished and un-routable pages + $collection->published()->routable(); // 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; } // slice the array to the limit you want $archives = array_slice($archives, 0, (int)$this->config->get('plugins.archives.limit'), is_string(reset($archives)) ? false : true ); - // add the archives_start date to the twig variables - $this->grav['twig']->twig_vars['archives_show_count'] = $this->config->get('plugins.archives.show_count'); - $this->grav['twig']->twig_vars['archives_data'] = $archives; + return $archives; } } diff --git a/archives.yaml b/archives.yaml index dca5173..44d64cb 100644 --- a/archives.yaml +++ b/archives.yaml @@ -3,12 +3,22 @@ built_in_css: true date_display_format: 'F Y' show_count: true limit: 12 +taxonomy_names: + month: archives_month + year: archives_year +#Defaults order: by: date dir: desc -filter_combinator: and filters: category: blog -taxonomy_names: - month: archives_month - year: archives_year +filter_combinator: and +#New Page-Specific Configurations +page_specific_config: + - route: '/blog' + order: + by: date + dir: desc + filters: + page@: '/blog' + filter_combinator: and diff --git a/blueprints.yaml b/blueprints.yaml index 5f6383b..591c034 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Archives -version: 1.6.1 +version: 2.0.0 description: The **Archives** plugin creates links for pages grouped by month/year icon: university author: @@ -71,14 +71,16 @@ form: type: number min: 1 - order.dir: - type: toggle - label: Order Direction - highlight: asc - default: desc - options: - asc: Ascending - desc: Descending + taxonomy_names: + type: array + label: Taxonomy Names + placeholder_key: e.g. month + placeholder_value: e.g. archives_month + + defaults_section: + type: section + underline: true + title: Default Configuration order.by: type: select @@ -87,16 +89,29 @@ form: label: Order Type options: default: Default - based on folder name - folder: Folder - based on prefix-less folder name title: Title - based on title field in header + basename: Basename - based on the alphabetic folder name date: Date - based on date field in header + modified: Modified - based on the modified timestamp + folder: Folder - based on prefix-less folder name + random: Random - order is randomized - filters.category: - type: selectize - label: Category filter - help: Comma separated list of category names + order.dir: + type: select + size: medium + label: Order Direction + default: desc + options: + asc: Ascending + desc: Descending + + filters: + type: textarea + yaml: true + label: Filter + placeholder: "page@: '/blog'" validate: - type: commalist + type: yaml filter_combinator: type: select @@ -105,11 +120,63 @@ form: label: Filter Combinator default: and options: - and: And - Boolean && - or: Or - Boolean || + and: 'And - Boolean &&' + or: 'Or - Boolean ||' - taxonomy_names: - type: array - label: Taxonomy Names - placeholder_key: e.g. month - placeholder_value: e.g. archives_month + pageconfig_section: + type: section + underline: true + title: Page-Specific Configuration + + page_specific_config: + type: list + label: Configurations + + fields: + .route: + type: text + label: Route + placeholder: '/blog' + validate: + required: true + .filters: + type: textarea + yaml: true + label: Filter + placeholder: "page@: '/blog'" + validate: + type: yaml + .filter_combinator: + type: select + size: medium + classes: fancy + label: Filter Combinator + default: '' + options: + '': Use Default + and: 'And - Boolean &&' + or: 'Or - Boolean ||' + .order_by: + type: select + size: medium + classes: fancy + label: Order Type + default: '' + options: + '': Use Default + default: Default - based on folder name + title: Title - based on title field in header + basename: Basename - based on the alphabetic folder name + date: Date - based on date field in header + modified: Modified - based on the modified timestamp + folder: Folder - based on prefix-less folder name + random: Random - order is randomized + .order_dir: + type: select + size: medium + label: Order Direction + default: '' + options: + '': Use Default + asc: Ascending + desc: Descending diff --git a/templates/partials/archives.html.twig b/templates/partials/archives.html.twig index 5aa8e37..e935ef0 100644 --- a/templates/partials/archives.html.twig +++ b/templates/partials/archives.html.twig @@ -2,7 +2,7 @@ {% for month,items in archives_data %}