Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new hook_localgov_publications_menu_tree_alter() hook. #119

Merged
merged 8 commits into from
Jan 19, 2024

Conversation

rupertj
Copy link
Member

@rupertj rupertj commented Dec 4, 2023

No description provided.

@finnlewis finnlewis mentioned this pull request Dec 7, 2023
2 tasks
@rupertj
Copy link
Member Author

rupertj commented Jan 6, 2024

For a quick example of what this is for, we use this on H&F to show a different shorter title in the menu block to the node title.

EG: https://www.lbhf.gov.uk/publications/corporate-plan/hf-plan-2023-2026

"About" in the menu has the node title "About this plan".
"Our borough" in the menu has the node title "Our borough at a glance".
etc...

To do this, we use a field that we've added to most content types called field_lbhf_teaser_title, which is used anywhere we need a shorter title.

The code to swop it over that uses the hook in this PR looks like this:

/**
 * Implements hook_localgov_publications_menu_tree_alter().
 *
 * Sets the menu link title for publication pages to the teaser title, if set.
 */
function lbhf_core_localgov_publications_menu_tree_alter(&$tree) {
  foreach ($tree as $item) {
    if (!empty($item['below'])) {
      lbhf_core_localgov_publications_menu_tree_alter($item['below']);
    }

    if (!isset($item['link']['nid'])) {
      continue;
    }

    $node = Node::load($item['link']['nid']);

    if (!$node instanceof NodeInterface) {
      continue;
    }

    if (!$node->hasField('field_lbhf_teaser_title')) {
      continue;
    }

    $teaserTitle = $node->get('field_lbhf_teaser_title')->value;
    if ($teaserTitle) {
      $item['link']['title'] = $teaserTitle;
    }
  }
}

@finnlewis
Copy link
Member

Looks like a useful thing, but I don't think I've ever written a hook.

Flagging for @ekes, @stephen-cox or @andybroomfield to take a look for awareness as much as anything.

@rupertj
Copy link
Member Author

rupertj commented Jan 19, 2024

@andybroomfield Implementing this hook in a theme works for me now. Care to re-review it? :)

Copy link

@andybroomfield andybroomfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working for me also, thanks.

@rupertj rupertj merged commit 02b4d61 into 1.x Jan 19, 2024
8 checks passed
@rupertj rupertj deleted the feature/1.x/alter-menu-links branch January 19, 2024 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants