mirror of
https://github.com/getgrav/grav.git
synced 2026-05-09 04:36:02 +02:00
New Plugin Comp; Fix renaming
This commit is contained in:
@@ -80,7 +80,7 @@ class DevToolsCommand extends ConsoleCommand
|
||||
protected function createComponent()
|
||||
{
|
||||
$name = $this->component['name'];
|
||||
$folderName = $this->inflector->hyphenize($name);
|
||||
$folderName = strtolower($this->inflector->hyphenize($name));
|
||||
$type = $this->component['type'];
|
||||
|
||||
$template = $this->component['template'];
|
||||
@@ -109,6 +109,9 @@ class DevToolsCommand extends ConsoleCommand
|
||||
$file->delete();
|
||||
}
|
||||
}
|
||||
|
||||
rename($componentFolder . DS . $type . '.php', $componentFolder . DS . $this->inflector->hyphenize($name) . '.php');
|
||||
rename($componentFolder . DS . $type . '.yaml', $componentFolder . DS . $this->inflector->hyphenize($name) . '.yaml');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -132,6 +132,13 @@ class NewPluginCommand extends DevToolsCommand
|
||||
|
||||
$this->component['author']['email'] = $helper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
$question = new ChoiceQuestion(
|
||||
'Please choose a base plugin: ',
|
||||
array('blank', 'random')
|
||||
);
|
||||
$this->component['template'] = $helper->ask($this->input, $this->output, $question);
|
||||
|
||||
$this->createComponent();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,3 +6,18 @@ author:
|
||||
name: {{ component.author.name }}
|
||||
email: {{ component.author.email }}
|
||||
license: MIT
|
||||
|
||||
|
||||
form:
|
||||
validation: strict
|
||||
fields:
|
||||
enabled:
|
||||
type: toggle
|
||||
label: Plugin status
|
||||
highlight: 1
|
||||
default: 0
|
||||
options:
|
||||
1: Enabled
|
||||
0: Disabled
|
||||
validate:
|
||||
type: bool
|
||||
@@ -4,10 +4,10 @@ namespace Grav\Plugin;
|
||||
use Grav\Common\Plugin;
|
||||
|
||||
/**
|
||||
* Class {{ component.name|variableize }}Plugin
|
||||
* Class {{ component.name|camelize }}Plugin
|
||||
* @package Grav\Plugin
|
||||
*/
|
||||
class {{ component.name|variableize }}Plugin extends Plugin
|
||||
class {{ component.name|camelize }}Plugin extends Plugin
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# v0.1.0
|
||||
## {{ "now"|date("m/d/Y") }}
|
||||
|
||||
1. [](#new)
|
||||
* ChangeLog started...
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) {{ "now"|date("Y") }} {{ component.author.name }}
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,100 @@
|
||||
# Grav {{ component.name|camelize }} Plugin
|
||||
|
||||
`{{ component.name|camelize }}` is a [Grav][grav] Plugin which allows a random article to load based on its taxonomy filters.
|
||||
|
||||
# Installation
|
||||
|
||||
Installing the Random plugin can be done in one of two ways. Our GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.
|
||||
|
||||
## GPM Installation (Preferred)
|
||||
|
||||
The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's Terminal (also called the command line). From the root of your Grav install type:
|
||||
|
||||
bin/gpm install random
|
||||
|
||||
This will install the Random plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/random`.
|
||||
|
||||
## Manual Installation
|
||||
|
||||
To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `random`. You can find these files either on [GitHub](https://github.com/getgrav/grav-plugin-random) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras).
|
||||
|
||||
You should now have all the plugin files under
|
||||
|
||||
/your/site/grav/user/plugins/random
|
||||
|
||||
>> NOTE: This plugin is a modular component for Grav which requires [Grav](http://github.com/getgrav/grav), the [Error](https://github.com/getgrav/grav-plugin-error) and [Problems](https://github.com/getgrav/grav-plugin-problems) plugins, and a theme to be installed in order to operate.
|
||||
|
||||
# Usage
|
||||
|
||||
`Random` creates a **route** that you define. Based on the **taxonomy** filters, it picks a random item.
|
||||
|
||||
By default `Random` looks for all page that are have taxonomy that match those in the plugin's `filter` settings. For this content to find _anything_ you need to define at
|
||||
least one page with a matching taxonomy. As the default value of the filter is `category: blog` you must have at least one page with this taxonomy. For example:
|
||||
|
||||
---
|
||||
title: Home
|
||||
taxonomy:
|
||||
category: blog
|
||||
---
|
||||
|
||||
# Grav is Running!
|
||||
## You have installed **Grav** successfully
|
||||
|
||||
Congratulations! You have installed the **Base Grav Package** that provides a **simple page** and the default **antimatter** theme to get you started.
|
||||
|
||||
|
||||
# Settings Defaults
|
||||
|
||||
route: /random
|
||||
filters:
|
||||
category: blog
|
||||
|
||||
# Customizing the Settings
|
||||
|
||||
To customize the plugin, you first need to create an override config. To do so, create the folder `user/config/plugins` (if it doesn't exist already) and copy the [random.yaml](random.yaml) config file in there.
|
||||
|
||||
Now you can edit it and tweak it however you prefer.
|
||||
|
||||
For further help with the `filters` settings, please refer to our [Taxonomy][taxonomy] and [Headers][headers] documentation.
|
||||
|
||||
# Creating a "Random Article" Button
|
||||
|
||||

|
||||
|
||||
In our [Blog Skeleton](http://demo.getgrav.org/blog-skeleton/) we placed a button in the sidebar that pulls up a random blog post. Here is the code we used in the `sidebar.html.twig` template file to create this button.
|
||||
|
||||
<a class="button" href="{{ base_url_relative }}/random"><i class="fa fa-retweet"></i> I'm Feeling Lucky!</a>
|
||||
|
||||
This button forwards the user to `/random` which is configured as the route for the random plugin. The `random.yaml` configuration file contains the following:
|
||||
|
||||
enabled: true
|
||||
route: /random
|
||||
filters: { category: blog}
|
||||
filter_combinator: and
|
||||
|
||||
# Updating
|
||||
|
||||
As development for the Random plugin continues, new versions may become available that add additional features and functionality, improve compatibility with newer Grav releases, and generally provide a better user experience. Updating Random is easy, and can be done through Grav's GPM system, as well as manually.
|
||||
|
||||
## GPM Update (Preferred)
|
||||
|
||||
The simplest way to update this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm). You can do this with this by navigating to the root directory of your Grav install using your system's Terminal (also called command line) and typing the following:
|
||||
|
||||
bin/gpm update random
|
||||
|
||||
This command will check your Grav install to see if your Random plugin is due for an update. If a newer release is found, you will be asked whether or not you wish to update. To continue, type `y` and hit enter. The plugin will automatically update and clear Grav's cache.
|
||||
|
||||
## Manual Update
|
||||
|
||||
Manually updating Random is pretty simple. Here is what you will need to do to get this done:
|
||||
|
||||
* Delete the `your/site/user/plugins/random` directory.
|
||||
* Download the new version of the Random plugin from either [GitHub](https://github.com/getgrav/grav-plugin-random) or [GetGrav.org](http://getgrav.org/downloads/plugins#extras).
|
||||
* Unzip the zip file in `your/site/user/plugins` and rename the resulting folder to `random`.
|
||||
* Clear the Grav cache. The simplest way to do this is by going to the root Grav directory in terminal and typing `bin/grav clear-cache`.
|
||||
|
||||
> Note: Any changes you have made to any of the files listed under this directory will also be removed and replaced by the new set. Any files located elsewhere (for example a YAML settings file placed in `user/config/plugins`) will remain intact.
|
||||
|
||||
[taxonomy]: http://learn.getgrav.org/content/taxonomy
|
||||
[headers]: http://learn.getgrav.org/content/headers
|
||||
[grav]: http://github.com/getgrav/grav
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 60 KiB |
@@ -0,0 +1,57 @@
|
||||
name: {{ component.name|titleize }}
|
||||
version: 0.1.0
|
||||
description: {{ component.description }}
|
||||
icon: refresh
|
||||
author:
|
||||
name: {{ component.author.name }}
|
||||
email: {{ component.author.email }}
|
||||
license: MIT
|
||||
|
||||
form:
|
||||
validation: strict
|
||||
fields:
|
||||
enabled:
|
||||
type: toggle
|
||||
label: Plugin status
|
||||
highlight: 1
|
||||
default: 0
|
||||
options:
|
||||
1: Enabled
|
||||
0: Disabled
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
route:
|
||||
type: text
|
||||
size: medium
|
||||
label: Route
|
||||
default: /random
|
||||
help: Default route of the random plugin
|
||||
|
||||
redirect:
|
||||
type: toggle
|
||||
label: Redirect
|
||||
highlight: 1
|
||||
default: 1
|
||||
options:
|
||||
1: Enabled
|
||||
0: Disabled
|
||||
validate:
|
||||
type: bool
|
||||
|
||||
filters.category:
|
||||
type: selectize
|
||||
label: Category filter
|
||||
help: Comma separated list of category names
|
||||
validate:
|
||||
type: commalist
|
||||
|
||||
filter_combinator:
|
||||
type: select
|
||||
size: medium
|
||||
classes: fancy
|
||||
label: Filter Combinator
|
||||
default: and
|
||||
options:
|
||||
and: And - Boolean &&
|
||||
or: Or - Boolean ||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Grav\Common\Page\Collection;
|
||||
use Grav\Common\Plugin;
|
||||
use Grav\Common\Uri;
|
||||
use Grav\Common\Taxonomy;
|
||||
|
||||
class {{ component.name|camelize }}Plugin extends Plugin
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate plugin if path matches to the configured one.
|
||||
*/
|
||||
public function onPluginsInitialized()
|
||||
{
|
||||
if ($this->isAdmin()) {
|
||||
$this->active = false;
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Uri $uri */
|
||||
$uri = $this->grav['uri'];
|
||||
$route = $this->config->get('plugins.{{ component.name|hyphenize }}.route');
|
||||
|
||||
if ($route && $route == $uri->path()) {
|
||||
$this->enable([
|
||||
'onPageInitialized' => ['onPageInitialized', 0]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display random page.
|
||||
*/
|
||||
public function onPageInitialized()
|
||||
{
|
||||
/** @var Taxonomy $taxonomy_map */
|
||||
$taxonomy_map = $this->grav['taxonomy'];
|
||||
|
||||
$filters = (array) $this->config->get('plugins.{{ component.name|hyphenize }}.filters');
|
||||
$operator = $this->config->get('plugins.{{ component.name|hyphenize }}.filter_combinator', 'and');
|
||||
|
||||
if (count($filters)) {
|
||||
$collection = new Collection();
|
||||
$collection->append($taxonomy_map->findTaxonomy($filters, $operator)->toArray());
|
||||
if (count($collection)) {
|
||||
unset($this->grav['page']);
|
||||
$page = $collection->random()->current();
|
||||
|
||||
if ($this->config->get('plugins.{{ component.name|hyphenize }}.redirect', true)) {
|
||||
$this->grav->redirect($page->url(true));
|
||||
} else {
|
||||
// override the modified time
|
||||
$page->modified(time());
|
||||
$this->grav['page'] = $page;
|
||||
|
||||
// Convince the URI object that it is this random page...
|
||||
$uri = $this->grav['uri'];
|
||||
$uri->url = $uri->base().$page->url();
|
||||
$uri->init();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
enabled: true
|
||||
route: /random
|
||||
redirect: true
|
||||
filters: { category: blog}
|
||||
filter_combinator: and
|
||||
@@ -3,7 +3,7 @@ namespace Grav\Theme;
|
||||
|
||||
use Grav\Common\Theme;
|
||||
|
||||
class {{ component.name|variableize }}Theme extends Theme
|
||||
class {{ component.name|camelize }}Theme extends Theme
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace Grav\Theme;
|
||||
|
||||
class {{ component.name|variableize }} extends {{ component.extends|variableize }}
|
||||
class {{ component.name|camelize }} extends {{ component.extends|camelize }}
|
||||
{
|
||||
// Some new methods, properties etc.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user