Skip to content

Commit

Permalink
Merge pull request #254 from localgovdrupal/1.x
Browse files Browse the repository at this point in the history
1.5.7 Release
  • Loading branch information
andybroomfield committed May 22, 2023
2 parents f68bc62 + 3077fe8 commit 7e88fc6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Entity/AlertBannerEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AlertBannerEntityType extends ConfigEntityBundleBase implements AlertBanne
public function postSave(EntityStorageInterface $storage, $update = TRUE) {

// Add fields and workflow when creating a new alert banner type.
if (!$update) {
if (!$update && !$this->isSyncing) {

$bundle = $this->id();
$config_directory = new FileStorage(__DIR__ . '/../../config/install');
Expand Down
19 changes: 16 additions & 3 deletions src/Plugin/Block/AlertBannerBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Block\BlockBase;

Expand Down Expand Up @@ -168,14 +169,26 @@ protected function getCurrentAlertBanners() {
$types = $this->mapTypesConfigToQuery();
$published_alert_banner_query = $this->entityTypeManager->getStorage('localgov_alert_banner')
->getQuery()
->condition('status', 1)
->sort('type_of_alert', 'DESC')
->sort('changed', 'DESC')
->condition('status', 1);

// Only order by type of alert if the field is present.
$alert_banner_has_type_of_alert = FieldStorageConfig::loadByName('localgov_alert_banner', 'type_of_alert');
if (!empty($alert_banner_has_type_of_alert)) {
$published_alert_banner_query->sort('type_of_alert', 'DESC');
}

// Continue alert banner query.
$published_alert_banner_query->sort('changed', 'DESC')
->accessCheck(TRUE);

// If types (bunldes) are selected, add filter condition.
if (!empty($types)) {
$published_alert_banner_query->condition('type', $types, 'IN');
}

// Execute alert banner query.
$published_alert_banners = $published_alert_banner_query->execute();

// Load alert banners and add all.
// Visibility check happens in build, so we get cache contexts on all.
foreach ($published_alert_banners as $alert_banner_id) {
Expand Down
55 changes: 55 additions & 0 deletions tests/src/Kernel/AlertBannerBlockOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,59 @@ public function testAlertBannerBlockOrder() {

}

/**
* Test alert banner block order without type of alert.
*/
public function testAlertBannerBlockOrderWithoutTypeOfAlert() {

// Delete type of alert field.
// This is so we are testing the case where :-
// - Alerts don't have a type, so are in date order.
// - Querying for current banners without the type field is possible.
$this->container
->get('entity_type.manager')
->getStorage('field_storage_config')
->load('localgov_alert_banner.type_of_alert')
->delete();

// Alert times.
$alert_times = [
(new DrupalDateTime('-4 hours'))->getTimestamp(),
(new DrupalDateTime('-2 hours'))->getTimestamp(),
(new DrupalDateTime('-3 hours'))->getTimestamp(),
(new DrupalDateTime('now'))->getTimestamp(),
];

// Set up alert banners.
foreach ($alert_times as $changed) {
$alert_entity = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => $this->randomMachineName(8),
'moderation_state' => 'published',
'changed' => $changed,
]);
$alert_entity->save();
$alert[] = $alert_entity->id();
}

// Create and render the block and get the alert banner IDs as an array.
$block_manager = $this->container->get('plugin.manager.block');
$config = [];
$plugin_block = $block_manager->createInstance('localgov_alert_banner_block', $config);
$render = $plugin_block->build();
foreach ($render as $render_value) {
$result[] = $render_value['#localgov_alert_banner']->id();
}

// Set expected order, which will be date changed order.
$expected = [
$alert[3],
$alert[1],
$alert[2],
$alert[0],
];
$this->assertEquals($expected, $result);
}

}

0 comments on commit 7e88fc6

Please sign in to comment.