Skip to content

Commit

Permalink
Merge pull request #118 from localgovdrupal/master
Browse files Browse the repository at this point in the history
PR for alpha5 release
  • Loading branch information
andybroomfield committed Apr 16, 2021
2 parents ad268c3 + 91957c0 commit 90ba096
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 27 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# LocalGovDrupal Alert banner

LocalGovDrupal Alert banner module, adds a global alert banner block and entity.

## Order of alerts
In terms of order, it should be Notable Death -> Major -> Minor -> Announcement and then in date updated order.
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ display:
path: admin/content/alert-banner
menu:
type: normal
title: 'Alert Banners'
title: 'Alert banners'
description: ''
expanded: false
parent: system.admin_content
Expand Down
9 changes: 9 additions & 0 deletions config/schema/localgov_alert_banner_block.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
block.settings.localgov_alert_banner_block:
type: block_settings
label: 'LocalGov alert banner block settings'
mapping:
include_types:
type: sequence
label: 'Displayed alert types'
sequence:
type: string
2 changes: 1 addition & 1 deletion localgov_alert_banner.install
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Drupal\Core\Field\BaseFieldDefinition;

/**
* Update alert banner entity defination to include the token on the entity.
* Update alert banner entity definition to include the token on the entity.
*/
function localgov_alert_banner_update_8801() {
$field_storage_definition = BaseFieldDefinition::create('string')
Expand Down
14 changes: 10 additions & 4 deletions localgov_alert_banner.links.task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ entity.localgov_alert_banner.version_history:
title: 'Revisions'

entity.localgov_alert_banner.status_form:
route_name: entity.localgov_alert_banner.status_form
base_route: entity.localgov_alert_banner.canonical
route_name: entity.localgov_alert_banner.status_form
base_route: entity.localgov_alert_banner.canonical
title: 'Status'
weight: 10
class: '\Drupal\localgov_alert_banner\Plugin\Menu\LocalTask\StatusFormTab'

entity.localgov_alert_banner.delete_form:
route_name: entity.localgov_alert_banner.delete_form
base_route: entity.localgov_alert_banner.canonical
route_name: entity.localgov_alert_banner.delete_form
base_route: entity.localgov_alert_banner.canonical
title: Delete
weight: 10

entity.localgov_alert_banner.collection:
route_name: entity.localgov_alert_banner.collection
base_route: system.admin_content
title: 'Alert banners'
weight: 50
38 changes: 26 additions & 12 deletions src/Form/AlertBannerEntityForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class AlertBannerEntityForm extends ContentEntityForm {
*/
protected $request;

/**
* Theme manager service.
*
* @var Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;

/**
* {@inheritdoc}
*/
Expand All @@ -52,6 +59,7 @@ public static function create(ContainerInterface $container) {
$instance->moduleHandler = $container->get('module_handler');
$instance->configFactory = $container->get('config.factory');
$instance->request = $container->get('request_stack');
$instance->themeManager = $container->get('theme.manager');
return $instance;
}

Expand All @@ -74,8 +82,24 @@ public function buildForm(array $form, FormStateInterface $form_state) {

// Create the vertical tabs like on node edit forms.
// @src https://drupal.stackexchange.com/a/276907
$form['#theme'][] = 'node_edit_form';
$form['#attached']['library'] = ['node/drupal.node'];
// Only do this if the theme is not gin! Issue #116.
// @todo Opt alert banner into gin admin theming.
if ($this->themeManager->getActiveTheme()->getName() != 'gin') {
$form['#theme'][] = 'node_edit_form';
$form['#attached']['library'] = ['node/drupal.node'];

// Support Elbow room module if it's installed.
if ($this->moduleHandler->moduleExists('elbow_room')) {
$form['#attached']['library'][] = 'elbow_room/base';
$elbowRoomConfig = $this->configFactory()->get('elbow_room.settings');
$form['#attached']['drupalSettings']['elbow_room']['default'] = $elbowRoomConfig->get('default');

// Add node form classes for elbow room to function.
$form['#attributes']['class'][] = 'node-form';
}
}

// Create the advanced field group.
$form['advanced'] = [
'#type' => 'container',
'#weight' => 99,
Expand All @@ -84,16 +108,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
],
];

// Support Elbow room module if it's installed.
if ($this->moduleHandler->moduleExists('elbow_room')) {
$form['#attached']['library'][] = 'elbow_room/base';
$elbowRoomConfig = $this->configFactory()->get('elbow_room.settings');
$form['#attached']['drupalSettings']['elbow_room']['default'] = $elbowRoomConfig->get('default');

// Add node form classes for elbow room to function.
$form['#attributes']['class'][] = 'node-form';
}

// Alert details group.
$form['alert_details'] = [
'#type' => 'fieldset',
Expand Down
64 changes: 60 additions & 4 deletions src/Plugin/Block/AlertBannerBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\localgov_alert_banner\Plugin\Block;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
Expand Down Expand Up @@ -65,6 +66,45 @@ public static function create(ContainerInterface $container, array $configuratio
);
}

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'include_types' => [],
];
}

/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) : array {
$form = parent::blockForm($form, $form_state);
$type_storage = $this->entityTypeManager->getStorage('localgov_alert_banner_type');
$config = $this->getConfiguration();
$config_options = [];
foreach ($type_storage->loadMultiple() as $id => $type) {
$config_options[$id] = $type->label();
}
$form['include_types'] = [
'#type' => 'checkboxes',
'#options' => $config_options,
'#title' => $this->t('Display types'),
'#description' => $this->t('If no types are selected all will be displayed.'),
'#default_value' => !empty($config['include_types']) ? $config['include_types'] : [],
];
return $form;
}

/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
parent::blockSubmit($form, $form_state);
$values = $form_state->getValues();
$this->configuration['include_types'] = $values['include_types'];
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -99,13 +139,29 @@ public function build() {
* Array with the IDs of any published alert banners.
*/
protected function getCurrentAlertBanners() {
$published_alert_banner = $this->entityTypeManager->getStorage('localgov_alert_banner')
$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')
->execute();
return $published_alert_banner;
->sort('changed', 'DESC');
if (!empty($types)) {
$published_alert_banner_query->condition('type', $types, 'IN');
}
return $published_alert_banner_query->execute();
}

/**
* Get an array of types from config we can use for querying.
*
* @return array
* An array of alter banner type IDs as keys and values.
*/
protected function mapTypesConfigToQuery() : array {
$include_types = $this->configuration['include_types'];
return array_filter($include_types, function ($t) {
return (bool) $t;
});
}

}
102 changes: 97 additions & 5 deletions tests/src/Functional/AlertBannerBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\Tests\localgov_alert_banner\Functional;

use Drupal\block\Entity\Block;
use Drupal\Tests\BrowserTestBase;

/**
Expand Down Expand Up @@ -32,21 +33,44 @@ class AlertBannerBlockTest extends BrowserTestBase {
protected $adminUser;

/**
* {@inheritdoc}
* Place the alert block.
*
* Utility to place the alert banner block so we can assert
* against its output.
*
* @param array $settings
* The block settings.
*
* @return \Drupal\block\Entity\Block
* The placed block.
*/
protected function setUp() {
parent::setUp();

protected function placeAlterBlock(array $settings = []) {
$this->adminUser = $this->drupalCreateUser(['administer blocks']);
$this->drupalLogin($this->adminUser);
$this->drupalPlaceBlock('localgov_alert_banner_block');
$block = $this->drupalPlaceBlock('localgov_alert_banner_block', $settings);
$this->drupalLogout($this->adminUser);
return $block;
}

/**
* Update the settings of an existing block.
*
* @param \Drupal\block\Entity\Block $block
* The block to update.
* @param array $newSettings
* The new settings to merge with the existing settings.
*/
protected function updateBlockSettings(Block $block, array $newSettings) {
$settings = $block->get('settings');
$block->set('settings', $newSettings + $settings);
$block->save();
}

/**
* Test alert banner block displays.
*/
public function testAlertBannerDisplays() {
$this->placeAlterBlock();
// Set up an alert banner.
$title = $this->randomMachineName(8);
$alert_message = 'Alert message: ' . $this->randomMachineName(16);
Expand All @@ -70,6 +94,7 @@ public function testAlertBannerDisplays() {
* Test non live alert banner does not display.
*/
public function testNonLiveAlertBannerDoesNotDisplay() {
$this->placeAlterBlock();
// Set up an alert banner.
$title = $this->randomMachineName(8);
$alert_message = 'Alert message: ' . $this->randomMachineName(16);
Expand All @@ -93,6 +118,7 @@ public function testNonLiveAlertBannerDoesNotDisplay() {
* Test display title option.
*/
public function testAlertDisplayTitle() {
$this->placeAlterBlock();
$title = $this->randomMachineName(8);
$alert_message = 'Alert message: ' . $this->randomMachineName(16);
$alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
Expand Down Expand Up @@ -121,6 +147,7 @@ public function testAlertDisplayTitle() {
* Test remove hide link option.
*/
public function testAlertRemoveHideLink() {
$this->placeAlterBlock();
$title = $this->randomMachineName(8);
$alert_message = 'Alert message: ' . $this->randomMachineName(16);
$alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
Expand All @@ -146,4 +173,69 @@ public function testAlertRemoveHideLink() {
$this->assertSession()->responseNotContains('js-alert-banner-close');
}

/**
* Test types display option.
*/
public function testAlertBannerTypeDisplay() {
$alterType = $this->container->get('entity_type.manager')
->getStorage('localgov_alert_banner_type')
->create(['id' => 'extra_type', 'label' => 'Extra type']);
$alterType->save();

$alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => 'Alert of type localgov_alert_banner',
'display_title' => 1,
'status' => TRUE,
]);
$alert->save();

$alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'extra_type',
'title' => 'Alert of type extra_type',
'display_title' => 1,
'status' => TRUE,
]);
$alert->save();

// No types selected should result in all types being displayed.
$block = $this->placeAlterBlock();
$this->drupalGet('<front>');
$this->assertSession()->pageTextContains('Alert of type localgov_alert_banner');
$this->assertSession()->pageTextContains('Alert of type extra_type');

$this->updateBlockSettings($block, [
'include_types' => [
'localgov_alert_banner' => 'localgov_alert_banner',
'extra_type' => 'extra_type',
],
]);
$this->drupalGet('<front>');
$this->assertSession()->pageTextContains('Alert of type localgov_alert_banner');
$this->assertSession()->pageTextContains('Alert of type extra_type');

$this->updateBlockSettings($block, [
'include_types' => [
'localgov_alert_banner' => 'localgov_alert_banner',
'extra_type' => '0',
],
]);
$this->drupalGet('<front>');
$this->assertSession()->pageTextContains('Alert of type localgov_alert_banner');
$this->assertSession()->pageTextNotContains('Alert of type extra_type');

$this->updateBlockSettings($block, [
'include_types' => [
'localgov_alert_banner' => '0',
'extra_type' => 'extra_type',
],
]);
$this->drupalGet('<front>');
$this->assertSession()->pageTextNotContains('Alert of type localgov_alert_banner');
$this->assertSession()->pageTextContains('Alert of type extra_type');

}

}
5 changes: 5 additions & 0 deletions tests/src/Functional/PermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
class PermissionsTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'classy';

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 90ba096

Please sign in to comment.