-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VACMS-14793: Adds Menu Alter trait and module for controlling header …
…and footer menus. Co-authored-by: Daniel Sasser <[email protected]> Co-authored-by: Christia Troyer <[email protected]>
- Loading branch information
Showing
6 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
docroot/modules/custom/va_gov_header_footer/src/EventSubscriber/FormEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_header_footer\EventSubscriber; | ||
|
||
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent; | ||
use Drupal\core_event_dispatcher\FormHookEvents; | ||
use Drupal\va_gov_header_footer\Traits\MenuFormAlter; | ||
use Drupal\va_gov_user\Service\UserPermsService; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Va gov header footer event subscriber. | ||
*/ | ||
class FormEventSubscriber implements EventSubscriberInterface { | ||
|
||
use MenuFormAlter; | ||
|
||
/** | ||
* The VA user permission service. | ||
* | ||
* @var \Drupal\va_gov_user\Service\UserPermsService | ||
*/ | ||
protected UserPermsService $permsService; | ||
|
||
/** | ||
* List of menu form Ids to alter. | ||
* | ||
* @var array | ||
*/ | ||
private array $menus = [ | ||
'menu_link_content_va-gov-footer_form', | ||
'menu_link_content_footer-bottom-rail_form', | ||
]; | ||
|
||
/** | ||
* Constructs event subscriber. | ||
* | ||
* @param \Drupal\va_gov_user\Service\UserPermsService $permsService | ||
* The messenger. | ||
*/ | ||
public function __construct(UserPermsService $permsService) { | ||
$this->permsService = $permsService; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): array { | ||
return [ | ||
FormHookEvents::FORM_ALTER => ['formAlter'], | ||
]; | ||
} | ||
|
||
/** | ||
* Form alters for va_gov_home. | ||
* | ||
* @param \Drupal\core_event_dispatcher\Event\Form\FormAlterEvent $event | ||
* The form event. | ||
*/ | ||
public function formAlter(FormAlterEvent $event): void { | ||
$form = &$event->getForm(); | ||
$formId = $event->getFormId(); | ||
$admin = $this->permsService->hasAdminRole(TRUE); | ||
if (in_array($formId, $this->menus) && !$admin) { | ||
$this->hideMenuLinkDescriptionField($form); | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
docroot/modules/custom/va_gov_header_footer/src/Traits/MenuFormAlter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_header_footer\Traits; | ||
|
||
/** | ||
* Provides centralized menu form alter methods. | ||
*/ | ||
trait MenuFormAlter { | ||
|
||
/** | ||
* Hides the description field on certain menu link forms. | ||
* | ||
* @param array $form | ||
* The form element array. | ||
*/ | ||
public function hideMenuLinkDescriptionField(array &$form): void { | ||
$form['description']['#access'] = FALSE; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
docroot/modules/custom/va_gov_header_footer/va_gov_header_footer.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: VA.gov Menus | ||
type: module | ||
description: Provides custom logic for menus. | ||
package: Custom | ||
core_version_requirement: ^9 || ^10 |
6 changes: 6 additions & 0 deletions
6
docroot/modules/custom/va_gov_header_footer/va_gov_header_footer.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
va_gov_header_footer.event_subscriber: | ||
class: Drupal\va_gov_header_footer\EventSubscriber\FormEventSubscriber | ||
arguments: ['@va_gov_user.user_perms'] | ||
tags: | ||
- { name: event_subscriber } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters