Skip to content

Commit b1fa72a

Browse files
authored
Merge pull request #1325 from UN-OCHA/berliner/HPC-10021
HPC-10021: Show display form if articles have already been added, mark protected articles in selection preview, don't show protected articles publicly
2 parents 773bacc + 8088270 commit b1fa72a

File tree

6 files changed

+60
-13
lines changed

6 files changed

+60
-13
lines changed

html/modules/custom/ghi_blocks/src/Plugin/Block/GHIBlockBase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ public function blockForm($form, FormStateInterface $form_state) {
894894
]));
895895
$this->configuration['is_preview'] = TRUE;
896896
$build = $this->build();
897-
$form['container']['preview'] = [
897+
$form['container']['preview'] = $build ? [
898898
'#theme' => 'block',
899899
'#attributes' => [
900900
'data-block-preview' => $this->getPluginId(),
@@ -908,7 +908,7 @@ public function blockForm($form, FormStateInterface $form_state) {
908908
'library' => ['ghi_blocks/block.preview'],
909909
],
910910
'content' => $build,
911-
];
911+
] : NULL;
912912
}
913913

914914
return $form;

html/modules/custom/ghi_content/src/Entity/ContentBase.php

+21
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public function access($operation = 'view', ?AccountInterface $account = NULL, $
5454
return parent::access($operation, $account, $return_as_object);
5555
}
5656

57+
/**
58+
* Check if the entity is currently protected.
59+
*
60+
* @return bool
61+
* TRUE if the entity is currently protected, FALSE otherwise.
62+
*/
63+
public function isProtected() {
64+
return $this->getEmbargoedAccessManager()?->isProtected($this) ?? FALSE;
65+
}
66+
5767
/**
5868
* {@inheritdoc}
5969
*/
@@ -477,6 +487,17 @@ protected static function getDateFormatter() {
477487
return \Drupal::service('date.formatter');
478488
}
479489

490+
/**
491+
* Get the embargoed access manager service.
492+
*
493+
* @return \Drupal\ghi_embargoed_access\EmbargoedAccessManager|null
494+
* The embargoed access manager service or NULL.
495+
*/
496+
protected static function getEmbargoedAccessManager() {
497+
$service = 'ghi_embargoed_access.manager';
498+
return \Drupal::hasService($service) ? \Drupal::service($service) : NULL;
499+
}
500+
480501
/**
481502
* Check if the node has been manually unpublished.
482503
*

html/modules/custom/ghi_content/src/Plugin/Block/RelatedArticles.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ public function buildContent() {
4949
$conf = $this->getBlockConfig();
5050
$options = [];
5151

52-
$articles = $this->getArticles();
53-
if (empty($articles)) {
54-
// Nothing to show.
55-
return [];
56-
}
57-
52+
$articles = $this->getArticles(TRUE);
5853
$display = $conf['display']['select'];
5954

6055
// If articles have been selected, use only those, otherwise use all.
@@ -68,6 +63,11 @@ public function buildContent() {
6863
}, $display['order']));
6964
}
7065

66+
if (empty($articles)) {
67+
// Nothing to show.
68+
return NULL;
69+
}
70+
7171
// Build the render array.
7272
$build = [
7373
'#theme' => 'related_articles_cards',
@@ -104,7 +104,7 @@ protected function getConfigurationDefaults() {
104104
* {@inheritdoc}
105105
*/
106106
public function getDefaultSubform($is_new = FALSE) {
107-
return 'articles';
107+
return empty($this->getArticles()) ? 'articles' : 'display';
108108
}
109109

110110
/**
@@ -154,10 +154,10 @@ public function displayForm(array $form, FormStateInterface $form_state) {
154154
/**
155155
* Get the articles that this block should display.
156156
*
157-
* @return \Drupal\Core\Entity\EntityInterface[]|null
157+
* @return \Drupal\ghi_content\Entity\Article[]
158158
* An array of entity objects indexed by their ids.
159159
*/
160-
private function getArticles() {
160+
private function getArticles($check_access = FALSE) {
161161
$conf = $this->getBlockConfig();
162162
$entity_keys = $conf['articles']['article_select']['entity_ids'] ?? [];
163163
/** @var \Drupal\ghi_content\Entity\Article[] $articles */
@@ -182,6 +182,11 @@ private function getArticles() {
182182
}
183183
}
184184
}
185+
if ($check_access) {
186+
$articles = array_filter($articles, function (Article $article) {
187+
return $article->access() && !$article->isProtected();
188+
});
189+
}
185190
return $articles;
186191
}
187192

html/modules/custom/ghi_embargoed_access/src/EmbargoedAccessManager.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,14 @@ public function alterNode(&$variables) {
263263
unset($variables['label']);
264264
unset($variables['metadata']);
265265
unset($variables['content']['field_image']);
266+
unset($variables['content']['field_summary']);
266267
$variables['display_submitted'] = FALSE;
267-
$variables['attributes']['class'][] = 'content-width';
268+
269+
if ($variables['view_mode'] == 'password_protected') {
270+
$variables['attributes']['class'][] = 'content-width';
271+
}
272+
273+
$variables['attributes']['class'][] = 'protected';
268274

269275
$cacheableMetadata = new CacheableMetadata();
270276
$cacheableMetadata->addCacheContexts([EntityIsProtectedCacheContext::CONTEXT_ID . ':' . $entity->getEntityTypeId() . '||' . $entity->id()]);

html/themes/custom/common_design_subtheme/css/styles.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

html/themes/custom/common_design_subtheme/sass/ghi/content/_article-grid.scss

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ article.node--view-mode-grid {
44
height: 100%;
55
border: 1px solid var(--cd-default-border-color);
66

7+
#layout-builder-modal &.protected::before {
8+
position: absolute;
9+
top: -3px;
10+
left: -3px;
11+
width: calc(100% + 6px);
12+
content: 'Protected';
13+
text-align: center;
14+
color: white;
15+
border-radius: inherit;
16+
border-bottom-right-radius: 0;
17+
border-bottom-left-radius: 0;
18+
background-color: var(--cd-contrast-red);
19+
font-size: 0.8rem;
20+
}
21+
722
.cd-teaser__image {
823
max-width: unset;
924
margin-top: 0;

0 commit comments

Comments
 (0)