Skip to content

Commit

Permalink
VACMS-17018 Resolve VAMC menu assignment issues. (#16994)
Browse files Browse the repository at this point in the history
* Revert "VACMS-16959: enabled Work with us parent menu item (#16975)"

This reverts commit 0ef8e57.

This commit bypassed the root cause of the issue and hardcoded a bypass.

* VACMS-16959 Resolve duplicate validators.

* VACMS-16959 Prevent clientside validation from being too greedy with menu.

* VACMS-16959 Fix bug of empty being locked down.
  • Loading branch information
swirtSJW authored Jan 25, 2024
1 parent 376659b commit f8508b2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions docroot/modules/custom/va_gov_media/va_gov_media.module
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ function va_gov_media_clientside_validation_validator_info_alter(&$validators) {
foreach ($validators as &$validator) {
$validator['attachments']['library'][] = 'va_gov_media/cv.alt-text.validate';
}
unset($validator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,7 @@ protected function applyVamcMenuRulesForDetailPage(array &$form) {

$menu_element_type = $this->getMenuItemType($alias);
$menu_element_type = $menu_element_type ?? $this->checkForSeparator($allowed_separators, $menu_item);
if (str_contains($subject_uuid['option'], 'Work with us')) {
// This is a special case where we want to allow the menu item
// to be enabled.
$menu_element_type = self::ENABLED;
}

$this->addAllowedParent($allowed_parents, $enabled_count, $menu_element_type, $subject_uuid);
}
}
Expand Down Expand Up @@ -620,11 +616,11 @@ protected function preventUnintendedChangeOfParent(array &$form) {
$form['menu']['link']['menu_parent']['#value'] = $this->currentMenuParent;
}
// Check for rare possibility that menu parent is the default menu root.
elseif ($this->currentMenuParent === "pittsburgh-health-care:") {
elseif (empty($this->currentMenuParent) || $this->currentMenuParent === "pittsburgh-health-care:") {
return;
}
// This is not new so check the current parent exists in the reduced form.
elseif (!$current_menu_item_is_present || $this->isCurrentMenuSettingDisabled($form)) {
elseif (!$current_menu_item_is_present || $this->isCurrentMenuParentDisabledAndLocked($form)) {
// Parent does not exist in reduced form, Put the original parent options
// back to prevent data loss. The existing menu setting should not be
// allowed, but it exists, so allow it to persist. It may have been
Expand All @@ -637,18 +633,19 @@ protected function preventUnintendedChangeOfParent(array &$form) {
$title .= " ($can_not_change)";
$form['menu']['link']['menu_parent']['#title'] = $title;
}

}

/**
* Checks to see if the current menu parent is disabled.
* Checks to see if the current menu parent is disabled and children locked.
*
* @param array $form
* The form array.
*
* @return bool
* TRUE if the current menu parent is disabled. FALSE otherwise.
*/
protected function isCurrentMenuSettingDisabled(array $form) : bool {
protected function isCurrentMenuParentDisabledAndLocked(array $form) : bool {
$is_disabled = FALSE;
$current_menu_setting = $form['menu']['link']['menu_parent']['#options'][$this->currentMenuParent] ?? '';
if (strpos($current_menu_setting, 'Disabled no-link') !== FALSE) {
Expand All @@ -658,6 +655,27 @@ protected function isCurrentMenuSettingDisabled(array $form) : bool {
return $is_disabled;
}

/**
* Checks if the current menu parent is disabled but might allow children.
*
* @param array $form
* The form array.
* @param string $menu_parent
* The id of the menu parent currently selected at node load.
*
* @return bool
* TRUE if the current menu parent is disabled. FALSE otherwise.
*/
public static function isCurrentMenuParentDisabled(array $form, $menu_parent) : bool {
$is_disabled = FALSE;
$current_menu_setting = $form['menu']['link']['menu_parent']['#options'][$menu_parent] ?? '';
if (strpos($current_menu_setting, 'Disabled') !== FALSE) {
$is_disabled = TRUE;
}

return $is_disabled;
}

/**
* Set the ['vagov_menu_access']['content_type'] on drupal settings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\va_gov_menu_access\Service\MenuReductionService;

/**
* Implements hook_page_attachments().
Expand Down Expand Up @@ -55,7 +56,26 @@ function _va_gov_menu_access_hide_menu_fields(array &$form) {
*/
function _va_gov_menu_access_set_menu_parent_requirement(array $form, FormStateInterface $form_state) {
if ($form_state->getValue(['menu', 'enabled'])) {
$form['menu']['link']['menu_parent']['#required'] = TRUE;
$menu_parent = $form_state->getValue([
'menu',
'menu_parent',
]);
if (!empty($menu_parent) && MenuReductionService::isCurrentMenuParentDisabled($form, $menu_parent)) {
// The clientside_validation_jquery module's validation is too aggressive
// with the menu parent, and considers any option that is disabled as
// empty, even though there is a value in the option list.
// Since the parent field contains a disabled parent, we have to turn off
// clientside_validation for that field.
$form['menu']['link']['menu_parent']['#attributes']['novalidate'] = 'novalidate';
$form['menu']['link']['menu_parent']['#attributes']['formnovalidate'] = 'formnovalidate';
// Those should have been enough to make clientside_validation happy,
// but we need this too. No worries, there is drupal validation to prevent
// a save with no parent from succeeding.
$form['menu']['link']['menu_parent']['#required'] = FALSE;
}
else {
$form['menu']['link']['menu_parent']['#required'] = TRUE;
}
$form['#validate'][] = '_va_gov_menu_access_parent_menu_selected_validation';
}
return $form;
Expand Down

0 comments on commit f8508b2

Please sign in to comment.