-
Notifications
You must be signed in to change notification settings - Fork 3
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
pl-mnm
wants to merge
2
commits into
dev
Choose a base branch
from
feat/indexRouting
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
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; | ||
} | ||
} |
This file contains 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
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; | ||
} | ||
} |
This file contains 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
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 %} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends '_views/detail/default' %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theonSectionDefineBehaviors
property is being invoked below.