diff --git a/README.md b/README.md index af95293..30e1391 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/admin/assets/admin.min.js b/admin/assets/admin.min.js new file mode 100644 index 0000000..8089c52 --- /dev/null +++ b/admin/assets/admin.min.js @@ -0,0 +1 @@ +$(' ').prependTo('#authorSave'); \ No newline at end of file diff --git a/admin/assets/style.min.css b/admin/assets/style.min.css new file mode 100644 index 0000000..bab54e8 --- /dev/null +++ b/admin/assets/style.min.css @@ -0,0 +1 @@ +#authorSave{margin-left: 1.5rem;}#authorSave:active{margin:1px 0 -1px 1.5rem;} \ No newline at end of file diff --git a/admin/pages/authors.md b/admin/pages/authors.md new file mode 100644 index 0000000..32f08e2 --- /dev/null +++ b/admin/pages/authors.md @@ -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 +--- diff --git a/admin/templates/authors.html.twig b/admin/templates/authors.html.twig new file mode 100644 index 0000000..7f2b48e --- /dev/null +++ b/admin/templates/authors.html.twig @@ -0,0 +1,11 @@ +{% extends 'partials/base.html.twig' %} + +{% block titlebar %} +

Authors

+{% endblock %} + +{% block content %} + {% include "forms/form.html.twig" %} + {% do assets.addCss('user://plugins/aura-authors/admin/assets/style.min.css') %} + {% do assets.addInlineJs('$(" ").prependTo(\"#authorSave\");') %} +{% endblock %} \ No newline at end of file diff --git a/assets/demo-min.png b/assets/demo-min.png deleted file mode 100644 index 67564e5..0000000 Binary files a/assets/demo-min.png and /dev/null differ diff --git a/assets/demo.jpg b/assets/demo.jpg new file mode 100644 index 0000000..da08566 Binary files /dev/null and b/assets/demo.jpg differ diff --git a/aura-authors.php b/aura-authors.php index c122956..caa8378 100644 --- a/aura-authors.php +++ b/aura-authors.php @@ -1,9 +1,11 @@ ['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; } -} +} \ No newline at end of file diff --git a/blueprints.yaml b/blueprints.yaml index 215f7ab..8f3ee9f 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -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' diff --git a/blueprints/aura-authors.yaml b/blueprints/aura-authors.yaml deleted file mode 100644 index 1fb1e02..0000000 --- a/blueprints/aura-authors.yaml +++ /dev/null @@ -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: - '': '' diff --git a/blueprints/default.yaml b/blueprints/default.yaml new file mode 100644 index 0000000..0f95295 --- /dev/null +++ b/blueprints/default.yaml @@ -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: + '': ''