Skip to content

Commit

Permalink
VACMS-14793: Adds Menu Alter trait and module for controlling header …
Browse files Browse the repository at this point in the history
…and footer menus.

Co-authored-by: Daniel Sasser <[email protected]>
Co-authored-by: Christia Troyer <[email protected]>
  • Loading branch information
chri5tia and dsasser committed Sep 15, 2023
1 parent e3be787 commit 70e5bbc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ module:
va_gov_github: 0
va_gov_govdelivery: 0
va_gov_graphql: 0
va_gov_header_footer: 0
va_gov_help_center: 0
va_gov_links: 0
va_gov_live_field_migration: 0
Expand Down
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);
}
}

}
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;
}

}
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
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 }
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public function formAlter(FormAlterEvent $event): void {
$admin = $this->permsService->hasAdminRole(TRUE);
$this->hubMenuFormAlter($form, $admin);
}
if ($event->getFormId() === 'menu_link_content_va-gov-footer_form' || $event->getFormId() === 'menu_link_content_footer-bottom-rail_form') {
$this->hideMenuLinkDescriptionField($form);
}
}

/**
Expand Down Expand Up @@ -125,16 +122,6 @@ public function hubMenuHideParentLink(array &$form, bool $admin): static {
return $this;
}

/**
* 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;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 70e5bbc

Please sign in to comment.