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

Make it so homepage: accepts a singleton, or a contentType listing #1336

Merged
merged 1 commit into from
Apr 27, 2020
Merged
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
11 changes: 6 additions & 5 deletions config/bolt/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ omit_backgrounds: true
#favicon: images/favicon-bolt.ico

# The default content to use for the homepage, and the template to render it
# with. This can either be a specific record (like `page/1`) or a listing of
# records (like `entries`). In the chosen homepage_template, you will have
# `record` or `records` at your disposal, depending on the homepage setting.
# with. This can either be a singleton like `homepage`, a specific record (like
# `page/1`) or a listing of records (like `entries`). In the chosen
# homepage_template, you will have `record` or `records` at your disposal,
# depending on the homepage setting.
#
# Note: If you've changed the filename, and your changes do not show up on
# the website, be sure to check for a theme.yml file in your themes
# the website, be sure to check for a theme.yaml file in your themes
# folder. If a template is set there, it will override the setting here.
homepage: homepage/1
homepage: homepage
homepage_template: index.twig

# The default content for the 404 page. Can be an (array of) template names or
Expand Down
12 changes: 10 additions & 2 deletions src/Controller/Frontend/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ public function homepage(ContentRepository $contentRepository): Response
{
$homepage = $this->config->get('theme/homepage') ?: $this->config->get('general/homepage');
$params = explode('/', $homepage);
$contentType = $this->config->get('contenttypes/' . $params[0]);

// Perhaps we need a listing instead. If so, forward the Request there
if (! $contentType->get('singleton') && ! isset($params[1])) {
return $this->forward('Bolt\Controller\Frontend\ListingController::listing', [
'contentTypeSlug' => $homepage,
]);
}

// @todo Get $homepage content, using "setcontent"
$record = $contentRepository->findOneBy([
'contentType' => $params[0],
'id' => $params[1],
'contentType' => $contentType->get('slug'),
'id' => $params[1] ?? 1,
]);
if (! $record) {
$record = $contentRepository->findOneBy(['contentType' => $params[0]]);
Expand Down
27 changes: 16 additions & 11 deletions src/TemplateChooser.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,30 @@ public function forRecord(Content $record): array
$templates = collect($this->forHomepage());
}

// First candidate: Content record has a templateselect field, and it's set.
// Second candidate: Content record has a templateselect field, and it's set.
foreach ($definition->get('fields') as $name => $field) {
if ($field['type'] === 'templateselect' && $record->hasField($name)) {
$templates->push((string) $record->getField($name));
}
}

// Second candidate: defined specifically in the content type.
// Third candidate: defined specifically in the content type.
if ($definition->has('record_template')) {
$templates->push($definition->get('record_template'));
}

// Third candidate: a template with the same filename as the name of
// Fourth candidate: a template with the same filename as the name of
// the content type.
$templates->push($definition->get('singular_slug') . '.html.twig');
$templates->push($definition->get('singular_slug') . '.twig');

// Fourth candidate: Theme-specific config.yml file.
// Fifth candidate: Theme-specific config.yml file.
$templates->push($this->config->get('theme/record_template'));

// Fifth candidate: global config.yml
// Sixth candidate: global config.yml
$templates->push($this->config->get('general/record_template'));

// Sixth candidate: fallback to 'record.html.twig'
// Seventh candidate: fallback to 'record.html.twig'
$templates->push('record.html.twig');

return $templates->unique()->filter()->toArray();
Expand All @@ -93,23 +93,28 @@ public function forListing(ContentType $contentType): array
{
$templates = new Collection();

// First candidate: defined specifically in the content type.
// First candidate: Content record is the homepage
if ($this->contentExtension->isHomepageListing($contentType)) {
$templates = collect($this->forHomepage());
}

// Second candidate: defined specifically in the content type.
if (! empty($contentType['listing_template'])) {
$templates->push($contentType['listing_template']);
}

// Second candidate: a template with the same filename as the name of
// Third candidate: a template with the same filename as the name of
// the content type.
$templates->push($contentType->getSlug() . '.html.twig');
$templates->push($contentType->getSlug() . '.twig');

// Third candidate: Theme-specific config.yml file.
// Fourth candidate: Theme-specific config.yml file.
$templates->push($this->config->get('theme/listing_template'));

// Fourth candidate: Global config.yml
// Fifth candidate: Global config.yml
$templates->push($this->config->get('general/listing_template'));

// Fifth candidate: fallback to 'listing.html.twig'
// Sixth candidate: fallback to 'listing.html.twig'
$templates->push('listing.html.twig');

return $templates->unique()->filter()->toArray();
Expand Down
12 changes: 12 additions & 0 deletions src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Bolt\Twig;

use Bolt\Configuration\Config;
use Bolt\Configuration\Content\ContentType;
use Bolt\Entity\Content;
use Bolt\Entity\Field\Excerptable;
use Bolt\Entity\Field\ImageField;
Expand Down Expand Up @@ -749,4 +750,15 @@ private function isSpecialpage(Content $content, string $type): bool

return false;
}

public function isHomepageListing(ContentType $contentType): bool
{
$homepageSetting = $this->config->get('general/homepage');

if ($homepageSetting === $contentType->get('slug') || $homepageSetting === $contentType->get('singular_slug')) {
return true;
}

return false;
}
}