mirror of
https://github.com/getgrav/grav.git
synced 2026-05-09 16:36:50 +02:00
Add support for listing custom page templates/types
This commit is contained in:
@@ -34,7 +34,7 @@ form:
|
||||
type:
|
||||
type: select
|
||||
classes: fancy
|
||||
label: Page Type
|
||||
label: Display Template
|
||||
default: default
|
||||
@data-options: '\Grav\Common\Page\Pages::types'
|
||||
validate:
|
||||
|
||||
@@ -89,12 +89,17 @@ abstract class Folder
|
||||
$compare = isset($params['compare']) ? 'get' . $params['compare'] : null;
|
||||
$pattern = isset($params['pattern']) ? $params['pattern'] : null;
|
||||
$filters = isset($params['filters']) ? $params['filters'] : null;
|
||||
$recursive = isset($params['recursive']) ? $params['recursive'] : true;
|
||||
$key = isset($params['key']) ? 'get' . $params['key'] : null;
|
||||
$value = isset($params['value']) ? 'get' . $params['value'] : 'getSubPathname';
|
||||
$value = isset($params['value']) ? 'get' . $params['value'] : ($recursive ? 'getSubPathname' : 'getFilename');
|
||||
|
||||
$directory = new \RecursiveDirectoryIterator($path,
|
||||
\RecursiveDirectoryIterator::SKIP_DOTS + \FilesystemIterator::UNIX_PATHS + \FilesystemIterator::CURRENT_AS_SELF);
|
||||
$iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
if ($recursive) {
|
||||
$directory = new \RecursiveDirectoryIterator($path,
|
||||
\RecursiveDirectoryIterator::SKIP_DOTS + \FilesystemIterator::UNIX_PATHS + \FilesystemIterator::CURRENT_AS_SELF);
|
||||
$iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
} else {
|
||||
$iterator = new \FilesystemIterator($path);
|
||||
}
|
||||
|
||||
$results = array();
|
||||
|
||||
|
||||
@@ -316,9 +316,19 @@ class Pages
|
||||
*/
|
||||
static public function types()
|
||||
{
|
||||
$blueprints = new Blueprints('theme://blueprints/');
|
||||
static $types;
|
||||
|
||||
return $blueprints->types();
|
||||
if (!$types) {
|
||||
$types = new Types();
|
||||
$types->scanBlueprints('theme://blueprints/');
|
||||
$types->scanTemplates('theme://templates/');
|
||||
|
||||
$event = new Event();
|
||||
$event->types = $types;
|
||||
Grav::instance()->fireEvent('onGetPageTemplates', $event);
|
||||
}
|
||||
|
||||
return $types->toSelect();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
65
system/src/Grav/Common/Page/Types.php
Normal file
65
system/src/Grav/Common/Page/Types.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace Grav\Common\Page;
|
||||
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Constructor;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Countable;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Export;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Iterator;
|
||||
|
||||
class Types
|
||||
{
|
||||
use Constructor, Iterator, Countable, Export;
|
||||
|
||||
protected $items = [];
|
||||
|
||||
public function register($type, $blueprint = null)
|
||||
{
|
||||
if ($blueprint || empty($this->items[$type])) {
|
||||
$this->items[$type] = $blueprint;
|
||||
}
|
||||
}
|
||||
|
||||
public function scanBlueprints($path)
|
||||
{
|
||||
$options = [
|
||||
'compare' => 'Filename',
|
||||
'pattern' => '|\.yaml$|',
|
||||
'filters' => [
|
||||
'key' => '|\.yaml$|'
|
||||
],
|
||||
'key' => 'Filename',
|
||||
'value' => 'PathName',
|
||||
'recursive' => false
|
||||
];
|
||||
|
||||
$this->items = Folder::all($path, $options) + $this->items;
|
||||
}
|
||||
|
||||
public function scanTemplates($path)
|
||||
{
|
||||
$options = [
|
||||
'compare' => 'Filename',
|
||||
'pattern' => '|\.html\.twig$|',
|
||||
'filters' => [
|
||||
'value' => '|\.html\.twig$|'
|
||||
],
|
||||
'value' => 'Filename',
|
||||
'recursive' => false
|
||||
];
|
||||
|
||||
foreach (Folder::all($path, $options) as $type) {
|
||||
$this->register($type);
|
||||
}
|
||||
}
|
||||
|
||||
public function toSelect()
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->items as $name => $file) {
|
||||
$list[$name] = ucfirst(strtr($name, '_', ' '));
|
||||
}
|
||||
ksort($list);
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user