From 5b8e91eeaf2281f43732374aa8883eacf5182d92 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 15 Apr 2024 15:32:51 -0300 Subject: [PATCH 1/9] Slap together minimal views integration. --- src/EmbargoIpRangeViewsData.php | 12 ++++++++++++ src/EmbargoViewsData.php | 12 ++++++++++++ src/Entity/Embargo.php | 1 + src/Entity/IpRange.php | 1 + 4 files changed, 26 insertions(+) create mode 100644 src/EmbargoIpRangeViewsData.php create mode 100644 src/EmbargoViewsData.php diff --git a/src/EmbargoIpRangeViewsData.php b/src/EmbargoIpRangeViewsData.php new file mode 100644 index 0000000..0816412 --- /dev/null +++ b/src/EmbargoIpRangeViewsData.php @@ -0,0 +1,12 @@ + Date: Mon, 15 Apr 2024 16:52:51 -0300 Subject: [PATCH 2/9] Base "reverse" integration. Could probably use a better name... --- embargo.views.inc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 embargo.views.inc diff --git a/embargo.views.inc b/embargo.views.inc new file mode 100644 index 0000000..2d9e5ea --- /dev/null +++ b/embargo.views.inc @@ -0,0 +1,24 @@ + \t('Embargoed Nodee'), + 'relationship' => [ + 'base' => 'embargo', + 'base field' => 'embargoed_node', + 'field' => 'nid', + 'id' => 'standard', + 'label' => \t('Embargoed Nodee'), + ], + ]; + +} From 66217cb2f57403a6fd1717411fff76064e3d1ebe Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 15 Apr 2024 17:04:57 -0300 Subject: [PATCH 3/9] Rename the altered-in relationship field. --- embargo.views.inc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/embargo.views.inc b/embargo.views.inc index 2d9e5ea..d103c82 100644 --- a/embargo.views.inc +++ b/embargo.views.inc @@ -10,14 +10,15 @@ */ function embargo_views_data_alter(array &$data) { - $data['node_field_data']['embargoed_node'] = [ - 'title' => \t('Embargoed Nodee'), + $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('Embargoed Nodee'), + 'label' => \t('Embargoes'), ], ]; From 7d9c78f46d766a2987edc164a38ccdb4030a30ec Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 16 Apr 2024 12:01:03 -0300 Subject: [PATCH 4/9] Fix rendering of embargo date. --- src/Plugin/Block/EmbargoNotificationBlock.php | 5 ++++- templates/embargo-notification.html.twig | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Plugin/Block/EmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoNotificationBlock.php index 7ad8d4d..c7835a0 100644 --- a/src/Plugin/Block/EmbargoNotificationBlock.php +++ b/src/Plugin/Block/EmbargoNotificationBlock.php @@ -151,7 +151,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, @@ -169,6 +171,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 @@ -202,7 +205,7 @@ public function getCacheTags() { */ public function getCacheContexts() { // Ensure that with every new node/route, this block will be rebuilt. - return Cache::mergeContexts(parent::getCacheContexts(), ['route']); + return Cache::mergeContexts(parent::getCacheContexts(), ['route', 'url']); } } diff --git a/templates/embargo-notification.html.twig b/templates/embargo-notification.html.twig index d1c7bb8..633852a 100644 --- a/templates/embargo-notification.html.twig +++ b/templates/embargo-notification.html.twig @@ -37,7 +37,7 @@ restricted indefinitely. {% 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 @@ -51,7 +51,7 @@ restricted indefinitely. {% 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 From cd08535b20100ec6be5c145f3e09e8b5c2d6525a Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 16 Apr 2024 14:00:08 -0300 Subject: [PATCH 5/9] Slight bit of reworking. Block construction and type-hinting. --- src/Plugin/Block/EmbargoNotificationBlock.php | 139 +++++++++--------- 1 file changed, 66 insertions(+), 73 deletions(-) diff --git a/src/Plugin/Block/EmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoNotificationBlock.php index c7835a0..e4f0080 100644 --- a/src/Plugin/Block/EmbargoNotificationBlock.php +++ b/src/Plugin/Block/EmbargoNotificationBlock.php @@ -5,7 +5,6 @@ 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; @@ -14,7 +13,7 @@ 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. @@ -32,114 +31,79 @@ class EmbargoNotificationBlock extends BlockBase implements ContainerFactoryPlug * * @var string */ - 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 */ - protected $user; + protected AccountProxyInterface $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 []; } @@ -187,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', 'url']); + 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; } } From 79c87de38200f75a868ac029b1348bd73e9263af Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 16 Apr 2024 14:16:51 -0300 Subject: [PATCH 6/9] Allow for the nullable addresses. --- src/Plugin/Block/EmbargoNotificationBlock.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin/Block/EmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoNotificationBlock.php index e4f0080..2cdfe0d 100644 --- a/src/Plugin/Block/EmbargoNotificationBlock.php +++ b/src/Plugin/Block/EmbargoNotificationBlock.php @@ -29,9 +29,9 @@ class EmbargoNotificationBlock extends BlockBase implements ContainerFactoryPlug /** * The admin email address. * - * @var string + * @var string|null */ - protected string $adminMail; + protected ?string $adminMail; /** * The notification message. From 80aa0a767f997cd41a0ff69a87c601fc315184c0 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 16 Apr 2024 14:19:06 -0300 Subject: [PATCH 7/9] Wider interface. --- src/Plugin/Block/EmbargoNotificationBlock.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Plugin/Block/EmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoNotificationBlock.php index 2cdfe0d..e4d3238 100644 --- a/src/Plugin/Block/EmbargoNotificationBlock.php +++ b/src/Plugin/Block/EmbargoNotificationBlock.php @@ -9,6 +9,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\ResettableStackedRouteMatchInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\embargo\EmbargoInterface; use Drupal\node\NodeInterface; @@ -57,9 +58,9 @@ class EmbargoNotificationBlock extends BlockBase implements ContainerFactoryPlug /** * The current user. * - * @var \Drupal\Core\Session\AccountProxyInterface + * @var \Drupal\Core\Session\AccountInterface */ - protected AccountProxyInterface $user; + protected AccountInterface $user; /** * The object renderer. From 383b510547d4c8d906ff1bf7aa632df33b563be9 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 16 Apr 2024 14:24:51 -0300 Subject: [PATCH 8/9] Remove unused use. --- src/Plugin/Block/EmbargoNotificationBlock.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/Block/EmbargoNotificationBlock.php b/src/Plugin/Block/EmbargoNotificationBlock.php index e4d3238..10e5eb1 100644 --- a/src/Plugin/Block/EmbargoNotificationBlock.php +++ b/src/Plugin/Block/EmbargoNotificationBlock.php @@ -10,7 +10,6 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\ResettableStackedRouteMatchInterface; use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Session\AccountProxyInterface; use Drupal\embargo\EmbargoInterface; use Drupal\node\NodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; From 7ab191b09418cba1f6a7a33cda65006b24fbb0db Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 16 Apr 2024 15:57:59 -0300 Subject: [PATCH 9/9] Fix up user relationship. --- embargo.views.inc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/embargo.views.inc b/embargo.views.inc index d103c82..5128bc5 100644 --- a/embargo.views.inc +++ b/embargo.views.inc @@ -21,5 +21,19 @@ function embargo_views_data_alter(array &$data) { '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'), + ], + ]; }