Skip to content

Commit

Permalink
Merge pull request #10 from nliautaud/pico-2.0
Browse files Browse the repository at this point in the history
Pico 2.0 support
  • Loading branch information
nliautaud authored Jul 4, 2018
2 parents bd7451c + 6651739 commit a1f4c7e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
46 changes: 31 additions & 15 deletions PicoPagesList.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* Flat and nested pages list navigation for Pico CMS.
*
*
* - Adds twig global `{{ nested_pages }}` in addition to `{{ pages }}`
* - Render flat or nested HTML navigation tree with `navigation` twig filter
* - Filter pages and nested pages by paths with `exclude()` and `only()` twig filters
*
*
* Examples :
*
*
* {{ pages | navigation }} // output a flat pages list
* {{ nested_pages | navigation }} // output a nested pages list
* {{ nested_pages | exclude('sub/page') | navigation }} // filtered nested pages list
Expand All @@ -20,36 +20,51 @@
*/
class PicoPagesList extends AbstractPicoPlugin
{
const API_VERSION = 2;
public $items;
private $currentPagePath;

/**
* Store the current url and construct the nested pages array.
* Construct the nested pages array.
*
* Triggered after Pico has read all known pages
*
* See {@link DummyPlugin::onSinglePageLoaded()} for details about the
* structure of the page data.
*
* @see Pico::getPages()
* @param array[] &$pages data of all known pages
* @return void
*/
public function onPagesLoaded(array &$pages)
{
$this->items = $this->nestedPages($pages);
}


/**
* Store the current url.
*
* Triggered after Pico has found the current page and possible siblings
*
* See {@link DummyPlugin::onSinglePageLoaded()} for details about the
* structure of the page data.
*
* @see Pico::getCurrentPage()
* @see Pico::getPreviousPage()
* @see Pico::getNextPage()
* @param array[] &$pages data of all known pages
* @param array|null &$currentPage data of the page being served
* @param array|null &$previousPage data of the previous page
* @param array|null &$nextPage data of the next page
* @return void
*/
public function onPagesLoaded(
array &$pages,
protected function onCurrentPageDiscovered(
array &$currentPage = null,
array &$previousPage = null,
array &$nextPage = null
) {
$base_url = $this->getConfig('base_url');
$this->currentPagePath = str_replace(array('?', $base_url), '', urldecode($currentPage['url']));
$this->items = $this->nestedPages($pages);
}

/**
Expand All @@ -59,15 +74,15 @@ public function onPagesLoaded(
*
* @see Pico::getTwig()
* @see DummyPlugin::onPageRendered()
* @param Twig_Environment &$twig twig template engine
* @param array &$twigVariables template variables
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
* @return void
*/
public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
public function onPageRendering(string &$templateName, array &$twigVariables)
{
$twig = $this->getPico()->getTwig();
$twigVariables['nested_pages'] = $this->items;

$twig->addFilter(new Twig_SimpleFilter('navigation', function($pages) {
return $this->output($pages);
}));
Expand Down Expand Up @@ -124,7 +139,7 @@ private function nested_path($page)
$parent = $value;
break;
}

$parent['_childs'][$part] = $value;
$parent = &$parent['_childs'][$part];
}
Expand All @@ -143,7 +158,7 @@ private static function rtrim($str, $substr)
$length = strlen($substr);
return (substr($str, -$length) === $substr) ? substr($str, 0, -$length) : $str;
}

/**
* Filter the pages array according to given paths, as exclusive or inclusive.
*
Expand Down Expand Up @@ -206,6 +221,8 @@ private function output($pages)
$html = '<ul>';
foreach ($pages as $pageID => $page)
{
if (!empty($page['hidden'])) continue;

$childsOutput = '';
if(isset($page['_childs'])) {
$childsOutput = $this->output($page['_childs']);
Expand Down Expand Up @@ -233,4 +250,3 @@ private function output($pages)
return $html;
}
}
?>
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ You may want a recursive Twig template or macro to walk trough it, for example :

The lists are sorted according to the default settings in Pico `config.php`.

```php
$config['pages_order_by'] = 'date';
$config['pages_order'] = 'desc';
```yml
pages_order_by: date
pages_order: desc
```

0 comments on commit a1f4c7e

Please sign in to comment.