From 1f07cede9a2151de446e690968bc113bb9a0a61a Mon Sep 17 00:00:00 2001 From: Mat Fish Date: Wed, 31 Aug 2022 12:37:46 +0300 Subject: [PATCH] Craft 3: Enable all elements --- README.md | 4 +-- composer.json | 2 +- src/EntryMeta.php | 55 +++++++++++++++++++++++-------------- src/models/Settings.php | 4 +-- src/templates/_meta.twig | 4 ++- src/templates/settings.twig | 2 +- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 772e5da..fe17e49 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -> Version 3 is now out (for Craft 4) extending metadata capability from entries to all element types! +> Version 1.1 is now out (for Craft 3) extending metadata capability from entries to all element types! > Plugin name was accordingly changed from Entry Meta to Element Meta. -> To migrate from Version 2 simply change column name on `entries` table from `metadata` to `emMetadata` and select `Entry` on the settings page. +> To migrate from Version 1.0 simply change column name on `entries` table from `metadata` to `emMetadata` and select `Entry` on the settings page. # Element Meta This package adds the ability to save schemaless metadata to all element types, including custom elements. diff --git a/composer.json b/composer.json index 41f2389..e205051 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "docs": "https://github.com/matfish2/craft-entry-meta/blob/master/README.md" }, "require": { - "craftcms/cms": "^4.0" + "craftcms/cms": "^3.7.0" }, "autoload": { "psr-4": { diff --git a/src/EntryMeta.php b/src/EntryMeta.php index 2f4abc4..a0517fd 100644 --- a/src/EntryMeta.php +++ b/src/EntryMeta.php @@ -26,7 +26,7 @@ class EntryMeta extends Plugin { public const COLUMN_NAME = 'emMetadata'; - public bool $hasCpSettings = true; + public $hasCpSettings = true; public function init() { @@ -140,8 +140,10 @@ public function getAllEnabled() $res[] = ClassesMap::LOOK_UP[$key]; } - foreach ($this->settings->enabledForCustom as $val) { - $res[] = array_reverse($val); + if (is_array($this->settings->enabledForCustom)) { + foreach ($this->settings->enabledForCustom as $val) { + $res[] = array_reverse($val); + } } return $res; @@ -157,8 +159,11 @@ public function getEnabledActiveRecords(): array $res[] = ClassesMap::LOOK_UP[$key][0]; } - foreach ($this->settings->enabledForCustom as $val) { - $res[] = $val[1]; + if (is_array($this->settings->enabledForCustom)) { + foreach ($this->settings->enabledForCustom as $val) { + $res[] = $val[1]; + } + } return $res; @@ -175,8 +180,10 @@ private function getEnabledElements(): array $res[] = ClassesMap::LOOK_UP[$key][1]; } - foreach ($this->settings->enabledForCustom as $val) { - $res[] = $val[0]; + if (is_array($this->settings->enabledForCustom)) { + foreach ($this->settings->enabledForCustom as $val) { + $res[] = $val[0]; + } } return $res; @@ -194,10 +201,12 @@ public function afterSaveSettings(): void $migrator->add($table); } - foreach ($this->settings->enabledForCustom as $val) { - $table = $detector->detect($val[1]); + if (is_array($this->settings->enabledForCustom)) { + foreach ($this->settings->enabledForCustom as $val) { + $table = $detector->detect($val[1]); - $migrator->add($table); + $migrator->add($table); + } } $cache = \Craft::$app->cache; @@ -239,22 +248,28 @@ private function registerCpMetaHooks(): void { Craft::$app->view->registerTwigExtension(new EntryMetaExtension()); - $enabled = $this->getEnabledElements(); + $lookup = [ + 'user' => 'cp.users.edit.details', + 'entry' => 'cp.entries.edit.details', + 'category' => 'cp.categories.edit.details', + 'asset' => 'cp.assets.edit.details', + ]; + + $enabled = $this->getEnabled(); foreach ($enabled as $el) { + if (isset($lookup[$el])) { + Craft::$app->getView()->hook($lookup[$el], function (array &$context) use ($el) { + + $entry = $el==='asset' ? $context['element'] : $context[$el]; - Event::on( - $el, - Element::EVENT_DEFINE_SIDEBAR_HTML, - function (DefineHtmlEvent $event) { - $entry = $event->sender; $meta = $entry->getElementMetadata(); - $template = Craft::$app->view->renderTemplate('entry-meta/_meta', [ + + return Craft::$app->view->renderTemplate('entry-meta/_meta', [ 'data' => $meta ]); - $event->html .= $template; - } - ); + }); + } } } diff --git a/src/models/Settings.php b/src/models/Settings.php index 7f3194d..225c15d 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -9,8 +9,8 @@ class Settings extends Model { public bool $displayMetadataInCp = true; - public array $enabledFor = []; - public array $enabledForCustom = []; + public $enabledFor = []; + public $enabledForCustom = []; public function rules() : array { diff --git a/src/templates/_meta.twig b/src/templates/_meta.twig index 285a777..948c3f6 100644 --- a/src/templates/_meta.twig +++ b/src/templates/_meta.twig @@ -1,3 +1,4 @@ +{% if data | length > 0 %}
{% for key,value in data %}
@@ -7,4 +8,5 @@
{% endfor %} -
\ No newline at end of file + +{% endif %} \ No newline at end of file diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 8cbecf6..9368025 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -1,4 +1,4 @@ -{% import '_includes/forms.twig' as forms %} +{% import "_includes/forms" as forms %} {{ forms.checkboxSelectField({ first: true,