Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add routing templates for Index pages #80

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
namespace boilerplate;

use Craft;
use boilerplate\behaviors\EntryIndexQueryBehavior;
use boilerplate\behaviors\IndexEntryBehaviors;
use boilerplate\behaviors\SectionRouterBehavior;
use craft\base\Element;
use craft\elements\Entry;
use craft\elements\db\EntryQuery;
use craft\events\DefineBehaviorsEvent;
use craft\web\Response;
use yii\base\Event;

Expand Down Expand Up @@ -80,6 +87,39 @@ public function onBeforeSendLivePreview(Event $event)
}
}

// define entry behaviors
// - custom index entry properties
public function onEntryDefineBehaviors(DefineBehaviorsEvent $event)
{
$entry = $event->sender;
if (
$entry->id &&
$entry->sectionId &&
strpos(
$entry->section->handle,
IndexEntryBehaviors::$sectionHandlePrefix,
) === 0
) {
$event->behaviors[$this->id . 'IndexEntry'] =
IndexEntryBehaviors::class;
}
}

// define custom index query
public function onEntryQueryDefineBehaviors(DefineBehaviorsEvent $event)
{
$event->behaviors[$this->id . EntryIndexQueryBehavior::class] =
EntryIndexQueryBehavior::class;
}

// define custom Section properties
public function onSectionDefineBehaviors(DefineBehaviorsEvent $event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think the SectionRouterBehavior class has been imported, or the onSectionDefineBehaviors property is being invoked below.

{
if ($event->sender instanceof Section && $event->sender->id) {
$event->behaviors[$this->id] = SectionRouterBehavior::class;
}
}

// Protected Methods
// =================

Expand All @@ -94,5 +134,15 @@ protected function addEventListeners()
$this,
'onBeforeSendLivePreview',
]);

Event::on(Entry::class, Element::EVENT_DEFINE_BEHAVIORS, [
$this,
'onEntryDefineBehaviors',
]);

Event::on(EntryQuery::class, EntryQuery::EVENT_DEFINE_BEHAVIORS, [
$this,
'onEntryQueryDefineBehaviors',
]);
}
}
39 changes: 39 additions & 0 deletions src/behaviors/EntryIndexQueryBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace boilerplate\behaviors;

use Craft;
use craft\elements\db\EntryQuery;
use craft\elements\Entry;
use craft\models\Section;
use yii\base\Behavior;

/**
* Class EntryIndexQueryBehavior
*
* Adds the ability to query for an entry's Index (single) entry
*
* @property Entry $owner
*/
class EntryIndexQueryBehavior extends Behavior
{
// query for the Index entry of the passed in value
public function index_($value): EntryQuery
{
$sectionHandle = '';

if ($value instanceof Entry) {
$sectionHandle = $value->getSection()->handle;
} elseif ($value instanceof Section) {
$sectionHandle = $value->handle;
} elseif (is_string($value)) {
$sectionHandle = $value;
}

if ($sectionHandle !== '') {
$this->owner->section('index_' . $sectionHandle)->limit(1);
}

return $this->owner;
}
}
36 changes: 36 additions & 0 deletions src/behaviors/IndexEntryBehaviors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace boilerplate\behaviors;

use Craft;
use craft\models\Section;
use yii\base\Behavior;

/**
* Class IndexEntryBehaviors
*
* Adds certain behaviors to all Index Single entries
*
* @property Entry $owner
*/
class IndexEntryBehaviors extends Behavior
{
// Sections which the behavior should be attached to
public static $sectionHandlePrefix = 'index_';

// Fetch the section for which this entry serves as the index
public function getIndexSection_(): Section|null
{
$indexSection = $this->owner->getSection()->handle;
$indexSection = preg_replace(
'/^' . $this::$sectionHandlePrefix . '/',
'',
$indexSection,
1,
);
$indexSection = Craft::$app->sections->getSectionByHandle(
$indexSection,
);
return $indexSection;
}
}
24 changes: 24 additions & 0 deletions templates/_routers/index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% minify %}
{#
Index Router

@param section Section object whose index is being rendered
(auto-set when coming via the router)
@param entry The section's index page single entry
(auto-set when directly hitting the single)
@param entries Query object of the index page's contents
(auto-set when coming via the router)
#}
{% set section = section ?? entry['indexSection_'] ?? entry.section %}
{% set entry = entry ?? craft.entries.index_(section).one() %}
{% set entries = entries ?? craft.entries.section(section) %}

{% if not entry %}
{% exit 404 %}
{% endif %}

{%- include [
'_views/indexes/' ~ section.handle|replace('index_'),
'_views/indexes/default',
] -%}
{% endminify %}
1 change: 1 addition & 0 deletions templates/_views/indexes/default.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends '_views/detail/default' %}