diff --git a/embargo.views.inc b/embargo.views.inc new file mode 100644 index 0000000..5128bc5 --- /dev/null +++ b/embargo.views.inc @@ -0,0 +1,39 @@ + \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'), + ], + ]; + +} 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 @@ +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 []; } @@ -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, @@ -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 @@ -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; } } 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