Skip to content
Snippets Groups Projects
Commit 011db886 authored by Liam Morland's avatar Liam Morland
Browse files

Coding standards

parent e3686a81
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\uw_cfg_common\CustomBlocks;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\preprocess_event_dispatcher\Event\BlockPreprocessEvent;
/**
* Class UwCblBase.
*/
class UwCblBase {
/**
* Check that we are on a layout builder add/edit form and that we are on
* a specified block.
*
* @param \Drupal\core_event_dispatcher\Event\Form\FormAlterEvent $event
* The event.
* @param string $block_name
* The human readable name of the block.
* @return boolean
*/
public function checkLayoutBuilder(FormAlterEvent $event, string $block_name): bool {
// Get the form from the event.
$form = &$event->getForm();
// Get the form_id from the form object.
$form_id = $form['#form_id'];
// Form ids to check for.
$form_ids = ['layout_builder_update_block', 'layout_builder_add_block'];
// If we are not in a layout builder, exit the function.
if (!in_array($form_id, $form_ids)) {
return FALSE;
}
// Check for correct block name.
if (isset($form['settings']['admin_label']['#plain_text']) && $form['settings']['admin_label']['#plain_text'] == $block_name) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Check if we have the correct block id to preprocess.
*
* @param Drupal\hook_event_dispatcher\Event\Preprocess\BlockPreprocessEvent $event
* The event.
* @param string $block_id
* The id of the block to check.
* @return boolean
*/
public function checkPreprocessBlock(BlockPreprocessEvent $event, string $block_id): bool {
// Get the variables from the event.
$variables = $event->getVariables();
// If the plugin does not much the block id, return negative.
if ($variables->get('derivative_plugin_id') !== $block_id) {
return FALSE;
}
return TRUE;
}
}
<?php
namespace Drupal\uw_cfg_common\CustomBlocks;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\preprocess_event_dispatcher\Event\BlockPreprocessEvent;
/**
* Class UwCblBase.
*/
class UwCblBase {
/**
* Check for layout builder and block.
*
* Check that we are on a layout builder add/edit form and that we are on
* a specified block.
*
* @param \Drupal\core_event_dispatcher\Event\Form\FormAlterEvent $event
* The event.
* @param string $block_name
* The human readable name of the block.
*
* @return bool
* TRUE if we are in a layout builder and in the specified block, FALSE
* otherwise.
*/
public function checkLayoutBuilder(FormAlterEvent $event, string $block_name): bool {
// Get the form from the event.
$form = &$event->getForm();
// Get the form_id from the form object.
$form_id = $form['#form_id'];
// Form ids to check for.
$form_ids = ['layout_builder_update_block', 'layout_builder_add_block'];
// If we are not in a layout builder, exit the function.
if (!in_array($form_id, $form_ids)) {
return FALSE;
}
// Check for correct block name.
if (isset($form['settings']['admin_label']['#plain_text']) && $form['settings']['admin_label']['#plain_text'] == $block_name) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Check if we have the correct block id to preprocess.
*
* @param Drupal\hook_event_dispatcher\Event\Preprocess\BlockPreprocessEvent $event
* The event.
* @param string $block_id
* The id of the block to check.
*
* @return bool
* TRUE if the plugin does matches the block ID, FALSE othrewise.
*/
public function checkPreprocessBlock(BlockPreprocessEvent $event, string $block_id): bool {
// Get the variables from the event.
$variables = $event->getVariables();
// If the plugin does not much the block id, return negative.
if ($variables->get('derivative_plugin_id') !== $block_id) {
return FALSE;
}
return TRUE;
}
}
<?php
/**
* @file
* Contains \Drupal\uw_cfg_common\Form\UwContactAccessForm.
*/
namespace Drupal\uw_cfg_common\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\PermissionHandlerInterface;
use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
/**
* Form class for the content access form.
*/
class UwContentAccessForm extends FormBase {
/**
......@@ -47,7 +40,8 @@ class UwContentAccessForm extends FormBase {
'data' => 'Functionality',
];
// Step through each of the uw roles and add to header as well as store role object.
// Step through each of the uw roles and add to header as well as store role
// object.
foreach ($uw_roles as $uw_role) {
// Add role to header of table for this form.
......@@ -117,11 +111,11 @@ class UwContentAccessForm extends FormBase {
// The submit button.
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save permissions'),
'#button_type' => 'primary',
);
];
return $form;
}
......@@ -140,8 +134,9 @@ class UwContentAccessForm extends FormBase {
// The array of uw roles.
$uw_roles = UwPermissions::uw_get_roles();
// Step through each of the submitted permissions to grant or revoke it for the role.
// Step through each of the permissions array to get the row name (role-permission).
// Step through each of the submitted permissions to grant or revoke it for
// the role. Step through each of the permissions array to get the row name
// (role-permission).
foreach ($submitted_permissions as $row_name => $submitted_permission) {
// Step through each of the role-permissions to revoke or grant.
......@@ -152,25 +147,28 @@ class UwContentAccessForm extends FormBase {
// 1st element is permission.
$perm_names = explode('-', $row_name);
// Get the list of permissions to revoke/grant from the uw_permissions array.
// Get the list of permissions to revoke/grant from the uw_permissions
// array.
$uw_perms = $uw_permissions[$perm_names[0]][$perm_names[1]][$uw_role_name];
// If the checkbox was selected on the form grant the permissions that is
// in the uw_permissions array for that role.
// If the checkbox was selected on the form grant the permissions that
// is in the uw_permissions array for that role.
if ($permission_value) {
// Step through each of the permissions for that role and grant the permission.
// Step through each of the permissions for that role and grant the
// permission.
foreach ($uw_perms as $uw_perm) {
// Grant the permission for the specified role.
$uw_roles[$uw_role_name]['object']->grantPermission($uw_perm);
}
}
// If the checkbox was not selected on the form revoke the permissions that is
// in the uw_permissions array for that role.
// If the checkbox was not selected on the form revoke the permissions
// that is in the uw_permissions array for that role.
else {
// Step through each of the permissions for that role and revoke the permission.
// Step through each of the permissions for that role and revoke the
// permission.
foreach ($uw_perms as $uw_perm) {
// Revoke the permission for the specified role.
......@@ -186,4 +184,5 @@ class UwContentAccessForm extends FormBase {
// Set the message that the permissions have been saved.
$this->messenger()->addStatus($this->t('The changes have been saved.'));
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\uw_cfg_common\UwPermissions;
use Drupal\user\Entity\Role;
/**
* Class UwPermissions.
......@@ -20,21 +21,21 @@ class UwPermissions {
$uw_roles['Site manager'] = [
'name' => 'Site manager',
'id' => 'uw_role_site_manager',
'object' => \Drupal\user\Entity\Role::load('uw_role_site_manager'),
'object' => Role::load('uw_role_site_manager'),
];
// UW content editor role.
$uw_roles['Content editor'] = [
'name' => 'Content editor',
'id' => 'uw_role_content_editor',
'object' => \Drupal\user\Entity\Role::load('uw_role_content_editor'),
'object' => Role::load('uw_role_content_editor'),
];
// UW content author role.
$uw_roles['Content author'] = [
'name' => 'Content author',
'id' => 'uw_role_content_author',
'object' => \Drupal\user\Entity\Role::load('uw_role_content_author'),
'object' => Role::load('uw_role_content_author'),
];
return $uw_roles;
......@@ -53,11 +54,11 @@ class UwPermissions {
// Blog permissions.
'Blog' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_blog'),
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_blog'),
'Create/edit tags' =>
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_blog_tags', ['create', 'edit']),
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_blog_tags', ['create', 'edit']),
'Delete tags' =>
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_blog_tags', ['delete']),
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_blog_tags', ['delete']),
],
// Event permissions.
......@@ -72,29 +73,29 @@ class UwPermissions {
// News permissions.
'News' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_news_item'),
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_news_item'),
'Create/edit tags' =>
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_news_tags', ['create', 'edit']),
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_news_tags', ['create', 'edit']),
'Delete tags' =>
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_news_tags', ['delete']),
UwPermissions::uw_build_role_permissions_list_taxonomy_term('uw_vocab_news_tags', ['delete']),
],
// Site footer permissions.
'Site footer' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_site_footer'),
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_site_footer'),
],
// Special alert permissions.
'Special alert' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_custom('administer special alert'),
UwPermissions::uw_build_role_permissions_list_custom('administer special alert'),
],
// Web page permissions.
'Web page' => [
'Use content type' =>
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_web_page'),
UwPermissions::uw_build_role_permissions_list_content_type('uw_ct_web_page'),
],
];
......@@ -106,6 +107,7 @@ class UwPermissions {
*
* @param string $ct_name
* The machine name of the content type.
*
* @return array
* An array of the uw permissions.
*/
......@@ -146,6 +148,7 @@ class UwPermissions {
*
* @param string $permission_name
* The machine name of the taxonomy term.
*
* @return array
* An array of the uw permissions.
*/
......@@ -170,7 +173,9 @@ class UwPermissions {
* @param string $tax_name
* The machine name of the taxonomy term.
* @param array $permission_types
* The list of permissions for the taxonomy term (create, edit and/or delete).
* The list of permissions for the taxonomy term (create, edit, and/or
* delete).
*
* @return array
* An array of the uw permissions.
*/
......@@ -213,7 +218,7 @@ class UwPermissions {
/**
* Grant/revoke UW permission to roles and save.
*
* @parm array $permissions_to_process
* @param array $permissions_to_process
* The array of permissions to be granted or revoked.
* @param string $type
* The type of permissions to be processed (grant/revoke).
......@@ -237,7 +242,7 @@ class UwPermissions {
// Step through each of the permissions that need to be granted/revoke
// and grant/revoke for the specified uw role.
foreach($uw_permissions[$feature][$permission][$role] as $uw_perm) {
foreach ($uw_permissions[$feature][$permission][$role] as $uw_perm) {
// If type is grant, grant the permission for the role.
if ($type == 'grant') {
......@@ -259,4 +264,5 @@ class UwPermissions {
// Save the permissions.
UwPermissions::uw_save_permissions($uw_roles);
}
}
\ No newline at end of file
}
<?php
/**
* @file
* Install, update and uninstall for Configuration of all common WCMS.
*/
use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
/**
* Implements hook_install().
*/
function uw_cfg_common_install() {
$permissions_to_process = [
'Blog' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
],
'Event' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
'Create/edit types' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete types' => [
'Site manager',
],
],
'News' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
],
'Site footer' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],
/*'Special alert' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],*/
'Web page' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],
];
UwPermissions::uw_grant_revoke_permissions($permissions_to_process, 'grant');
}
<?php
/**
* @file
* Install, update and uninstall for Configuration of all common WCMS.
*/
use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
/**
* Implements hook_install().
*/
function uw_cfg_common_install() {
$permissions_to_process = [
'Blog' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
],
'Event' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
'Create/edit types' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete types' => [
'Site manager',
],
],
'News' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
'Create/edit tags' => [
'Site manager',
'Content author',
'Content editor',
],
'Delete tags' => [
'Site manager',
],
],
'Site footer' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],
/*'Special alert' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],*/
'Web page' => [
'Use content type' => [
'Site manager',
'Content author',
'Content editor',
],
],
];
UwPermissions::uw_grant_revoke_permissions($permissions_to_process, 'grant');
}
<?php
/**
* Implements hook_form_FORM_ID_alter().
*
* Remove the None option from layout builder styles.
*/
function uw_cfg_common_form_layout_builder_configure_section_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Remove the None option from layout builder styles.
unset($form['layout_builder_style']['#empty_option']);
// Ensuring that the contained width is selected by default.
$form['layout_builder_style']['#default_value'] = $form['layout_builder_style']['#default_value'] ? $form['layout_builder_style']['#default_value'] : 'uw-contained-width';
}
<?php
/**
* @file
* Module file.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*
* Remove the None option from layout builder styles.
*/
function uw_cfg_common_form_layout_builder_configure_section_alter(&$form, FormStateInterface $form_state, $form_id) {
// Remove the None option from layout builder styles.
unset($form['layout_builder_style']['#empty_option']);
// Ensuring that the contained width is selected by default.
$form['layout_builder_style']['#default_value'] = $form['layout_builder_style']['#default_value'] ?: 'uw-contained-width';
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment