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

122 consider stacked h1 pattern for guides #133

Open
wants to merge 11 commits into
base: 2.x
Choose a base branch
from
6 changes: 6 additions & 0 deletions localgov_guides.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
localgov_guides.settings:
title: Localgov Guides Settings
description: Configure the localgov_guides module
parent: system.admin_config_system
Copy link
Contributor

Choose a reason for hiding this comment

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

I think Content authoring would be a better parent? Would be interested in others opinions.

Copy link
Author

Choose a reason for hiding this comment

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

Me too. I couldn't figure if there was a standard localgovdrupal approach for these sorts of settings.

route_name: localgov_guides.settings
weight: 10
14 changes: 14 additions & 0 deletions localgov_guides.module
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ function localgov_guides_theme($existing, $type, $theme, $path) {
'format' => [],
],
],
'guides_page_header_title' => [
'variables' => [
'title' => NULL,
'overview' => NULL,
'node' => NULL,
],
],
'guides_page_header_lede' => [
'variables' => [
'lede' => NULL,
'overview' => NULL,
'node' => NULL,
],
],
'guides_prev_next_block' => [
'variables' => [
'previous_url' => NULL,
Expand Down
7 changes: 7 additions & 0 deletions localgov_guides.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
localgov_guides.settings:
path: '/admin/config/system/localgov_guides'
defaults:
_title: 'Localgov Guides Settings'
_form: 'Drupal\localgov_guides\Form\SettingsForm'
requirements:
_permission: 'administer site configuration'
1 change: 1 addition & 0 deletions localgov_guides.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ services:
# Alter page header.
localgov_guides.page_header:
class: Drupal\localgov_guides\EventSubscriber\PageHeaderSubscriber
arguments: ['@config.factory']
tags:
- { name: 'event_subscriber' }
52 changes: 42 additions & 10 deletions src/EventSubscriber/PageHeaderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
*/
class PageHeaderSubscriber implements EventSubscriberInterface {

/**
* The config.factory service.
*
* @var Drupal\Core\Config\ImmutableConfig
*/
protected $configFactory;

/**
* {@inheritdoc}
*/
public function __construct($config_factory) {
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
Expand All @@ -39,19 +53,37 @@ public function setPageHeader(PageHeaderDisplayEvent $event) {
}

$overview = $node->localgov_guides_parent->entity ?? NULL;
if (!empty($overview)) {
$event->setTitle($overview->getTitle());
if ($overview->get('body')->summary) {
$event->setLede([
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $overview->get('body')->summary,
]);
if (!$this->configFactory->get('localgov_guides.settings')->get('modern_header')) {
// The legacy rendering uses overview content for Guide Page titles.
if (!empty($overview)) {
$event->setTitle($overview->getTitle());
if ($overview->get('body')->summary) {
$event->setLede([
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $overview->get('body')->summary,
]);
}
$event->setCacheTags(Cache::mergeTags($node->getCacheTags(), $overview->getCacheTags()));
}
else {
$event->setLede('');
}
$event->setCacheTags(Cache::mergeTags($node->getCacheTags(), $overview->getCacheTags()));
}
else {
$event->setLede('');
// The newer rendering uses the node's own content for Guide page titles.
$event->setTitle([
'#theme' => 'guides_page_header_title',
'#title' => $node->getTitle(),
'#node' => $node,
'#overview' => $overview,
]);
$event->setLede([
'#theme' => 'guides_page_header_lede',
'#lede' => $overview->getTitle(),
'#node' => $node,
'#overview' => $overview,
]);
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Drupal\localgov_guides\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Configure LocalGov Guides settings for this site.
*/
class SettingsForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'localgov_guides_settings';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['localgov_guides.settings'];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['modern_header'] = [
'#type' => 'checkbox',
'#title' => $this->t('Use "modern" header block on Guide pages'),
'#description' => $this->t('When checked, Localgov Guide pages will use modern Twig-based render arrays for the header block title and lede.'),
'#default_value' => (bool) $this->config('localgov_guides.settings')->get('modern_header'),
];
return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('localgov_guides.settings')
->set('modern_header', $form_state->getValue('modern_header'))
->save();
parent::submitForm($form, $form_state);
}

}
3 changes: 3 additions & 0 deletions templates/guides-page-header-lede.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if lede is not empty %}
<p{{ attributes }}>{{ lede }}</p>
{% endif %}
1 change: 1 addition & 0 deletions templates/guides-page-header-title.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ title }}