Skip to content

Commit

Permalink
Fixed project config not syncing field layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Apr 24, 2019
1 parent 1f4555f commit 3c4593d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
## 1.0.1 - 2019-04-24
### Fixed
- Fixed project config not syncing field layout

## 1.0.0 - 2018-03-04
- Initial Release
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ether/cart-notices",
"description": "Automatically show notices based off cart details",
"version": "1.0.0",
"version": "1.0.1",
"type": "craft-plugin",
"license": "proprietary",
"minimum-stability": "dev",
Expand Down
52 changes: 45 additions & 7 deletions src/CartNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@

namespace ether\cartnotices;

use Craft;
use craft\base\Plugin;
use craft\events\ConfigEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\models\FieldLayout;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use ether\cartnotices\elements\Notice;
use ether\cartnotices\web\twig\CraftVariableBehavior;
use yii\base\ErrorException;
use yii\base\Event;
use yii\base\Exception;
use yii\base\Model;
use yii\web\Response;

/**
* Class CartNotices
Expand Down Expand Up @@ -51,6 +57,11 @@ public function init ()
UrlManager::EVENT_REGISTER_CP_URL_RULES,
[$this, 'onRegisterCpUrlRules']
);

Craft::$app->projectConfig
->onAdd('cartNotices', [$this, 'handleChangedCartNotices'])
->onUpdate('cartNotices', [$this, 'handleChangedCartNotices'])
->onRemove('cartNotices', [$this, 'handleRemovedCartNotices']);
}

public function getCpNavItem ()
Expand All @@ -74,24 +85,30 @@ protected function createSettingsModel ()
}

/**
* @return mixed|\yii\web\Response
* @return mixed|Response
*/
public function getSettingsResponse ()
{
$url = UrlHelper::cpUrl('cart-notices/settings');

return \Craft::$app->controller->redirect($url);
return Craft::$app->controller->redirect($url);
}

/**
* @return bool
* @throws \yii\base\Exception
* @throws Exception
* @throws ErrorException
*/
public function beforeSaveSettings (): bool
{
$fieldLayout = \Craft::$app->getFields()->assembleLayoutFromPost();
$fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost();
$fieldLayout->type = Notice::class;
\Craft::$app->getFields()->saveLayout($fieldLayout);
Craft::$app->getFields()->saveLayout($fieldLayout);

Craft::$app->projectConfig->set(
'cartNotices',
$fieldLayout->getConfig()
);

return parent::beforeSaveSettings();
}
Expand All @@ -105,7 +122,7 @@ public function beforeSaveSettings (): bool
*/
protected function beforeInstall (): bool
{
if (!\Craft::$app->getPlugins()->isPluginInstalled('commerce'))
if (!Craft::$app->getPlugins()->isPluginInstalled('commerce'))
throw new \Exception('Commerce is required for this plugin to be installed!');

return parent::beforeInstall();
Expand Down Expand Up @@ -135,12 +152,33 @@ public function onRegisterCpUrlRules (RegisterUrlRulesEvent $event)
$event->rules['cart-notices/<type>'] = 'cart-notices/notice/index';
}

// Events: Project Config
// -------------------------------------------------------------------------

/**
* @param ConfigEvent $event
*
* @throws Exception
*/
public function handleChangedCartNotices (ConfigEvent $event)
{
$fieldLayout = FieldLayout::createFromConfig($event->newValue);
$fieldLayout->type = Notice::class;
Craft::$app->getFields()->saveLayout($fieldLayout);
}

public function handleRemovedCartNotices (ConfigEvent $event)
{
$fieldLayout = Craft::$app->getFields()->getLayoutByType(Notice::class);
Craft::$app->getFields()->deleteLayout($fieldLayout);
}

// Helpers
// =========================================================================

public static function t ($message, $params = [])
{
return \Craft::t('cart-notices', $message, $params);
return Craft::t('cart-notices', $message, $params);
}

}

0 comments on commit 3c4593d

Please # to comment.