Skip to content

Commit

Permalink
Merge pull request #374 from City-of-Helsinki/UHF-8525
Browse files Browse the repository at this point in the history
UHF-8525: Flush external caches when global announcement is modified
  • Loading branch information
tuutti authored Sep 18, 2023
2 parents 2b5c04c + 6c88b99 commit 97ffcb4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
20 changes: 13 additions & 7 deletions conf/cmi/core.entity_form_display.node.announcement.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mode: default
content:
body:
type: text_textarea_with_summary
weight: 15
weight: 16
region: content
settings:
rows: 9
Expand All @@ -43,14 +43,14 @@ content:
third_party_settings: { }
field_announcement_all_pages:
type: boolean_checkbox
weight: 11
weight: 13
region: content
settings:
display_label: true
third_party_settings: { }
field_announcement_content_pages:
type: select2_entity_reference
weight: 12
weight: 15
region: content
settings:
width: 100%
Expand All @@ -60,7 +60,7 @@ content:
third_party_settings: { }
field_announcement_link:
type: link_default
weight: 16
weight: 17
region: content
settings:
placeholder_url: ''
Expand All @@ -78,7 +78,7 @@ content:
third_party_settings: { }
field_announcement_type:
type: select2
weight: 10
weight: 11
region: content
settings:
width: 100%
Expand All @@ -93,6 +93,13 @@ content:
match_operator: CONTAINS
match_limit: 20
third_party_settings: { }
field_publish_externally:
type: boolean_checkbox
weight: 14
region: content
settings:
display_label: true
third_party_settings: { }
langcode:
type: language_select
weight: 1
Expand Down Expand Up @@ -124,7 +131,7 @@ content:
settings: { }
third_party_settings: { }
simple_sitemap:
weight: 10
weight: 12
region: content
settings: { }
third_party_settings: { }
Expand Down Expand Up @@ -165,7 +172,6 @@ content:
settings: { }
third_party_settings: { }
hidden:
field_publish_externally: true
hide_sidebar_navigation: true
promote: true
sticky: true
Expand Down
57 changes: 56 additions & 1 deletion public/modules/custom/helfi_etusivu/helfi_etusivu.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

declare(strict_types = 1);

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
Expand Down Expand Up @@ -178,14 +180,67 @@ function helfi_etusivu_preprocess_toolbar(&$variables): void {
/**
* Implements hook_block_alter().
*/
function helfi_etusivu_block_alter(&$definitions) {
function helfi_etusivu_block_alter(&$definitions) : void {
foreach ($definitions as $id => $definition) {
if ($id === 'local_tasks_block') {
$definitions[$id]['class'] = 'Drupal\helfi_etusivu\Plugin\Block\EtusivuLocalTasksBlock';
}
}
}

/**
* Invalidate external caches.
*
* This is used to automatically flush caches on all external sites when
* an 'announcement' node marked as 'publish externally' is created/modified
* or deleted.
*
* @todo Decouple this from node save. Establishing connection to Azure PubSub
* service seems to take pretty long time
*
* @see https://helsinkisolutionoffice.atlassian.net/browse/UHF-8533
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to flush cache tags for.
*/
function helfi_etusivu_invalidate_external_caches(EntityInterface $entity) : void {
if (!$entity instanceof ContentEntityInterface) {
return;
}
// Only flush caches when we're saving announcement node with
// 'field_publish_externally' option checked.
$isAnnouncementNode = $entity->getEntityTypeId() === 'node' && $entity->bundle() === 'announcement';
$publishExternally = $entity->hasField('field_publish_externally') && (bool) $entity->get('field_publish_externally')->value === TRUE;

if (!$isAnnouncementNode || !$publishExternally) {
return;
}
/** @var \Drupal\helfi_api_base\Cache\CacheTagInvalidator $service */
$service = \Drupal::service('helfi_api_base.cache_tag_invalidator');
$service->invalidateTags(['helfi_external_entity_announcement']);
}

/**
* Implements hook_entity_update().
*/
function helfi_etusivu_entity_update(EntityInterface $entity) : void {
helfi_etusivu_invalidate_external_caches($entity);
}

/**
* Implements hook_entity_delete().
*/
function helfi_etusivu_entity_delete(EntityInterface $entity) : void {
helfi_etusivu_invalidate_external_caches($entity);
}

/**
* Implements hook_entity_insert().
*/
function helfi_etusivu_entity_insert(EntityInterface $entity) : void {
helfi_etusivu_invalidate_external_caches($entity);
}

/**
* Implements hook_helfi_hero_design_alter().
*/
Expand Down

0 comments on commit 97ffcb4

Please sign in to comment.