Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHS-5904: Add new "log out" block to existing sites #1696

Merged
merged 9 commits into from
Dec 19, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
uuid: 7e424b1f-3692-41a3-a48a-dd1a450e0203
langcode: en
status: true
dependencies:
module:
- stanford_samlauth
theme:
- humsci_colorful
id: humsci_colorful_samlsunetidlogoutblock
theme: humsci_colorful
region: footer
weight: 0
provider: null
plugin: stanford_samlauth_logout_block
settings:
id: stanford_samlauth_logout_block
label: 'SAML SUNetID Logout Block'
label_display: '0'
provider: stanford_samlauth
link_text: 'SUNetID Logout'
visibility: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
uuid: 2f7f0d52-c463-411d-a2e2-656d185acd4d
langcode: en
status: true
dependencies:
module:
- stanford_samlauth
theme:
- humsci_traditional
id: humsci_traditional_samlsunetidlogoutblock
theme: humsci_traditional
region: footer
weight: 0
provider: null
plugin: stanford_samlauth_logout_block
settings:
id: stanford_samlauth_logout_block
label: 'SAML SUNetID Logout Block'
label_display: '0'
provider: stanford_samlauth
link_text: 'SUNetID Logout'
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* su_humsci_profile.install
*/

use Drupal\Core\Config\FileStorage;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Site\Settings;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\hs_entities\Entity\HsEntityType;
Expand Down Expand Up @@ -1096,3 +1098,55 @@ function su_humsci_profile_update_9718() {
// Uninstalls modules after removing configs to prevent updb errors.
\Drupal::service('module_installer')->uninstall(['hs_webform', 'webform_ui', 'webform']);
}

/**
* Add new logout block to existing sites.
*/
function su_humsci_profile_update_9719() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this a bit more, this update hook is not needed. We force import configs prefixed with block.block.humsci_colorful_ and block.block.humsci_traditional_. So any changes to those configs would be reverted during config import anyways.

ref: https://github.com/SU-HSDO/suhumsci/blob/develop/config/default/config_ignore.settings.yml#L10

$config_storage = new FileStorage(Settings::get('config_sync_directory'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use config.storage.sync service here:

Suggested change
$config_storage = new FileStorage(Settings::get('config_sync_directory'));
$config_storage = \Drupal::service('config.storage.sync');

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one, I did try that initially, but it fails:
> [error] The configuration <em class="placeholder">block.block.humsci_colorful_samlsunetidlogoutblock</em> was not found in the sync directory.
I found what worked here:

$config_directory = new FileStorage(Settings::get('config_sync_directory'));

My theory is that this is dealing with theme config? But not sure if that is correct. If we're set on using the service, I'll have to poke at that again next week.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird. We have both versions in the site, but in the past we've had kickbacks because SWS prefers to use the service. Let's keep the FileStorage() version for now and come back if needed after SWS review.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\Drupal::service('config.storage.sync') works fine for me. Using the service is preferred.

$entity_type_manager = \Drupal::service('entity_type.manager');

$config_names = [
'block.block.humsci_colorful_samlsunetidlogoutblock',
'block.block.humsci_traditional_samlsunetidlogoutblock',
];

foreach ($config_names as $config_name) {

// Load the configuration data from the sync directory.
$config_data = $config_storage->read($config_name);

if (!$config_data) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use $config_storage->exists('name')

throw new \Exception(t('The configuration %config_name was not found in the sync directory.', ['%config_name' => $config_name]));
}

// Get the block storage and possible existing config.
$block_storage = $entity_type_manager->getStorage('block');
$existing_config = $block_storage->load($config_data['id']);

if (!$existing_config) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it guaranteed there will not be existing config? These are new configs and blocks are prefixed if they are custom on sites.

// Change the logout link text to match what the existing login text is.
$login_blocks = $entity_type_manager->getStorage('block')->loadByProperties([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looping through blocks multiple times and doing the same thing to produce a "log out" text. Why not do that prior to creating the config because we don't need to do it multiple times. Just once for one block.
Or maybe you want to also add the theme property condition?

'plugin' => 'stanford_samlauth_login_block',
]);

if ($login_blocks) {
foreach ($login_blocks as $login_block) {
/** @var Drupal\block\Entity\BlockDrupal\block\Entity\Block $login_block */
$settings = $login_block->get('settings');

$log_in = $settings['link_text'];
$log_out = preg_replace('/in\b/', 'out', $log_in);
$log_out = preg_replace('/In\b/', 'Out', $log_out);
$log_out = str_replace(' to ', ' from ', $log_out);
$log_out = str_replace(' To ', ' From ', $log_out);
}
}

$config_data['settings']['link_text'] = $log_out;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$log_out may not be established. Set a default value above as a fallback.

$block_storage->create($config_data)->save();

\Drupal::logger('su_humsci_profile')->info('Block configuration %config_name has been imported.', ['%config_name' => $config_name]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,22 @@ function su_humsci_profile_form_user_register_form_alter(&$form, FormStateInterf
/**
* Implements hook_preprocess_HOOK().
*/
function su_humsci_profile_preprocess_block__stanford_samlauth(&$variables) {
$variables['content']['login']['#attributes']['class'] = [
function su_humsci_profile_preprocess_block(&$variables) {
$classes = [
'text-align-right',
'hs-secondary-button',
];

$base_plugin_id = $variables['base_plugin_id'];

switch ($base_plugin_id) {
case 'stanford_samlauth_login_block':
$variables['content']['login']['#attributes']['class'] = $classes;
break;

case 'stanford_samlauth_logout_block':
$variables['content']['logout']['#attributes']['class'] = $classes;
}
}

/**
Expand Down
Loading