Skip to content

Commit

Permalink
Conditional alert banner placement (#142)
Browse files Browse the repository at this point in the history
* Added Condition Field module (#137)

* Added visibility field (#137)

* Patch condition_field for missing schema: https://www.drupal.org/project/condition_field/issues/3215202

* Copy configuration for visibility field to new alert banner types (#137 #108)

* Only use the request_path plugin for controlling banner visibility.

Includes update path for field_condition schema. See: https://www.drupal.org/project/condition_field/issues/3215202#comment-14117352

* Added update hook to add visibility field to existing alert banners

* Add visibility field to all existing localgov_alert_banner bundles

Co-authored-by: Andy Broomfield <andy.broomfield@brighton-hove.gov.uk>
  • Loading branch information
stephen-cox and andybroomfield committed Jun 9, 2021
1 parent ee7fafe commit 2b829cf
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 27 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ jobs:
echo "GIT_BASE=${GITHUB_BASE_REF}" >> $GITHUB_ENV
echo "GIT_BRANCH=${GITHUB_HEAD_REF}" >> $GITHUB_ENV
echo "GIT_REPO=${{ github.event.pull_request.head.repo.full_name }}" >> $GITHUB_ENV
- name: Set composer branch reference for main version branches
if: endsWith(github.ref, '.x')
run: echo "COMPOSER_REF=${GIT_BRANCH}-dev" >> $GITHUB_ENV

- name: Set composer branch reference for main version branches
if: endsWith(github.ref, '.x') == false
run: echo "COMPOSER_REF=dev-${GIT_BRANCH}" >> $GITHUB_ENV
Expand Down Expand Up @@ -188,9 +188,13 @@ jobs:
run: docker-compose -f docker-compose.yml up -d

- name: Run PHPUnit tests
env:
LOCALGOV_VERSION: ${{ matrix.localgov-version }}
RANDOMIZE_TESTS: ""
run: |
[ "${LOCALGOV_VERSION}" != "1.x" ] && RANDOMIZE_TESTS="--order-by=random"
mkdir -p ./html/web/sites/simpletest && chmod 777 ./html/web/sites/simpletest
sed -i "s#http://localgov.lndo.site#http://drupal#; s#mysql://database:database@database/database#sqlite://localhost//dev/shm/test.sqlite#" ./html/phpunit.xml.dist
docker exec -t drupal bash -c 'chown docker:docker -R /var/www/html'
# docker exec -u docker -t drupal bash -c "cd /var/www/html && ./bin/phpunit --testdox"
docker exec -u docker -t drupal bash -c "cd /var/www/html && ./bin/paratest --processes=4"
docker exec -u docker -t drupal bash -c "cd /var/www/html && ./bin/paratest --processes=4 ${RANDOMIZE_TESTS}"
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
"homepage": "https://github.com/localgovdrupal/localgov_alert_banner",
"minimum-stability": "dev",
"require": {
"drupal/condition_field": "^2.0",
"drupal/field_group": "~3.1"
},
"extra": {
"enable-patching": true,
"composer-exit-on-patch-failure": true,
"patchLevel": {
"drupal/core": "-p2"
},
"patches": {
"drupal/condition_field": {
"Fix schema #3215202": "https://www.drupal.org/files/issues/2021-05-28/configuration-schema-fix-3215202-6.patch"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ langcode: en
status: true
dependencies:
config:
- field.field.localgov_alert_banner.localgov_alert_banner.visibility
- field.field.localgov_alert_banner.localgov_alert_banner.link
- field.field.localgov_alert_banner.localgov_alert_banner.short_description
- field.field.localgov_alert_banner.localgov_alert_banner.type_of_alert
Expand All @@ -10,6 +11,7 @@ dependencies:
module:
- localgov_alert_banner
module:
- condition_field
- link
- text
id: localgov_alert_banner.localgov_alert_banner.default
Expand All @@ -24,6 +26,12 @@ content:
settings:
display_label: true
third_party_settings: { }
visibility:
weight: 5
settings: { }
third_party_settings: { }
type: condition_field_default
region: content
link:
weight: 4
settings:
Expand All @@ -32,6 +40,13 @@ content:
third_party_settings: { }
type: link_default
region: content
remove_hide_link:
type: boolean_checkbox
weight: 6
region: content
settings:
display_label: true
third_party_settings: { }
short_description:
weight: 3
settings:
Expand All @@ -40,19 +55,6 @@ content:
third_party_settings: { }
type: text_textarea
region: content
type_of_alert:
type: options_select
weight: 0
region: content
settings: { }
third_party_settings: { }
remove_hide_link:
type: boolean_checkbox
weight: 6
region: content
settings:
display_label: true
third_party_settings: { }
title:
type: string_textfield
weight: 1
Expand All @@ -61,9 +63,15 @@ content:
size: 60
placeholder: ''
third_party_settings: { }
type_of_alert:
type: options_select
weight: 0
region: content
settings: { }
third_party_settings: { }
uid:
type: entity_reference_autocomplete
weight: 5
weight: 7
settings:
match_operator: CONTAINS
size: 60
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ langcode: en
status: true
dependencies:
config:
- field.field.localgov_alert_banner.localgov_alert_banner.visibility
- field.field.localgov_alert_banner.localgov_alert_banner.link
- field.field.localgov_alert_banner.localgov_alert_banner.short_description
- field.field.localgov_alert_banner.localgov_alert_banner.type_of_alert
Expand All @@ -10,13 +11,21 @@ dependencies:
module:
- localgov_alert_banner
module:
- condition_field
- link
- text
id: localgov_alert_banner.localgov_alert_banner.default
targetEntityType: localgov_alert_banner
bundle: localgov_alert_banner
mode: default
content:
visibility:
weight: 3
label: above
settings: { }
third_party_settings: { }
type: condition_field_string
region: content
link:
weight: 2
label: hidden
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
langcode: en
status: true
dependencies:
config:
- field.storage.localgov_alert_banner.visibility
- localgov_alert_banner.localgov_alert_banner_type.localgov_alert_banner
module:
- condition_field
id: localgov_alert_banner.localgov_alert_banner.visibility
field_name: visibility
entity_type: localgov_alert_banner
bundle: localgov_alert_banner
label: Visibility
description: 'Set the visibility of the alert to only show it on specific conditions.'
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
enabled_plugins:
'entity_bundle:node': false
request_path: true
user_role: false
field_type: condition_field
18 changes: 18 additions & 0 deletions config/install/field.storage.localgov_alert_banner.visibility.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
langcode: en
status: true
dependencies:
module:
- condition_field
- localgov_alert_banner
id: localgov_alert_banner.visibility
field_name: visibility
entity_type: localgov_alert_banner
type: condition_field
settings: { }
module: condition_field
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
1 change: 1 addition & 0 deletions localgov_alert_banner.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ dependencies:
- drupal:options
- drupal:user
- drupal:views
- condition_field:condition_field
47 changes: 47 additions & 0 deletions localgov_alert_banner.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* LocalGov Alert Banner install file.
*/

use Drupal\Core\Config\FileStorage;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
* Update alert banner entity definition to include the token on the entity.
Expand All @@ -19,3 +22,47 @@ function localgov_alert_banner_update_8801() {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('token', 'localgov_alert_banner', 'localgov_alert_banner', $field_storage_definition);
}

/**
* Add visibility field to existing alert banners.
*/
function localgov_alert_banner_update_8901() {

// Enable dependent condition_field module.
\Drupal::service('module_installer')->install(['condition_field']);

$config_directory = new FileStorage(__DIR__ . '/config/install');

// Add visibility field storage.
$field_storage = $config_directory->read('field.storage.localgov_alert_banner.visibility');
if ($field_storage && !FieldStorageConfig::loadByName('localgov_alert_banner', 'visibility')) {
FieldStorageConfig::create($field_storage)->save();
}

// Fetch all configured localgov_alert_banner bundles.
$alert_banner_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('localgov_alert_banner');
foreach ($alert_banner_bundles as $bundle => $info) {

// Add visibility field to bundle.
$field_record = $config_directory->read('field.field.localgov_alert_banner.localgov_alert_banner.visibility');
if ($field_record && !FieldConfig::loadByName('localgov_alert_banner', $bundle, 'visibility')) {
$field_record['bundle'] = $bundle;
FieldConfig::create($field_record)->save();
}

// Add visibility field to the bundles entity form.
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('localgov_alert_banner.' . $bundle . '.default');
if ($form_display) {
$form_display->setComponent('visibility', [
'region' => 'content',
'settings' => [],
'third_party_settings' => [],
'type' => 'condition_field_default',
'weight' => 5,
]);
$form_display->save();
}
}
}
72 changes: 72 additions & 0 deletions src/Entity/AlertBannerEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\localgov_alert_banner\Entity;

use Drupal\condition_field\ConditionAccessResolver;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EditorialContentEntityBase;
Expand Down Expand Up @@ -84,6 +86,13 @@ class AlertBannerEntity extends EditorialContentEntityBase implements AlertBanne
use EntityChangedTrait;
use EntityPublishedTrait;

/**
* Drupal\Core\Condition\ConditionManager definition.
*
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $pluginManagerCondition;

/**
* {@inheritdoc}
*/
Expand All @@ -94,6 +103,14 @@ public static function preCreate(EntityStorageInterface $storage_controller, arr
];
}

/**
* {@inheritdoc}
*/
public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = []) {
parent::__construct($values, $entity_type, $bundle, $translations);
$this->pluginManagerCondition = \Drupal::service('plugin.manager.condition');
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -224,6 +241,39 @@ public function setOwner(UserInterface $account) {
return $this;
}

/**
* Is the alert banner visible?
*
* @return bool
* True if the alert banner is visible, otherwise FALSE.
*/
public function isVisible() {

// Check if the field is present and has a value.
if (!$this->hasField('visibility') || $this->get('visibility')->isEmpty()) {
return TRUE;
}

// Visibility condition config.
$conditions_config = $this->get('visibility')->getValue()[0]['conditions'];

// Construct visibility conditions.
$conditions = [];
foreach ($conditions_config as $condition_id => $values) {
/** @var \Drupal\Core\Condition\ConditionInterface $condition */
$condition = $this->pluginManagerCondition->createInstance($condition_id, $values);
$conditions[] = $condition;
}

// Check if visibility conditions met.
if (ConditionAccessResolver::checkAccess($conditions, 'or')) {
return TRUE;
}
else {
return FALSE;
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -337,4 +387,26 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}

/**
* {@inheritdoc}
*/
public function getCacheContexts() {

// Add cache contexts depending on the enabled visibility condition plugins.
if ($this->hasField('visibility') && !$this->get('visibility')->isEmpty()) {
$contexts = [];
$conditions_config = $this->get('visibility')->getValue()[0]['conditions'];

foreach ($conditions_config as $condition_id => $values) {
/** @var \Drupal\Core\Condition\ConditionInterface $condition */
$condition = $this->pluginManagerCondition->createInstance($condition_id, $values);
$contexts = Cache::mergeContexts($contexts, $condition->getCacheContexts());
}

$this->addCacheContexts($contexts);
}

return parent::getCacheContexts();
}

}

0 comments on commit 2b829cf

Please sign in to comment.