Created
June 23, 2025 09:53
-
-
Save Megafry/06280a843e594f7e6e8f2581fb611d3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "templates/_base.twig" %} | |
{% set components = craft.extendCraft.getComponents() %} | |
{% block extraHead %}{% endblock %} | |
{% block main %} | |
{{ include ("utils/_component-loop" , {components: components}, withContext = false) }} | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\ExtendCraft\variables; | |
use craft\elements\Entry; | |
use Craft; | |
use craft\elements\MatrixBlock; | |
use craft\services\Fields; | |
class ExtendCraft | |
{ | |
// for CraftCMS 5 | |
public function getCmpEntries(): array | |
{ | |
$entries = []; | |
$entryTypes = Craft::$app->entries->getAllEntryTypes(); | |
$matchingTypes = array_filter($entryTypes, fn($entryType) => str_starts_with($entryType->handle, 'cmp')); | |
$typeHandles = array_map(fn($entryType) => $entryType->handle, $matchingTypes); | |
foreach ($typeHandles as $handle) { | |
// orderBy('RAND()') | |
$cmp = Entry::find()->type($handle)->one(); | |
if ($cmp) { | |
$entries[] = $cmp; | |
} | |
} | |
return $entries; | |
} | |
// for CraftCMS 4 | |
public function getComponents(): array | |
{ | |
$entries = []; | |
$matrixField = Craft::$app->getFields()->getFieldByHandle('components'); | |
if ($matrixField) { | |
// Get all block types | |
$blockTypes = $matrixField->getBlockTypes(); | |
foreach ($blockTypes as $blockType) { | |
// Find entries for each block type | |
$block = MatrixBlock::find() | |
->field('components') | |
->type($blockType->handle) | |
->one(); | |
$entries[] = $block; | |
} | |
} | |
return $entries; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment