Skip to content

Commit

Permalink
Add: support for matrix & neo nested related elements
Browse files Browse the repository at this point in the history
  • Loading branch information
MindseekerMedia committed Oct 6, 2024
1 parent 2f7534f commit 56eba86
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/RelatedElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use craft\elements\Category;
use craft\elements\Entry;
use craft\events\DefineHtmlEvent;
use craft\fields\Matrix;
use yii\base\Event;

/**
Expand All @@ -35,7 +36,7 @@ private function attachEventHandlers(): void
Event::on(
Element::class,
Element::EVENT_DEFINE_SIDEBAR_HTML,
static fn (DefineHtmlEvent $event) => $event->html .=
static fn(DefineHtmlEvent $event) => $event->html .=
($event->sender instanceof Entry ||
$event->sender instanceof Category ||
$event->sender instanceof Asset)
Expand All @@ -53,7 +54,9 @@ public static function renderTemplate(Entry|Category|Asset $entry): string
];

$relatedElements = [];
$nestedRelatedElements = [];
$hasResults = false;
$customFields = $entry->getFieldLayout()->getCustomFields();

foreach ($relatedTypes as $type => $class) {
$relatedElements[$type] = $class::find()->relatedTo($entry)->status(null)->orderBy('title')->all();
Expand All @@ -62,11 +65,35 @@ public static function renderTemplate(Entry|Category|Asset $entry): string
}
}

foreach ($customFields as $field) {
$isMatrixField = $field instanceof Matrix;
$isNeoField = class_exists('\benf\neo\Field') && get_class($field) === \benf\neo\Field::class;

if ($isMatrixField || $isNeoField) {
$blocks = $entry->getFieldValue($field->handle);

foreach ($blocks->all() as $block) {
foreach ($relatedTypes as $type => $class) {
$nestedRelatedElements[$field->name][$type] = $class::find()
->relatedTo($block)
->status(null)
->orderBy('title')
->all();

if (!empty($nestedRelatedElements[$field->name][$type])) {
$hasResults = true;
}
}
}
}
}

return Craft::$app->getView()->renderTemplate(
'related-elements/_element-sidebar',
[
'hasResults' => $hasResults,
'relatedElements' => $relatedElements,
'nestedRelatedElements' => $nestedRelatedElements,
]
);
}
Expand Down
33 changes: 33 additions & 0 deletions src/templates/_element-sidebar.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@
</div>
{% endfor %}
{% endfor %}
{% for fieldKey, field in nestedRelatedElements %}
<div class="field" style="padding-block: 10px;">
<em>
{{ fieldKey }}
</em>
</div>
{% for key, section in field %}
{% for element in section %}
<div class="field" style="min-height: 34px; padding-top: 0; padding-bottom: 10px; border-top: none;">
<div class="flex flex-nowrap gap-s">
<span style="position: relative; top: -3px;">∟</span>
<a href="{{ element.cpEditUrl }}" class="flex flex-nowrap gap-s">
<span class="status small {{ element.status }}" role="img" aria-label="Status: {{ element.status }}" style="margin-inline-end: 0;"></span>
<span class="cp-icon small">
{% switch key %}
{% case 'Entry' %}
{{ iconSvg('newspaper') }}
{% case 'Category' %}
{{ iconSvg('sitemap') }}
{% case 'Asset' %}
{{ iconSvg('asset') }}
{% endswitch %}
</span>
<span style="word-break: break-word;">
{{ element.title ?? element.id }}
</span>
</a>
</div>

</div>
{% endfor %}
{% endfor %}
{% endfor %}
{% else %}
<div class="field" style="padding-block: 10px;">
<p>No related elements found.</p>
Expand Down

0 comments on commit 56eba86

Please sign in to comment.