Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Update plugin for Composer 2
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbushnell committed Mar 30, 2021
1 parent 96da9e0 commit 64da3f6
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 196 deletions.
1 change: 0 additions & 1 deletion .craftplugin

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
},
```
Expand All @@ -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.
6 changes: 3 additions & 3 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*/
return [
'modules' => [
'guide-module' => [
'class' => \modules\guidemodule\GuideModule::class
'guide' => [
'class' => \modules\guide\Guide::class
],
],
'bootstrap' => ['guide-module'],
'bootstrap' => ['guide'],
];
70 changes: 25 additions & 45 deletions src/GuideModule.php → src/Guide.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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/<page:\d+>'] = 'guide-module/default/index';
$event->rules['guide'] = 'guide/default/index';
$event->rules['guide/<page:\d+>'] = 'guide/default/index';
}
);

Expand All @@ -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',
];
}
}
59 changes: 0 additions & 59 deletions src/assetbundles/guidemodule/GuideModuleAsset.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/assetbundles/guidemodule/css/GuideModule.css

This file was deleted.

26 changes: 0 additions & 26 deletions src/config/guidemodule.php

This file was deleted.

51 changes: 15 additions & 36 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,13 @@
* @copyright Copyright (c) 2020 TrendyMinds
*/

namespace modules\guidemodule\controllers;

use modules\guidemodule\Config;
namespace modules\guide\controllers;

use Craft;
use craft\elements\Entry;
use craft\web\Controller;
use modules\guide\Guide;

/**
* Default Controller
*
* Generally speaking, controllers are the middlemen between the front end of
* the CP/website and your module’s services. They contain action methods which
* handle individual tasks.
*
* A common pattern used throughout Craft involves a controller action gathering
* post data, saving it on a model, passing the model off to a service, and then
* responding to the request appropriately depending on the service method’s response.
*
* Action methods begin with the prefix “action”, followed by a description of what
* the method does (for example, actionSaveIngredient()).
*
* https://craftcms.com/docs/plugins/controllers
*
* @author TrendyMinds
* @package GuideModule
* @since 1.0.0
*/
class DefaultController extends Controller
{

Expand All @@ -54,7 +33,7 @@ class DefaultController extends Controller

/**
* Handle a request going to our module's index action URL,
* e.g.: actions/guide-module/default
* e.g.: actions/guide/default
*
* @return mixed
*/
Expand All @@ -67,14 +46,14 @@ public function actionIndex()
$this->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)
])
]);
}

/**
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 64da3f6

Please sign in to comment.