Skip to content

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Apr 6, 2020
2 parents 6d85913 + 523def8 commit 0bd5506
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 49 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 55 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
[email protected]: 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
Expand Down
78 changes: 64 additions & 14 deletions archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand 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 {
Expand All @@ -159,29 +208,30 @@ 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());
}
}

// 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;
}
}
18 changes: 14 additions & 4 deletions archives.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 0bd5506

Please sign in to comment.