Skip to content

Commit

Permalink
Merge branch 'release/1.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviocopes committed Jul 14, 2016
2 parents e217f02 + 027106c commit d5b0a90
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
85 changes: 67 additions & 18 deletions archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

class ArchivesPlugin extends Plugin
{
/**
* @var string
*/
protected $month_taxonomy_name;

/**
* @var string
*/
protected $year_taxonomy_name;

/**
* @return array
*/
Expand All @@ -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([
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions archives.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ order:
filter_combinator: and
filters:
category: blog
taxonomy_names:
month: archives_month
year: archives_year

2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion templates/partials/archives.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% for month,items in archives_data %}
<li>
<a href="{{ base_url }}/archives_month{{ config.system.param_sep }}{{ month|date('M_Y')|lower|e('url') }}">
<a href="{{ base_url }}/{{ config.plugins.archives.taxonomy_names.month }}{{ config.system.param_sep }}{{ month|date('M_Y')|lower|e('url') }}">
{% if archives_show_count %}
<span class="label">{{ items|length }}</span>
{% endif %}
Expand Down

0 comments on commit d5b0a90

Please sign in to comment.