Skip to content

Commit

Permalink
Craft 3: Enable all elements
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Aug 31, 2022
1 parent 05948e3 commit 1f07ced
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
55 changes: 35 additions & 20 deletions src/EntryMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EntryMeta extends Plugin
{
public const COLUMN_NAME = 'emMetadata';

public bool $hasCpSettings = true;
public $hasCpSettings = true;

public function init()
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
);
});
}
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 3 additions & 1 deletion src/templates/_meta.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% if data | length > 0 %}
<dl class="meta read-only entry-meta-plugin">
{% for key,value in data %}
<div class="data">
Expand All @@ -7,4 +8,5 @@
</dd>
</div>
{% endfor %}
</dl>
</dl>
{% endif %}
2 changes: 1 addition & 1 deletion src/templates/settings.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% import '_includes/forms.twig' as forms %}
{% import "_includes/forms" as forms %}

{{ forms.checkboxSelectField({
first: true,
Expand Down

0 comments on commit 1f07ced

Please sign in to comment.