Skip to content

Instantly share code, notes, and snippets.

@NickStees
Created August 25, 2020 15:26
Show Gist options
  • Save NickStees/5d8dd94eb0847616538cc0de5776b0d4 to your computer and use it in GitHub Desktop.
Save NickStees/5d8dd94eb0847616538cc0de5776b0d4 to your computer and use it in GitHub Desktop.
Drupal module to pass menu to Vue.js
<?php
/**
* Passing the module settings off to Javascript
*/
function vue_header_init(){
// Menus to pass to Vue
$menuNames = [
'deptMenu' => 'menu-department-home-pages',
// 'quickLinks' => 'menu-quick-links',
];
//
$jsSettingsForVue = [];
foreach ($menuNames as $name => $menuId) {
$menuLinks = menu_load_links($menuId);
if(!empty($menuLinks)){
//we only need link.node_path_alias and link.link_title
// so lets start a fresh array
$justMenuStuff = [];
foreach ($menuLinks as $key => $value) {
if ($value["hidden"] === "1") { // skip hidden
break;
}
if(strpos($menuLinks[$key]['link_path'], 'node') === 0 ){
// get alias of node
$node_path_alias = '/'.drupal_get_path_alias($value['link_path']);
}else{
// not a node URL so just use link_path
$node_path_alias = $menuLinks[$key]['link_path'];
}
$justMenuStuff[$key]['link_path'] = $value['link_path'];
$justMenuStuff[$key]['link_title'] = $value['link_title'];
$justMenuStuff[$key]['node_path_alias'] = $node_path_alias;
}
$jsSettingsForVue[$name] = $justMenuStuff;
}
}
//Send settings to Javascript
drupal_add_js(array('vue_header' => $jsSettingsForVue), array('type' => 'setting'));
}
@NickStees
Copy link
Author

Results in something like this on pages.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment