Skip to content

Commit

Permalink
VACMS-14793: Adds new menu form alter trait.
Browse files Browse the repository at this point in the history
- Adds new va_gov_header_footer module to maintain customizations to the footer/header menus.
  • Loading branch information
dsasser authored and chri5tia committed Sep 15, 2023
1 parent 8106e9d commit 6853785
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 86 deletions.
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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,68 @@
<?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();
if (in_array($formId, $this->menus)) {
$this->hideMenuLinkDescriptionField($form);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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;
}

/**
* Hides the parent link form field for home page hub list link form.
*
* @param array $form
* The form element array.
* @param bool $admin
* TRUE if current user is an administrator.
*
* @return $this
*/
public function hubMenuHideParentLink(array &$form, bool $admin): static {
if (!empty($form['menu_parent'])) {
$form['menu_parent']['#access'] = $admin;
}
return $this;
}

/**
* Hides the view_mode form field for home page hub list link form.
*
* @param array $form
* The form element array.
* @param bool $admin
* TRUE if current user is an administrator.
*
* @return $this
*/
public function hubMenuHideViewMode(array &$form, bool $admin): static {
if (!empty($form['view_mode'])) {
$form['view_mode']['#access'] = $admin;
}
return $this;
}

/**
* Hides the expanded form field for home page hub list link form.
*
* @param array $form
* The form element array.
*
* @return $this
*/
public function hubMenuHideExpanded(array &$form): static {
if (!empty($form['expanded'])) {
$form['expanded']['#access'] = FALSE;
}
return $this;
}

/**
* Hides the attributes form field for home page hub list link form.
*
* @param array $form
* The form element array.
*
* @return $this
*/
public function hubMenuHideAddtributes(array &$form): static {
if (!empty($form['options']['attributes'])) {
$form['options']['attributes']['#access'] = FALSE;
}
return $this;
}

}
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 @@ -4,6 +4,7 @@

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;

Expand All @@ -12,6 +13,8 @@
*/
class FormEventSubscriber implements EventSubscriberInterface {

use MenuFormAlter;

/**
* The VA user permission service.
*
Expand All @@ -29,6 +32,15 @@ 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.
*
Expand All @@ -41,9 +53,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 All @@ -61,87 +70,4 @@ public function hubMenuFormAlter(array &$form, bool $admin): void {
->hubMenuHideParentLink($form, $admin);
}

/**
* Hides the attributes form field for home page hub list link form.
*
* @param array $form
* The form element array.
*
* @return $this
*/
public function hubMenuHideAddtributes(array &$form): static {
if (!empty($form['options']['attributes'])) {
$form['options']['attributes']['#access'] = FALSE;
}
return $this;
}

/**
* Hides the expanded form field for home page hub list link form.
*
* @param array $form
* The form element array.
*
* @return $this
*/
public function hubMenuHideExpanded(array &$form): static {
if (!empty($form['expanded'])) {
$form['expanded']['#access'] = FALSE;
}
return $this;
}

/**
* Hides the view_mode form field for home page hub list link form.
*
* @param array $form
* The form element array.
* @param bool $admin
* TRUE if current user is an administrator.
*
* @return $this
*/
public function hubMenuHideViewMode(array &$form, bool $admin): static {
if (!empty($form['view_mode'])) {
$form['view_mode']['#access'] = $admin;
}
return $this;
}

/**
* Hides the parent link form field for home page hub list link form.
*
* @param array $form
* The form element array.
* @param bool $admin
* TRUE if current user is an administrator.
*
* @return $this
*/
public function hubMenuHideParentLink(array &$form, bool $admin): static {
if (!empty($form['menu_parent'])) {
$form['menu_parent']['#access'] = $admin;
}
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}
*/
public static function getSubscribedEvents(): array {
return [
FormHookEvents::FORM_ALTER => ['formAlter'],
];
}

}

0 comments on commit 6853785

Please sign in to comment.