Skip to content

Commit

Permalink
fix(YALB-1685): Error when going to "Manage Settings" on certain pages (
Browse files Browse the repository at this point in the history
#525) (#542)

* fix(YALB-1685): update bundle to always be main

In this bug, we finally found the issue to be that these nodes are in a
different bundle, missing the CTA field all together.  If they were
main, it would display as it should as they are defined.  Since we do
not have control over which field definitions get loaded, and it seems
to use the bundle to load them, this updates any that have been
mis-labelled to main so that nodes could be edited again.

@co-authored-by Berger, Marc <@codechefmarc>

* fix(YALB-1685): do not directly access the database

While the previous implementation worked, it felt strange directly
accessing the database.  This goes through Drupal objects to accomplish
the same result, which feels a bit better in case there is any hooks or
background processing that Drupal does in these cases.

* revert(YALB-1685): extra menu items on node sidebar

This reverts the extra menu items on the node sidebar, which is causing
the error due to the bundle being one such that the field definitions do
not contain the CTA mega menu top level title field.  It also reverts
the possible fix to update the bundle to main on cache refresh.

Now, the only way to add this field data is to do so via the Manage Main
Menu view, which works as intended.

* fix(YALB-1667): find parent page title for basic menu

This sets up component library twig to be able to retrieve the node
title later if criteria is met that makes it use node_title.

This attempts to not touch the mega menu logic, hence the new variable.

See component-library-twig/components/02-molecules/menu/_yds-menu-item.twig
  • Loading branch information
dblanken-yale authored Jan 22, 2024
1 parent 8d5a300 commit eebfad2
Showing 1 changed file with 12 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
*/

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\menu_item_extras\Entity\MenuItemExtrasMenuLinkContent;

/**
* @file
Expand Down Expand Up @@ -240,9 +238,20 @@ function ys_core_preprocess_menu(&$variables) {
// @see component-library-twig/components/02-molecules/menu/_yds-menu-item.twig
$clonedMenuItem['list__item__is_heading'] = TRUE;

// Get the CTA text from menu item extras field.
// Get the CTA text from menu item extras field. (for mega menu)
$clonedMenuItem['heading_cta'] = $clonedMenuItem['entity']->get('field_menu_top_level_link_cta')->value ?: t('Explore @title', ['@title' => $clonedMenuItem['title']]);

// Default the node_title to the menu title.
$clonedMenuItem['node_title'] = $clonedMenuItem['title'];

// If the menu item is associated with a node, replace the node_title
// with the node's title.
if ($menuItem['url']->isRouted()) {
$nodeId = $menuItem['url']->getRouteParameters()['node'];
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nodeId);
$clonedMenuItem['node_title'] = $node->getTitle();
}

// Add cloned item to the beginning of the menu.
array_unshift($menuItem['below'], $clonedMenuItem);

Expand Down Expand Up @@ -397,98 +406,6 @@ function ys_core_taxonomy_term_update() {
Cache::invalidateTags(['rendered']);
}

/**
* The following functions add menu item extras fields to node add/edit forms.
*
* @see https://www.drupal.org/project/menu_item_extras/issues/2992096#comment-14140361
*/

/**
* Implements hook_form_FORM_ID_alter().
*/
function ys_core_form_node_page_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
ys_core_page_form_alter($form, $form_state);
}

/**
* Implements hook_form_FORM_ID_alter().
*/
function ys_core_form_node_page_form_alter(&$form, FormStateInterface $form_state, $form_id) {
ys_core_page_form_alter($form, $form_state);
}

/**
* Alters the page node forms.
*
* @var array $form
* The form array.
* @var Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*/
function ys_core_page_form_alter(&$form, FormStateInterface $form_state) {

// Add menu link fields to node form.
if ($link = _ys_core_get_link($form_state)) {
$form_display = EntityFormDisplay::load('menu_link_content.' . $link->getMenuName() . '.default');
assert($form_display instanceof EntityFormDisplay);
$form['menu']['link']['extra'] = [
'#type' => 'container',
'#parents' => ['menu', 'extra'],
];
$form_display->buildForm($link, $form['menu']['link']['extra'], $form_state);
// Only keep custom fields, other properties already are in the form.
foreach (Element::children($form['menu']['link']['extra']) as $key) {
if (strpos($key, 'field_') !== 0) {
unset($form['menu']['link']['extra'][$key]);
}
}

foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'ys_core_save_menu_link_fields';
}
}
}
}

/**
* Saves the menu item extras when on the node add/edit pages.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function ys_core_save_menu_link_fields(array $form, FormStateInterface $form_state) {
if ($link = _ys_core_get_link($form_state)) {
// Only save the menu item extras if there is a menu link with a route.
if ($link->getUrlObject()->getRouteName()) {
$form_display = EntityFormDisplay::load('menu_link_content.' . $link->getMenuName() . '.default');
if ($form_display instanceof EntityFormDisplay) {
$form_display->extractFormValues($link, $form['menu']['link']['extra'], $form_state);
$link->save();
}
}
}
}

/**
* Gets any menu item extras content.
*
* @return Drupal\menu_item_extras\Entity\MenuItemExtrasMenuLinkContent
* Menu item extras content.
*/
function _ys_core_get_link(FormStateInterface $form_state) {
/** @var Drupal\node\Entity $form_state */
$node = $form_state->getFormObject()->getEntity();
$defaults = menu_ui_get_menu_link_defaults($node);
if ($mlid = $defaults['entity_id']) {
return MenuItemExtrasMenuLinkContent::load($mlid);
}
return MenuItemExtrasMenuLinkContent::create($defaults);
}

/**
* End allow menu item extras fields to be included on node add and edit forms.
*/

/**
* Implements hook_preprocess_page().
*/
Expand Down

0 comments on commit eebfad2

Please sign in to comment.