From 64da3f693ef724d70e57e3170857bb7981a93921 Mon Sep 17 00:00:00 2001 From: Aaron Bushnell Date: Tue, 30 Mar 2021 09:26:20 -0400 Subject: [PATCH] Update plugin for Composer 2 --- .craftplugin | 1 - CHANGELOG.md | 4 ++ README.md | 12 ++-- config/app.php | 6 +- src/{GuideModule.php => Guide.php} | 70 +++++++------------ .../guidemodule/GuideModuleAsset.php | 59 ---------------- .../guidemodule/css/GuideModule.css | 15 ---- src/config/guidemodule.php | 26 ------- src/controllers/DefaultController.php | 51 ++++---------- src/templates/index.twig | 14 ++-- 10 files changed, 62 insertions(+), 196 deletions(-) delete mode 100644 .craftplugin rename src/{GuideModule.php => Guide.php} (58%) delete mode 100644 src/assetbundles/guidemodule/GuideModuleAsset.php delete mode 100644 src/assetbundles/guidemodule/css/GuideModule.css delete mode 100644 src/config/guidemodule.php diff --git a/.craftplugin b/.craftplugin deleted file mode 100644 index 0cfe683..0000000 --- a/.craftplugin +++ /dev/null @@ -1 +0,0 @@ -{"pluginName":"Guide","pluginDescription":"A Craft CMS module to display a help guide in the control panel","pluginVersion":"1.0.0","pluginAuthorName":"TrendyMinds","pluginAuthorUrl":"https://trendyminds.com","codeComments":"yes","pluginComponents":["controllers","cpsection","services","settings","variables"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"module_api_version_3_0"} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c40332..4b4b6fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 2.0.0 - 2021-03-30 +### Added +- Support for Composer 2.x + ## 1.0.0 - 2020-04-20 ### Added - Initial release diff --git a/README.md b/README.md index f8d1960..dc6ff21 100644 --- a/README.md +++ b/README.md @@ -7,23 +7,23 @@ A Craft CMS module to display a help guide in the control panel Guide is built to run on Craft CMS 3.2 and later ## Installation -1. [Download a zip archive](https://github.com/trendyminds/guide/archive/master.zip), rename the folder to `guidemodule` and add it to your `modules/` folder. +1. [Download a zip archive](https://github.com/trendyminds/guide/archive/master.zip), rename the folder to `guide` and add it to your `modules/` folder. 2. Add the contents of the `app.php` file to your `config/app.php` (or just copy it there if it does not exist) ```php return [ 'modules' => [ - 'guide-module' => [ - 'class' => \modules\guidemodule\GuideModule::class + 'guide' => [ + 'class' => \modules\guide\Guide::class ], ], - 'bootstrap' => ['guide-module'], + 'bootstrap' => ['guide'], ]; ``` 3. Add the following to your project's `composer.json` file so that Composer can find your module: ```json "autoload": { "psr-4": { - "modules\\guidemodule\\": "modules/guidemodule/src/" + "modules\\guide\\": "modules/guide/src/" } }, ``` @@ -35,6 +35,6 @@ composer dump-autoload ## Configuration -You may configure both the name of the plugin as well as which content type to use as your "Guide" entries in `src/config/guidemodule.php`. +You may configure both the name of the plugin as well as which content type to use as your "Guide" entries in `src/config/guide.php`. **NOTE**: The section you use must have public URLs disabled. If these are enabled the Guide will not pull this data in and will display an error. diff --git a/config/app.php b/config/app.php index e801103..108988d 100644 --- a/config/app.php +++ b/config/app.php @@ -14,9 +14,9 @@ */ return [ 'modules' => [ - 'guide-module' => [ - 'class' => \modules\guidemodule\GuideModule::class + 'guide' => [ + 'class' => \modules\guide\Guide::class ], ], - 'bootstrap' => ['guide-module'], + 'bootstrap' => ['guide'], ]; diff --git a/src/GuideModule.php b/src/Guide.php similarity index 58% rename from src/GuideModule.php rename to src/Guide.php index 958fefe..b2286f7 100644 --- a/src/GuideModule.php +++ b/src/Guide.php @@ -8,10 +8,7 @@ * @copyright Copyright (c) 2020 TrendyMinds */ -namespace modules\guidemodule; - -use modules\guidemodule\services\GuideModuleService as GuideModuleServiceService; -use modules\guidemodule\Config; +namespace modules\guide; use Craft; use craft\events\RegisterCpNavItemsEvent; @@ -25,33 +22,10 @@ use yii\base\Event; use yii\base\Module; -/** - * Craft plugins are very much like little applications in and of themselves. We’ve made - * it as simple as we can, but the training wheels are off. A little prior knowledge is - * going to be required to write a plugin. - * - * For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL, - * as well as some semi-advanced concepts like object-oriented programming and PHP namespaces. - * - * https://craftcms.com/docs/plugins/introduction - * - * @author TrendyMinds - * @package GuideModule - * @since 1.0.0 - * - * @property GuideModuleServiceService $guideModuleService - */ -class GuideModule extends Module +class Guide extends Module { // Static Properties // ========================================================================= - - /** - * Static property that is an instance of this module class so that it can be accessed via - * GuideModule::$instance - * - * @var GuideModule - */ public static $instance; // Public Methods @@ -62,8 +36,8 @@ class GuideModule extends Module */ public function __construct($id, $parent = null, array $config = []) { - Craft::setAlias('@modules/guidemodule', $this->getBasePath()); - $this->controllerNamespace = 'modules\guidemodule\controllers'; + Craft::setAlias('@modules/guide', $this->getBasePath()); + $this->controllerNamespace = 'modules\guide\controllers'; // Base template directory Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) { @@ -78,17 +52,6 @@ public function __construct($id, $parent = null, array $config = []) parent::__construct($id, $parent, $config); } - /** - * Set our $instance static property to this class so that it can be accessed via - * GuideModule::$instance - * - * Called after the module class is instantiated; do any one-time initialization - * here such as hooks and events. - * - * If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; - * you do not need to load it in your init() method. - * - */ public function init() { parent::init(); @@ -99,8 +62,8 @@ public function init() UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) { - $event->rules['guide'] = 'guide-module/default/index'; - $event->rules['guide/'] = 'guide-module/default/index'; + $event->rules['guide'] = 'guide/default/index'; + $event->rules['guide/'] = 'guide/default/index'; } ); @@ -125,11 +88,28 @@ function(RegisterUserPermissionsEvent $event) { function(RegisterCpNavItemsEvent $event) { $event->navItems[] = [ 'url' => 'guide', - 'label' => Config::getName(), - 'icon' => '@modules/guidemodule/icon-mask.svg' + 'label' => $this->getSettings()->name, + 'icon' => '@modules/guide/icon-mask.svg' ]; } ); } } + + /** + * The settings of what the public name and section for powering the Guide should be. + * These can be modified based on your project needs + * + * @return object + */ + public function getSettings(): object + { + return (object) [ + // The public facing name of the plugin in the Craft control panel + 'name' => 'Guide', + + // The handle of the section that drives the Guide. This section must not have public URLs + 'section' => 'guide', + ]; + } } diff --git a/src/assetbundles/guidemodule/GuideModuleAsset.php b/src/assetbundles/guidemodule/GuideModuleAsset.php deleted file mode 100644 index 135ee73..0000000 --- a/src/assetbundles/guidemodule/GuideModuleAsset.php +++ /dev/null @@ -1,59 +0,0 @@ -sourcePath = "@modules/guidemodule/assetbundles/guidemodule"; - - // define the dependencies - $this->depends = [ - CpAsset::class, - ]; - - $this->css = [ - 'css/GuideModule.css', - ]; - - parent::init(); - } -} diff --git a/src/assetbundles/guidemodule/css/GuideModule.css b/src/assetbundles/guidemodule/css/GuideModule.css deleted file mode 100644 index 94bf81a..0000000 --- a/src/assetbundles/guidemodule/css/GuideModule.css +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Guide module for Craft CMS - * - * Guide CSS - * - * @author TrendyMinds - * @copyright Copyright (c) 2020 TrendyMinds - * @link https://trendyminds.com - * @package GuideModule - * @since 1.0.0 - */ - -.sidebar nav li ul { - display: block; -} diff --git a/src/config/guidemodule.php b/src/config/guidemodule.php deleted file mode 100644 index aae1cec..0000000 --- a/src/config/guidemodule.php +++ /dev/null @@ -1,26 +0,0 @@ -requirePermission('canViewGuide'); // Display the template with the custom variables - return $this->renderTemplate('guide-module/index', [ - "pluginName" => Config::getName(), - "guideSection" => Config::getSection(), - "entry" => Entry::findOne([ - 'section' => Config::getSection(), - 'id' => Craft::$app->request->getSegment(2) - ]) - ]); + return $this->renderTemplate('guide/index', [ + "pluginName" => Guide::$instance->getSettings()->name, + "guideSection" => Guide::$instance->getSettings()->section, + "entry" => Entry::findOne([ + 'section' => Guide::$instance->getSettings()->section, + 'id' => Craft::$app->request->getSegment(2) + ]) + ]); } /** @@ -84,20 +63,20 @@ public function actionIndex() */ private function _validateGuide() { - $section = Craft::$app->sections->getSectionByHandle(Config::getSection()); + $section = Craft::$app->sections->getSectionByHandle(Guide::$instance->getSettings()->section); if (!$section) { - throw new \Exception("This section does not exist. You must supply the Guide with a valid section handle in config/guidemodule.php."); + throw new \Exception("This section does not exist. You must supply the Guide with a valid section handle in config/guide.php."); } $sectionSettings = Craft::$app->getSections()->getSectionSiteSettings($section->id); if (!$sectionSettings || count($sectionSettings) !== 1) { - throw new \Exception("This section does not have any valid settings. You must supply the Guide with a valid section handle in config/guidemodule.php."); + throw new \Exception("This section does not have any valid settings. You must supply the Guide with a valid section handle in config/guide.php."); } if ($sectionSettings[0]->hasUrls) { - throw new \Exception("The section you use to power the Guide cannot have public URLs. Please ensure your section does not have public URLs or use a different section handle in config/guidemodule.php"); + throw new \Exception("The section you use to power the Guide cannot have public URLs. Please ensure your section does not have public URLs or use a different section handle in config/guide.php"); } return true; diff --git a/src/templates/index.twig b/src/templates/index.twig index b25ef47..12bbdc6 100644 --- a/src/templates/index.twig +++ b/src/templates/index.twig @@ -1,7 +1,5 @@ {% extends "_layouts/cp" %} -{% do view.registerAssetBundle("modules\\guidemodule\\assetbundles\\guidemodule\\GuideModuleAsset") %} - {% set title = pluginName %} {% block sidebar %} @@ -9,7 +7,7 @@
    {% nav page in craft.entries.section(guideSection).all() %}
  • - {{page.title}} + {{ page.title }} {% ifchildren %}
      @@ -24,7 +22,13 @@ {% block content %} {% if entry.body is defined and entry.body %} -

      {{entry.title}}

      - {{entry.body}} +

      {{ entry.title }}

      + {{ entry.body }} {% endif %} {% endblock %} + +{% css %} + .sidebar nav li ul { + display: block; + } +{% endcss %}