From 4ee1a901a44a9913c0937719711aa6abcb5fef0a Mon Sep 17 00:00:00 2001 From: Mat Fish Date: Wed, 1 Dec 2021 09:13:21 +0200 Subject: [PATCH] humanize cp keys --- CHANGELOG.md | 7 +++++ src/EntryMeta.php | 47 ++++++++++++++++++++------------ src/templates/_meta.twig | 4 +-- src/twig/EntryMetaExtension.php | 48 +++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 src/twig/EntryMetaExtension.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 22fc74d..1b8a9f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Release Notes for Entry Meta +## 1.0.0-rc.4 - 2021-12-01 +### Added +- Convert keys and values displayed on sidebar to human-readable format. + +### Changed +- Refactored plugin class + ## 1.0.0-rc.3 - 2021-11-29 ### Changed - Breaking Change: Remove query service. Use ActiveQuery instead for more flexible queries. diff --git a/src/EntryMeta.php b/src/EntryMeta.php index d9fc03d..b08613b 100644 --- a/src/EntryMeta.php +++ b/src/EntryMeta.php @@ -11,6 +11,7 @@ use matfish\EntryMeta\behaviors\EntryActiveQueryBehavior; use matfish\EntryMeta\behaviors\EntryElementBehavior; use matfish\EntryMeta\models\Settings; +use matfish\EntryMeta\twig\EntryMetaExtension; use yii\base\Event; use craft\records\Entry as EntryRecord; @@ -23,15 +24,30 @@ public function init() { parent::init(); + $this->registerElementBehavior(); + $this->registerQueryBehavior(); + + if (Craft::$app->request->getIsCpRequest() && $this->settings->displayMetadataInCp) { + $this->registerCpMetaHook(); + } + } + + protected function createSettingsModel() + { + return new Settings(); + } + + private function registerElementBehavior() + { Event::on(ActiveQuery::class, ActiveQuery::EVENT_INIT, function ($e) { if ($e->sender->modelClass === EntryRecord::class) { $e->sender->attachBehaviors([EntryActiveQueryBehavior::class]); } }); + } - /** - * Attach a behavior after an entry has been loaded from the database (populated). - */ + private function registerQueryBehavior() + { Event::on(ElementQuery::class, ElementQuery::EVENT_AFTER_POPULATE_ELEMENT, function (PopulateElementEvent $event) { $element = $event->element; @@ -40,22 +56,19 @@ public function init() } }); - if ($this->settings->displayMetadataInCp) { - Craft::$app->getView()->hook(self::CP_HOOK, function (array &$context) { - - $entry = $context['entry']; - $meta = $entry->getEntryMetadata(); - - return Craft::$app->view->renderTemplate('entry-meta/_meta', [ - 'data' => $meta - ]); - }); - } } - protected function createSettingsModel() + private function registerCpMetaHook() { - return new Settings(); - } + Craft::$app->view->registerTwigExtension(new EntryMetaExtension()); + + Craft::$app->getView()->hook(self::CP_HOOK, function (array &$context) { + $entry = $context['entry']; + $meta = $entry->getEntryMetadata(); + return Craft::$app->view->renderTemplate('entry-meta/_meta', [ + 'data' => $meta + ]); + }); + } } \ No newline at end of file diff --git a/src/templates/_meta.twig b/src/templates/_meta.twig index 15dec4f..7392eca 100644 --- a/src/templates/_meta.twig +++ b/src/templates/_meta.twig @@ -1,8 +1,8 @@ {% for key,value in data %}
-
{{ key }}
+
{{ key | keyForHumans }}
- {{ value | json_encode }} + {{ value | valueForHumans | raw }}
{% endfor %} \ No newline at end of file diff --git a/src/twig/EntryMetaExtension.php b/src/twig/EntryMetaExtension.php new file mode 100644 index 0000000..76798e4 --- /dev/null +++ b/src/twig/EntryMetaExtension.php @@ -0,0 +1,48 @@ +' . json_encode($value) . ''; + } + + if (is_bool($value)) { + return $value ? 'true' : 'false'; + } + + return $value; + } + +} \ No newline at end of file