-
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 new menu form alter trait.
- Adds new va_gov_header_footer module to maintain customizations to the footer/header menus.
- Loading branch information
Showing
6 changed files
with
176 additions
and
86 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
68 changes: 68 additions & 0 deletions
68
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,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); | ||
} | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
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,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; | ||
} | ||
|
||
} |
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