Skip to content

Commit

Permalink
Clean up, update changelog and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed May 30, 2024
1 parent a72e918 commit cd5db46
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 16 additions & 22 deletions src/MatrixMate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 */
Expand All @@ -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);
});
}

}

/**
Expand All @@ -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 = '*';
}
Expand Down Expand Up @@ -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 = <<<JS
if (Craft && Craft.MatrixMate) {
Expand Down
1 change: 0 additions & 1 deletion src/services/MatrixMateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Craft;
use craft\base\Component;
use craft\base\Field;
use craft\elements\User;
use craft\fields\Matrix;
use craft\helpers\Json;

Expand Down

0 comments on commit cd5db46

Please sign in to comment.