From cd5db463e00a6614503ec067f23315d72c4eb6f8 Mon Sep 17 00:00:00 2001 From: Mats Mikkel Rummelhoff Date: Thu, 30 May 2024 23:26:56 +0200 Subject: [PATCH] Clean up, update changelog and readme --- CHANGELOG.md | 3 +++ README.md | 1 + src/MatrixMate.php | 38 +++++++++++++----------------- src/services/MatrixMateService.php | 1 - 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2f101f..2c4c164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ 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/). +## Unreleased +- Adds support for Commerce products + ## 2.1.4 - 2023-11-11 ### Fixed - Fixed an issue where block types hidden via `hiddenTypes` could still show up in Matrix blocks' cogwheel menus, if they were included in one or several block type groups. diff --git a/README.md b/README.md index 9a51db6..464a785 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Additionally, a "global" config that will apply everywhere else is added using t `globalSet:{handle}` - Apply for a particular global set `volume:{handle}` - Apply for assets in a particular volume `users` - Apply for user account edit pages +`productType:{handle}` - Apply for Commerce products with a particular product type #### Config context inheritance diff --git a/src/MatrixMate.php b/src/MatrixMate.php index 0a665fc..03e3690 100644 --- a/src/MatrixMate.php +++ b/src/MatrixMate.php @@ -15,28 +15,18 @@ use craft\base\Plugin; use craft\elements\Asset; use craft\elements\Category; -use craft\elements\db\ElementQuery; use craft\elements\Entry; use craft\elements\GlobalSet; use craft\elements\User; use craft\events\DefineHtmlEvent; -use craft\events\PopulateElementEvent; -use craft\events\TemplateEvent; use craft\helpers\Json; use craft\services\Fields; use craft\web\Application; -use craft\web\Controller; - -use craft\web\CpScreenResponseFormatter; -use craft\web\Request; -use craft\web\Response; -use craft\web\View; use vaersaagod\matrixmate\assetbundles\matrixmate\MatrixMateAsset; use vaersaagod\matrixmate\services\MatrixMateService; use vaersaagod\matrixmate\models\Settings; -use yii\base\ActionEvent; use yii\base\Event; /** @@ -150,16 +140,6 @@ function (DefineHtmlEvent $event) { } ); - // Register asset bundle for products - $commercePlugin = Craft::$app->getPlugins()->getPlugin('commerce', false); - if($commercePlugin && $commercePlugin->isInstalled) { - Craft::$app->getView()->hook('cp.commerce.product.edit.content', function (array $context) { - /** @var Element|null $element */ - $element = $context['product'] ?? null; - $this->registerAssetBundleForElement($element); - }); - } - // Register asset bundle for users Craft::$app->getView()->hook('cp.users.edit', function (array $context) { /** @var Element|null $element */ @@ -174,6 +154,15 @@ function (DefineHtmlEvent $event) { $this->registerAssetBundleForElement($element); }); + // Register asset bundle for products + if (!empty(Craft::$app->getPlugins()->getPlugin('commerce'))) { + Craft::$app->getView()->hook('cp.commerce.product.edit.content', function (array $context) { + /** @var Element|null $element */ + $element = $context['product'] ?? null; + $this->registerAssetBundleForElement($element); + }); + } + } /** @@ -193,7 +182,12 @@ protected function getContextForElement(Element $element): string } elseif ($element instanceof User) { $context = 'users'; } elseif ($element instanceof \craft\commerce\elements\Product) { - $context = "productType:{$element->getType()->id}"; + try { + $context = "productType:{$element->getType()->id}"; + } catch (\Throwable $e) { + Craft::error($e, __METHOD__); + $context = '*'; + } } else { $context = '*'; } @@ -223,7 +217,7 @@ protected function registerAssetBundleForElement(?Element $element): void } $context = $this->getContextForElement($element); - $configJs = Json::encode($fieldConfig, JSON_UNESCAPED_UNICODE); + $configJs = Json::encode($fieldConfig); $js = <<