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 a view display extender for the localgov_page_header for the lede #257

Open
wants to merge 8 commits into
base: 2.x
Choose a base branch
from
9 changes: 9 additions & 0 deletions config/schema/localgov_core.views.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
views.display_extender.localgov_page_header_display_extender:
type: views_display_extender
mapping:
lede:
type: text
label: 'Lede'
tokenize:
type: boolean
label: 'Should replacement tokens be used from the first row'
46 changes: 44 additions & 2 deletions src/Event/PageHeaderDisplayEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Drupal\localgov_core\Event;

use Drupal\Component\EventDispatcher\Event;
use Drupal\Core\Entity\EntityInterface;
use Drupal\views\ViewExecutable;

/**
* Event that is fired when displaying the page header.
Expand All @@ -18,6 +20,13 @@ class PageHeaderDisplayEvent extends Event {
*/
protected $entity = NULL;

/**
* View executable associated with the current route.
*
* @var \Drupal\views\ViewExecutable|null
*/
protected $view = NULL;

/**
* The page lede override.
*
Expand Down Expand Up @@ -56,7 +65,10 @@ class PageHeaderDisplayEvent extends Event {
/**
* {@inheritdoc}
*/
public function __construct($entity) {
public function __construct($entity = NULL) {
// @todo remove the $entity paramater or deprecate it.
// Since we should use the setters and getters.
// Or we can mark this class as internal?
$this->entity = $entity;
}

Expand All @@ -66,10 +78,40 @@ public function __construct($entity) {
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity.
*/
public function getEntity() {
public function getEntity() : ?EntityInterface {
return $this->entity;
}

/**
* Entity setter.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*/
public function setEntity(EntityInterface $entity) : void {
$this->entity = $entity;
}

/**
* View getter.
*
* @return \Drupal\views\ViewExecutable|null
* The view.
*/
public function getView() : ?ViewExecutable {
return $this->view;
}

/**
* View setter.
*
* @param \Drupal\views\ViewExecutable $view
* The view.
*/
public function setView(ViewExecutable $view) {
$this->view = $view;
}

/**
* Lede getter.
*
Expand Down
50 changes: 48 additions & 2 deletions src/Plugin/Block/PageHeaderBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Drupal\localgov_core\Event\PageHeaderDisplayEvent;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

Expand Down Expand Up @@ -62,6 +64,13 @@ class PageHeaderBlock extends BlockBase implements ContainerFactoryPluginInterfa
*/
protected $entity = NULL;

/**
* View executable associated with the current route.
*
* @var \Drupal\views\ViewExecutable|null
*/
protected $view = NULL;

/**
* The page title override.
*
Expand Down Expand Up @@ -130,6 +139,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$route = $this->currentRouteMatch->getRouteObject();
if (!is_null($route)) {
$parameters = $route->getOption('parameters');
$defaults = $route->getDefaults();
if (!is_null($parameters)) {
foreach ($parameters as $name => $options) {
if (!isset($options['type'])) {
Expand All @@ -152,10 +162,31 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
}
}
}

// If this is a view path, set the view instead.
if (isset($defaults['view_id']) && isset($defaults['display_id'])) {
$view = Views::getView($defaults['view_id']);
if ($view) {
$view->setDisplay($defaults['display_id']);
$this->view = $view;
}
}
}

// Dispatch event to allow modules to alter block content.
$event = new PageHeaderDisplayEvent($this->entity);
$event = new PageHeaderDisplayEvent();

// If an entity is set, pass to the event.
if ($this->entity instanceof EntityInterface) {
$event->setEntity($this->entity);
}

// If a view is set, pass to the event.
if ($this->view instanceof ViewExecutable) {
$event->setView($this->view);
}

// Dispatch event.
$this->eventDispatcher->dispatch($event, PageHeaderDisplayEvent::EVENT_NAME);

// Set the title, lede, visibility and cache tags.
Expand All @@ -164,7 +195,8 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$this->lede = is_null($event->getLede()) ? $this->getLede() : $event->getLede();
$this->visible = $event->getVisibility();
$entityCacheTags = is_null($this->entity) ? [] : $this->entity->getCacheTags();
$this->cacheTags = is_null($event->getCacheTags()) ? $entityCacheTags : $event->getCacheTags();
$viewCacheTags = is_null($this->view) ? [] : $this->view->getCacheTags();
$this->cacheTags = is_null($event->getCacheTags()) ? array_merge($entityCacheTags, $viewCacheTags) : $event->getCacheTags();
}

/**
Expand Down Expand Up @@ -224,6 +256,20 @@ protected function getLede() {
];
}

// Return view custom summary.
if ($this->view instanceof ViewExecutable) {
$extender = $this->view->getDisplay()->getExtenders()['localgov_page_header_display_extender'] ?? NULL;

// Need to render view to apply tokens.
$this->view->render();
$lede = $extender->getLede();
return [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $lede,
];
}

return NULL;
}

Expand Down
Loading