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

ISTWCMS-4229: Protect home page

parent 73d8e962
No related branches found
No related tags found
1 merge request!63ISTWCMS-4229: Protect home page
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\uw_cfg_common\Service\UWService;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
......@@ -276,3 +277,81 @@ function uw_cfg_common_form_node_type_add_form_alter(&$form, FormStateInterface
$form['submission']['preview_mode']['#default_value'] = 0;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Node edit form: node/NID/edit.
*
* Prevent certain changes to the home page.
*/
function uw_cfg_common_form_node_uw_ct_web_page_edit_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// No changes for those with access.
if (\Drupal::currentUser()->hasPermission('bypass home page protection')) {
return;
}
// Do not allow the home page to be parent of any item.
unset($form['menu']['link']['menu_parent']['#options']['main:uw_base_profile.front_page']);
// Early return if not editing home page.
$nid = (int) \Drupal::routeMatch()->getRawParameter('node');
if (!UWService::nodeIsHomePage($nid)) {
return;
}
// Remove access to certain controls.
$form['path']['#access'] = FALSE;
$form['promote']['#access'] = FALSE;
$form['sticky']['#access'] = FALSE;
// For 'menu', setting #access did not work for non-admins. So, also hide the
// sub-components and make it a container so that nothing appears on the page.
$form['menu']['#access'] = FALSE;
$form['menu']['#type'] = 'container';
$form['menu']['enabled']['#access'] = FALSE;
$form['menu']['link']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Menu edit form: admin/structure/menu/manage/main.
*
* Prevent certain changes to the home page.
*/
function uw_cfg_common_form_menu_edit_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// No changes for those with access.
if (\Drupal::currentUser()->hasPermission('bypass home page protection')) {
return;
}
// Return early if not editing "Main navigation" menu.
if (!isset($form['links']['links']['menu_plugin_id:uw_base_profile.front_page'])) {
return;
}
// Remove access to home page controls.
$form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['enabled']['#access'] = FALSE;
$form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['operations']['#access'] = FALSE;
$form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['weight']['#access'] = FALSE;
// Make home page not draggable.
$key = array_search('draggable', $form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['#attributes']['class'], TRUE);
unset($form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['#attributes']['class'][$key]);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Menu link edit form: admin/structure/menu/link/LINK/edit.
*
* Do not allow the home page to be parent of any item.
*/
function uw_cfg_common_form_menu_link_edit_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// No changes for those with access.
if (\Drupal::currentUser()->hasPermission('bypass home page protection')) {
return;
}
// Do not allow the home page to be parent of any item.
unset($form['menu_parent']['#options']['main:uw_base_profile.front_page']);
}
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