Dedicated Authors admin panel, optimised use of event hooks, updated documentation

This commit is contained in:
matt-j-m 2020-05-15 13:00:41 +10:00
parent ea84035fee
commit ba03692b2f
11 changed files with 220 additions and 167 deletions

View File

@ -2,7 +2,7 @@
The **Aura Authors** Plugin for [Grav CMS](https://github.com/getgrav/grav) enables you to store author bios in a centrally managed repository and have them displayed across various pages of your site.
![Aura Authors Plugin for Grav - Demo](assets/demo-min.png)
![Aura Authors Plugin for Grav - Demo](assets/demo.jpg)
## Features
@ -10,6 +10,7 @@ The **Aura Authors** Plugin for [Grav CMS](https://github.com/getgrav/grav) enab
* Central repository ensures that a single change to an author's bio will automatically update it across multiple pages
* Optionally include author's image and links to social media accounts such as Twitter, LinkedIn etc.
* Use the included mobile and desktop responsive styling or provide your own
* Supports `author` taxonomy type for page collections based on author's individual taxonomy label
## Installation
@ -17,7 +18,7 @@ It is recommended to install Aura Authors directly through the Admin Plugin by b
## Configuration
* Enter author details via the Admin Plugin by browsing to `Plugins` > `Aura Authors` and selecting `Add Item`.
* Enter author details via the `Authors` section of the Admin Plugin.
* Copy the following code snippet to the relevant Twig template within your theme, at the place where you would like the author bio to be displayed.
@ -25,13 +26,13 @@ It is recommended to install Aura Authors directly through the Admin Plugin by b
{% include 'partials/author-bio.html.twig' ignore missing %}
```
* The above will output the relevant author bio on all pages of that type, once an author is defined at the page level (see Usage below). Alternatively if you do not have access or do not wish to edit the theme you can include the code snippet directly within a page via the page editor. For this option to work you will need ensure Twig processing is enabled either at the page level (`Page Editor` > `Advanced` > `Overrides` > `Process`) or the site level (`Configuration` > `System` > `Content` > `Process`).
* Once an author is defined at the page level (see Usage below) the relevant author bio will now be displayed on that page (provided it uses the template updated above). Alternatively if you do not have access or do not wish to edit the theme you can include the code snippet directly within a page via the page editor. For this option to work you will need ensure Twig processing is enabled either at the page level (`Page Editor` > `Advanced` > `Overrides` > `Process`) or the site level (`Configuration` > `System` > `Content` > `Process`).
* Optionally customise the layout of the author bio by copying the included templates/partials/author-bio.html.twig into the same location under your theme and editing it. Default styling can be disabled via the Plugin configuration panel if you wish to provide your own.
* Optionally customise the layout of the author bio by copying the included file `templates/partials/author-bio.html.twig` into the same location under your theme and editing it. Default styling can be disabled via the Plugin configuration panel if you wish to provide your own.
## Usage
Authors can be selected per page via the page editor, on the `Aura` tab. The list of authors will be automatically populated with author records you create via the Plugins panel.
Authors can be selected per page via the page editor, on the `Aura` tab. The list of authors will be automatically populated with author records you create via the `Authors` menu item.
## Credits

1
admin/assets/admin.min.js vendored Normal file
View File

@ -0,0 +1 @@
$('<i class="fa fa-check"></i> ').prependTo('#authorSave');

1
admin/assets/style.min.css vendored Normal file
View File

@ -0,0 +1 @@
#authorSave{margin-left: 1.5rem;}#authorSave:active{margin:1px 0 -1px 1.5rem;}

90
admin/pages/authors.md Normal file
View File

@ -0,0 +1,90 @@
---
title: Authors
access:
admin.authors: true
admin.super: true
form:
name: authors
action: '/authors'
template: authors
refresh_prevention: true
fields:
authors:
type: list
display_label: false
collapsed: true
style: vertical
help: "Add or edit author details"
data-default@: ['\Grav\Plugin\AuraAuthorsPlugin::getAuthors']
fields:
.name:
type: text
size: large
label: Name
validate:
required: true
.label:
type: text
size: large
label: Taxonomy Label
validate:
pattern: "[a-z][a-z0-9_\-]+"
message: "Use all lowercase letters and replace spaces with hyphens."
required: true
.image:
type: file
size: large
label: Image
multiple: false
destination: 'user/images'
accept:
- image/*
.description:
type: textarea
size: long
label: Description
.person-facebook-url:
type: text
size: large
label: Facebook URL
placeholder: 'https://www.facebook.com/username'
.person-twitter-user:
type: text
size: large
label: Twitter Username
placeholder: 'username'
.person-instagram-url:
type: text
size: large
label: Instagram URL
placeholder: 'https://www.instagram.com/username'
.person-linkedin-url:
type: text
size: large
label: LinkedIn URL
placeholder: 'https://www.linkedin.com/in/name'
.person-pinterest-url:
type: text
size: large
label: Pinterest URL
placeholder: 'https://www.pinterest.com/user/username'
.person-youtube-url:
type: text
size: large
label: YouTube URL
placeholder: 'https://www.youtube.com/username'
.person-website-url:
type: text
label: Website URL
placeholder: 'https://www.example.com'
buttons:
submit:
type: submit
value: ' Save'
classes: button
id: authorSave
---

View File

@ -0,0 +1,11 @@
{% extends 'partials/base.html.twig' %}
{% block titlebar %}
<h1><i class="fa fa-fw fa-users"></i> Authors</h1>
{% endblock %}
{% block content %}
{% include "forms/form.html.twig" %}
{% do assets.addCss('user://plugins/aura-authors/admin/assets/style.min.css') %}
{% do assets.addInlineJs('$("<i class=\"fa fa-check\"></i> ").prependTo(\"#authorSave\");') %}
{% endblock %}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

BIN
assets/demo.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

View File

@ -1,9 +1,11 @@
<?php
namespace Grav\Plugin;
use Grav\Common\Data;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\File;
use Symfony\Component\Yaml\Yaml;
/**
* Class AuraAuthorsPlugin
@ -11,112 +13,101 @@ use RocketTheme\Toolbox\Event\Event;
*/
class AuraAuthorsPlugin extends Plugin
{
protected static $authorsFile = DATA_DIR . 'authors/authors.yaml';
protected $route = 'authors';
static protected $authorList = [];
/** @var array */
static protected $authorList = array();
/**
* @return array
*/
public static function getSubscribedEvents()
public static function getAuthors()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onBlueprintCreated' => ['onBlueprintCreated', 0],
];
$fileInstance = File::instance(self::$authorsFile);
if (!$fileInstance->content()) {
return;
}
return Yaml::parse($fileInstance->content());
}
public static function saveAuthors(array $authors)
{
$file = File::instance(self::$authorsFile);
$file->save(Yaml::dump($authors));
echo json_encode('Saved');
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
// Add author to site taxonomies
$taxonomies = $this->config->get('site.taxonomies');
$taxonomies[] = 'author';
$this->config->set('site.taxonomies', $taxonomies);
// Populate author list for use in blueprint
$authors = $this->grav['config']->get('plugins.aura-authors.authors');
if ($authors) {
foreach ($authors as $author) {
if (!array_key_exists($author['label'], self::$authorList)) {
self::$authorList[$author['label']] = $author['name'];
}
}
// Admin only events
if ($this->isAdmin()) {
$this->enable([
'onAdminMenu' => ['onAdminMenu', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onGetPageBlueprints' => ['onGetPageBlueprints', 0],
'onAdminSave' => ['onAdminSave', 0],
]);
return;
}
asort(self::$authorList);
}
}
/**
* Extend page blueprints with additional configuration options.
*
* @param Event $event
*/
public function onBlueprintCreated(Event $event)
public function onGetPageBlueprints($event)
{
static $inEvent = false;
/** @var Data\Blueprint $blueprint */
$blueprint = $event['blueprint'];
if (!$inEvent && $blueprint->get('form/fields/tabs', null, '/')) {
$inEvent = true;
$blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get('aura-authors');
$blueprint->extend($extends, true);
$inEvent = false;
}
$types = $event->types;
$types->scanBlueprints('plugins://' . $this->name . '/blueprints');
}
/**
* Add plugin directory to twig lookup paths
*/
public function onTwigTemplatePaths()
public function onAdminSave(Event $event)
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Add author to page taxonomy
*
* @param Event $e
*/
public function onPageInitialized()
{
if ($this->isAdmin()) {
// Don't proceed if Admin is not saving a Page
if (!$event['object'] instanceof Page) {
return;
}
$page = $event['object'];
if (isset($page->header()->aura['author'])) {
$author = array('author' => array($page->header()->aura['author']));
$page->header()->taxonomy = array_merge($page->header()->taxonomy, $author);
}
}
public function onPageInitialized()
{
$page = $this->grav['page'];
$header = $page->header();
if ((isset($header->aura['author'])) && ($header->aura['author'] != '')) {
$taxonomy = $page->taxonomy();
$taxonomy['author'][] = $header->aura['author'];
$page->taxonomy($taxonomy);
if ($page->template() === 'authors') {
$post = $this->grav['uri']->post();
if ($post) {
$authors = isset($post['data']['authors']) ? $post['data']['authors'] : [];
$file = File::instance(self::$authorsFile);
$file->save(Yaml::dump($authors));
$admin = $this->grav['admin'];
$admin->setMessage($admin::translate('PLUGIN_ADMIN.SUCCESSFULLY_SAVED'), 'info');
}
}
}
/**
* Create structured authors array and expose to Twig
*/
public function onTwigSiteVariables()
public function onTwigTemplatePaths()
{
$authors = array();
$raw = $this->grav['config']->get('plugins.aura-authors.authors');
if ($raw) {
foreach ($raw as $author) {
$authors[$author['label']] = $author;
}
}
$this->grav['twig']->twig_vars['authors'] = $authors;
$this->grav['twig']->twig_paths[] = __DIR__ . '/admin/templates';
}
public function onAdminMenu()
{
$this->grav['twig']->plugins_hooked_nav['Authors'] = ['route' => $this->route, 'icon' => 'fa-users'];
}
public static function listAuthors() {
return self::$authorList;
$authorList = [];
$authors = self::getAuthors();
foreach ($authors as $author) {
$authorList[$author['label']] = $author['name'];
}
asort($authorList);
return $authorList;
}
}
}

View File

@ -34,68 +34,3 @@ form:
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
authors:
type: list
label: Author details
help: "Add or edit author details"
fields:
.name:
type: text
size: large
label: Name
validate:
required: true
.label:
type: text
size: large
label: Taxonomy Label
validate:
pattern: "[a-z][a-z0-9_\-]+"
message: "Use all lowercase letters and replace spaces with hyphens."
required: true
.image:
type: file
size: large
label: Image
multiple: false
destination: 'user/plugins/aura-authors/assets'
accept:
- image/*
.description:
type: textarea
size: long
label: Description
.facebook-url:
type: text
size: large
label: Facebook URL
placeholder: 'https://www.facebook.com/username'
.twitter-url:
type: text
size: large
label: Twitter URL
placeholder: 'https://twitter.com/username'
.instagram-url:
type: text
size: large
label: Instagram URL
placeholder: 'https://www.instagram.com/username'
.linkedin-url:
type: text
size: large
label: LinkedIn URL
placeholder: 'https://www.linkedin.com/in/name'
.pinterest-url:
type: text
size: large
label: Pinterest URL
placeholder: 'https://www.pinterest.com/user/username'
.youtube-url:
type: text
size: large
label: YouTube URL
placeholder: 'https://www.youtube.com/username'
.website-url:
type: text
label: Website URL
placeholder: 'https://www.example.com'

View File

@ -1,17 +0,0 @@
form:
fields:
tabs:
fields:
aura:
type: tab
title: Aura
fields:
header.aura.author:
type: select
label: Author
size: medium
data-options@: '\Grav\Plugin\AuraAuthorsPlugin::listAuthors'
options:
'': ''

40
blueprints/default.yaml Normal file
View File

@ -0,0 +1,40 @@
title: Aura
'@extends':
type: default
context: blueprints://pages
form:
fields:
tabs:
type: tabs
active: 1
fields:
options:
type: tab
title: PLUGIN_ADMIN.OPTIONS
fields:
publishing:
type: section
title: PLUGIN_ADMIN.PUBLISHING
underline: true
fields:
header.metadata:
unset@: true
aura:
type: tab
title: Aura
fields:
header.aura.author:
type: select
label: Author
size: medium
data-options@: '\Grav\Plugin\AuraAuthorsPlugin::listAuthors'
options:
'': ''