Skip to content

Commit

Permalink
Merge branch 'main' into fix/caching
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey authored May 27, 2024
2 parents d8c5b3b + 817c825 commit ec7eac6
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 78 deletions.
39 changes: 39 additions & 0 deletions embargo.views.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @file
* Views hook implementations.
*/

/**
* Implements hook_views_data_alter().
*/
function embargo_views_data_alter(array &$data) {

$data['node_field_data']['embargo__embargoes'] = [
'title' => \t('Embargoes'),
'help' => \t('Embargoes applicable to the given node.'),
'relationship' => [
'base' => 'embargo',
'base field' => 'embargoed_node',
'field' => 'nid',
'id' => 'standard',
'label' => \t('Embargoes'),
],
];
$data['users_field_data']['embargo__exempt_users'] = [
'title' => \t('Embargo exemptions'),
'help' => \t('Embargoes for which the given user is specifically exempt.'),
'relationship' => [
'id' => 'entity_reverse',
'field_name' => 'embargo__exempt_users',
'entity_type' => 'embargo',
'field table' => 'embargo__exempt_users',
'field field' => 'exempt_users_target_id',
'base' => 'embargo',
'base field' => 'id',
'label' => \t('Embargo exemptions'),
],
];

}
12 changes: 12 additions & 0 deletions src/EmbargoIpRangeViewsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Drupal\embargo;

use Drupal\views\EntityViewsData;

/**
* Embargo IP range entity views data generation.
*/
class EmbargoIpRangeViewsData extends EntityViewsData {

}
12 changes: 12 additions & 0 deletions src/EmbargoViewsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Drupal\embargo;

use Drupal\views\EntityViewsData;

/**
* Embargo entity views data generation.
*/
class EmbargoViewsData extends EntityViewsData {

}
1 change: 1 addition & 0 deletions src/Entity/Embargo.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* handlers = {
* "storage" = "Drupal\embargo\EmbargoStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "views_data" = "Drupal\embargo\EmbargoViewsData",
* "list_builder" = "Drupal\embargo\EmbargoListBuilder",
* "form" = {
* "add" = "Drupal\embargo\Form\EmbargoForm",
Expand Down
1 change: 1 addition & 0 deletions src/Entity/IpRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* handlers = {
* "storage" = "Drupal\embargo\IpRangeStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "views_data" = "Drupal\embargo\EmbargoIpRangeViewsData",
* "list_builder" = "Drupal\embargo\EmbargoListBuilder",
* "form" = {
* "add" = "Drupal\embargo\Form\IpRangeForm",
Expand Down
148 changes: 72 additions & 76 deletions src/Plugin/Block/EmbargoNotificationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\ResettableStackedRouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\embargo\EmbargoInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;

/**
* Provides a "Embargo Notifications" block.
Expand All @@ -30,116 +29,81 @@ class EmbargoNotificationBlock extends BlockBase implements ContainerFactoryPlug
/**
* The admin email address.
*
* @var string
* @var string|null
*/
protected $adminMail;
protected ?string $adminMail;

/**
* The notification message.
*
* @var string
*/
protected $notificationMessage;
protected string $notificationMessage;

/**
* A route matching interface.
*
* @var \Drupal\Core\Routing\ResettableStackedRouteMatchInterface
*/
protected $routeMatch;
protected ResettableStackedRouteMatchInterface $routeMatch;

/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;

/**
* Embargo entity storage.
*
* @var \Drupal\embargo\EmbargoStorageInterface
*/
protected $storage;
protected Request $request;

/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
* @var \Drupal\Core\Session\AccountInterface
*/
protected $user;
protected AccountInterface $user;

/**
* The object renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
protected RendererInterface $renderer;

/**
* Construct embargo notification block.
*
* @param array $configuration
* Block configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Routing\ResettableStackedRouteMatchInterface $route_match
* A route matching interface.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request being made to check access against.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* A configuration factory interface.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* An entity type manager.
* @param \Drupal\Core\Session\AccountProxyInterface $user
* The current user.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The object renderer.
* Drupal's entity type manager service.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ResettableStackedRouteMatchInterface $route_match, RequestStack $request_stack, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $user, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$settings = $config_factory->get('embargo.settings');
$this->adminMail = $settings->get('contact_email');
$this->notificationMessage = $settings->get('notification_message');
$this->storage = $entity_type_manager->getStorage('embargo');
$this->routeMatch = $route_match;
$this->request = $request_stack->getCurrentRequest();
$this->user = $user;
$this->renderer = $renderer;
}
protected EntityTypeManagerInterface $entityTypeManager;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_route_match'),
$container->get('request_stack'),
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('current_user'),
$container->get('renderer'),
);
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
$instance = new static($configuration, $plugin_id, $plugin_definition);

$settings = $container->get('config.factory')->get('embargo.settings');
$instance->adminMail = $settings->get('contact_email');
$instance->notificationMessage = $settings->get('notification_message');
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->routeMatch = $container->get('current_route_match');
$instance->request = $container->get('request_stack')->getCurrentRequest();
$instance->user = $container->get('current_user');
$instance->renderer = $container->get('renderer');

return $instance;
}

/**
* {@inheritdoc}
*/
public function build() {
$node = $this->routeMatch->getParameter('node');
if (!($node instanceof NodeInterface)) {
public function build() : array {
if (!($node = $this->getNode())) {
return [];
}
// Displays even if the embargo is exempt in the current context.
$applicable_embargoes = $this->storage->getApplicableEmbargoes($node);
/** @var \Drupal\embargo\EmbargoStorageInterface $storage */
$storage = $this->entityTypeManager->getStorage('embargo');
$applicable_embargoes = $storage->getApplicableEmbargoes($node);
if (empty($applicable_embargoes)) {
return [];
}
Expand All @@ -151,7 +115,9 @@ public function build() {
$expired = $embargo->expiresBefore($now);
$exempt_user = $embargo->isUserExempt($this->user);
$exempt_ip = $embargo->ipIsExempt($ip);

$embargoes[$id] = [
'actual' => $embargo,
'indefinite' => $embargo->getExpirationType() === EmbargoInterface::EXPIRATION_TYPE_INDEFINITE,
'expired' => $expired,
'exempt_user' => $exempt_user,
Expand All @@ -169,6 +135,7 @@ public function build() {
'additional_emails' => $embargo->additional_emails->view('default'),
];
}

$build = [
'#theme' => 'embargo_notification',
'#message' => $this->t($this->notificationMessage, ['@contact' => $this->adminMail]), // phpcs:ignore
Expand All @@ -184,25 +151,54 @@ public function build() {
/**
* {@inheritdoc}
*/
public function getCacheTags() {
public function getCacheTags() : array {
$tags = parent::getCacheTags();

// When the given node changes (route), the block should rebuild.
if ($node = $this->routeMatch->getParameter('node')) {
return Cache::mergeTags(
parent::getCacheTags(),
if ($node = $this->getNode()) {
$tags = Cache::mergeTags(
$tags,
$node->getCacheTags(),
);
}

// Return default tags, if not on a node page.
return parent::getCacheTags();
return $tags;
}

/**
* {@inheritdoc}
*/
public function getCacheContexts() {
// Ensure that with every new node/route, this block will be rebuilt.
return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
public function getCacheContexts() : array {
$contexts = Cache::mergeContexts(
parent::getCacheContexts(),
// Ensure that with every new node/route, this block will be rebuilt.
[
'route',
'url',
],
);

if ($node = $this->getNode()) {
$contexts = Cache::mergeContexts(
$contexts,
$node->getCacheContexts(),
);
}

return $contexts;
}

/**
* Helper; get the active node.
*
* @return \Drupal\node\NodeInterface|null
* Get the active node.
*/
protected function getNode() : ?NodeInterface {
$node_candidate = $this->routeMatch->getParameter('node');
return $node_candidate instanceof NodeInterface ?
$node_candidate :
NULL;
}

}
4 changes: 2 additions & 2 deletions templates/embargo-notification.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
restricted <strong>indefinitely</strong>.
{% endtrans %}
{% else %}
{% set expiry_date = embargo.expiration_date.getPhpDateTime|date('Y-m-d') %}
{% set expiry_date = embargo.actual.expiration_date.date.getPhpDateTime|date('Y-m-d') %}
{% trans %}
Access to all associated files of this resource is
restricted until
Expand All @@ -51,7 +51,7 @@
restricted <strong>indefinitely</strong>.
{% endtrans %}
{% else %}
{% set expiry_date = embargo.expiration_date.getPhpDateTime|date('Y-m-d') %}
{% set expiry_date = embargo.actual.expiration_date.date.getPhpDateTime|date('Y-m-d') %}
{% trans %}
Access to this resource and all associated files is
restricted until
Expand Down

0 comments on commit ec7eac6

Please sign in to comment.