Skip to content

Commit

Permalink
humanize cp keys
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Dec 1, 2021
1 parent 281cdf3 commit 4ee1a90
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
47 changes: 30 additions & 17 deletions src/EntryMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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
]);
});
}
}
4 changes: 2 additions & 2 deletions src/templates/_meta.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% for key,value in data %}
<div class="data">
<h5 class="heading">{{ key }}</h5>
<h5 class="heading">{{ key | keyForHumans }}</h5>
<div class="value">
<span>{{ value | json_encode }}</span>
<span>{{ value | valueForHumans | raw }}</span>
</div>
</div>
{% endfor %}
48 changes: 48 additions & 0 deletions src/twig/EntryMetaExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace matfish\EntryMeta\twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class EntryMetaExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('keyForHumans', [$this, 'getKeyForHumans']),
new TwigFilter('valueForHumans', [$this, 'getValueForHumans'])
];
}

public function getKeyForHumans($key)
{
$key = ucfirst($key);

if (str_contains($key, '_')) { // Snake case
$res = implode(' ', explode('_', $key));

return mb_convert_case($res, MB_CASE_TITLE, "UTF-8");
}

// Camel case
$key = preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $key);

return $key;

}

public function getValueForHumans($value)
{
if (is_array($value)) {
return '<code>' . json_encode($value) . '</code>';
}

if (is_bool($value)) {
return $value ? 'true' : 'false';
}

return $value;
}

}

0 comments on commit 4ee1a90

Please sign in to comment.