Skip to content

Commit

Permalink
Remove Pico's map Twig filter
Browse files Browse the repository at this point in the history
Fixes #623
  • Loading branch information
PhrozenByte committed Mar 21, 2022
1 parent 6698130 commit 1da5e26
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Released: -
* [Fixed] #602: Fix contents and meta data of meta pages (pages starting with
an `_`) getting replaced by the 404 page when being requested
* [Fixed] Add a proper error message for a missing theme directory
* [Removed] ! Remove Pico's `map` Twig filter; it conflicts with Twig's `map`
filter and can be replaced by Twig's `column` or `map` filter
```

### Version 3.0.0-alpha.2
Expand Down
33 changes: 0 additions & 33 deletions lib/PicoTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public function getFilters(): array
{
return [
'markdown' => new TwigFilter('markdown', [ $this, 'markdownFilter' ], [ 'is_safe' => [ 'html' ] ]),
'map' => new TwigFilter('map', [ $this, 'mapFilter' ]),
'sort_by' => new TwigFilter('sort_by', [ $this, 'sortByFilter' ]),
'link' => new TwigFilter('link', [ $this->pico, 'getPageUrl' ]),
'url' => new TwigFilter('url', [ $this->pico, 'substituteUrl' ]),
Expand Down Expand Up @@ -127,38 +126,6 @@ public function markdownFilter(string $markdown, array $meta = [], bool $singleL
return $this->getPico()->parseFileContent($markdown, $singleLine);
}

/**
* Returns a array with the values of the given key or key path
*
* This method is registered as the Twig `map` filter. You can use this
* filter to e.g. get all page titles (`{{ pages|map("title") }}`).
*
* @param array|Traversable $var variable to map
* @param mixed $mapKeyPath key to map; either a scalar or a
* array interpreted as key path (i.e. ['foo', 'bar'] will return all
* $item['foo']['bar'] values)
*
* @return array mapped values
*
* @throws TwigRuntimeError
*/
public function mapFilter($var, $mapKeyPath): array
{
if (!is_array($var) && (!is_object($var) || !($var instanceof Traversable))) {
throw new TwigRuntimeError(sprintf(
'The map filter only works with arrays or "Traversable", got "%s"',
is_object($var) ? get_class($var) : gettype($var)
));
}

$result = [];
foreach ($var as $key => $value) {
$mapValue = $this->getKeyOfVar($value, $mapKeyPath);
$result[$key] = ($mapValue !== null) ? $mapValue : $value;
}
return $result;
}

/**
* Sorts an array by one of its keys or a arbitrary deep sub-key
*
Expand Down

0 comments on commit 1da5e26

Please sign in to comment.