we_megamenu.module in Drupal Mega Menu 8.x
Same filename and directory in other branches
Drupal 8 Mega Menu Module.
File
we_megamenu.moduleView source
<?php
use Drupal\Component\Utility\UrlHelper;
/**
* @file
* Drupal 8 Mega Menu Module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\we_megamenu\WeMegaMenuBuilder;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
/**
* Implements hook_theme().
*/
function we_megamenu_theme($existing, $type, $theme, $path) {
$items['we_megamenu_backend'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'admin',
'blocks' => NULL,
'block_theme' => NULL,
'data_config' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_frontend'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => '',
'blocks' => NULL,
'block_theme' => NULL,
'data_config' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_ul'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'frontend',
'items' => NULL,
'subtree' => NULL,
'data_config' => NULL,
'block_theme' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_li'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'frontend',
'title' => NULL,
'subtree' => NULL,
'items' => NULL,
'item' => NULL,
'data_config' => NULL,
'block_theme' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_submenu'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'frontend',
'title' => NULL,
'subtree' => NULL,
'items' => NULL,
'data_config' => NULL,
'item_config' => NULL,
'block_theme' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_block'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'frontend',
'title' => NULL,
'subtree' => NULL,
'items' => NULL,
'data_config' => NULL,
'item_config' => NULL,
'block_content' => NULL,
'block_theme' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_row'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'frontend',
'title' => NULL,
'subtree' => NULL,
'items' => NULL,
'data_config' => NULL,
'item_config' => NULL,
'block_theme' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_col'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'frontend',
'title' => NULL,
'subtree' => NULL,
'items' => NULL,
'data_config' => NULL,
'item_config' => NULL,
'block_theme' => NULL,
'trail' => NULL,
],
];
$items['we_megamenu_subul'] = [
'variables' => [
'menu_name' => NULL,
'content' => NULL,
'section' => 'frontend',
'items' => NULL,
'subtree' => NULL,
'data_config' => NULL,
'item_config' => NULL,
'block_theme' => NULL,
'trail' => NULL,
],
];
return $items;
}
/**
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_backend(array &$vars) {
ini_set('xdebug.max_nesting_level', 1024);
$menu_name = $vars['menu_name'];
$block_theme = $vars['block_theme'];
$data_config = WeMegaMenuBuilder::loadConfig($menu_name, $block_theme);
if (empty($data_config)) {
$data_config = WeMegaMenuBuilder::initMegamenu($menu_name, $block_theme);
}
else {
if (isset($data_config->menu_update_flag) && $data_config->menu_update_flag) {
// drupal_flush_all_caches();
$theme_name = $block_theme;
$menu_items = WeMegaMenuBuilder::getMenuTreeOrder($menu_name, FALSE);
WeMegaMenuBuilder::updateMegamenuFromDrupalMenu($data_config, $menu_items, 0);
$data_config->menu_update_flag = 0;
WeMegaMenuBuilder::saveConfig($menu_name, $theme_name, json_encode($data_config));
// drupal_flush_all_caches();
}
}
$block_config = isset($data_config->block_config) ? $data_config->block_config : [];
$menu_items = WeMegaMenuBuilder::getMenuTreeOrder($menu_name, TRUE);
$trail = WeMegaMenuBuilder::buildPageTrail($menu_items);
$vars['content'] = [
'#theme' => 'we_megamenu_ul',
'#items' => $menu_items,
'#data_config' => $data_config ? $data_config : NULL,
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $trail,
];
$vars['attributes'] = new Attribute();
$vars['attributes']['class'] = [];
$vars['attributes']['class'][] = $menu_name;
$vars['attributes']['class'][] = 'navbar';
$vars['attributes']['class'][] = 'navbar-default';
$vars['attributes']['class'][] = 'navbar-we-mega-menu';
$vars['attributes']['class'][] = $vars['section'];
$vars['attributes']['class'][] = isset($data_config->block_config->{'auto-mobile-collapse'}) == 1 ? 'mobile-collapse' : '';
$vars['attributes']['data-menu-name'] = $menu_name;
$vars['attributes']['data-block-theme'] = $block_theme;
$vars['attributes']['data-style'] = isset($data_config->block_config->style) ? $data_config->block_config->style : 'Default';
$vars['attributes']['data-animation'] = isset($data_config->block_config->animation) ? $data_config->block_config->animation : 'None';
$vars['attributes']['data-delay'] = isset($data_config->block_config->delay) ? $data_config->block_config->delay : '';
$vars['attributes']['data-duration'] = isset($data_config->block_config->duration) ? $data_config->block_config->duration : '';
$vars['attributes']['data-autoarrow'] = isset($data_config->block_config->{'auto-arrow'}) ? $data_config->block_config->{'auto-arrow'} : '';
$vars['attributes']['data-alwayshowsubmenu'] = isset($data_config->block_config->{'always-show-submenu'}) ? $data_config->block_config->{'always-show-submenu'} : '';
$vars['attributes']['data-action'] = isset($data_config->block_config->action) ? $data_config->block_config->action : 'hover';
$vars['attributes']['data-mobile-collapse'] = isset($data_config->block_config->{'auto-mobile-collapse'}) ? $data_config->block_config->{'auto-mobile-collapse'} : 0;
}
/**
* Prepare variables for we_megamenu_frontend.
*
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_frontend(&$vars) {
ini_set('xdebug.max_nesting_level', 1024);
$menu_name = $vars['menu_name'];
$block_theme = $vars['block_theme'];
$data_config = WeMegaMenuBuilder::loadConfig($menu_name, $block_theme);
if (empty($data_config)) {
$data_config = WeMegaMenuBuilder::initMegamenu($menu_name, $block_theme);
}
else {
if (isset($data_config->menu_update_flag) && $data_config->menu_update_flag) {
// drupal_flush_all_caches();
$theme_name = $block_theme;
$menu_items = WeMegaMenuBuilder::getMenuTreeOrder($menu_name, FALSE);
WeMegaMenuBuilder::updateMegamenuFromDrupalMenu($data_config, $menu_items, 0);
$data_config->menu_update_flag = 0;
WeMegaMenuBuilder::saveConfig($menu_name, $theme_name, json_encode($data_config));
// drupal_flush_all_caches();
}
}
$menu_items = WeMegaMenuBuilder::getMenuTreeOrder($menu_name, FALSE);
$trail = WeMegaMenuBuilder::buildPageTrail($menu_items);
$vars['content'] = [
'#theme' => 'we_megamenu_ul',
'#items' => $menu_items,
'#data_config' => $data_config ? $data_config : NULL,
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $trail,
];
$vars['attributes'] = new Attribute();
$vars['attributes']['class'] = [];
$vars['attributes']['class'][] = $menu_name;
$vars['attributes']['class'][] = 'navbar';
$vars['attributes']['class'][] = 'navbar-default';
$vars['attributes']['class'][] = 'navbar-we-mega-menu';
$vars['attributes']['class'][] = $vars['section'];
$vars['attributes']['class'][] = isset($data_config->block_config->{'auto-mobile-collapse'}) == 1 ? 'mobile-collapse' : '';
$vars['attributes']['data-menu-name'] = $menu_name;
$vars['attributes']['data-block-theme'] = $block_theme;
$vars['attributes']['data-style'] = isset($data_config->block_config->style) ? $data_config->block_config->style : 'Default';
$vars['attributes']['data-animation'] = isset($data_config->block_config->animation) ? $data_config->block_config->animation : 'None';
$vars['attributes']['data-delay'] = isset($data_config->block_config->delay) ? $data_config->block_config->delay : '';
$vars['attributes']['data-duration'] = isset($data_config->block_config->duration) ? $data_config->block_config->duration : '';
$vars['attributes']['data-autoarrow'] = isset($data_config->block_config->{'auto-arrow'}) ? $data_config->block_config->{'auto-arrow'} : '';
$vars['attributes']['data-alwayshowsubmenu'] = isset($data_config->block_config->{'always-show-submenu'}) ? $data_config->block_config->{'always-show-submenu'} : '';
$vars['attributes']['data-action'] = isset($data_config->block_config->action) ? $data_config->block_config->action : 'hover';
$vars['attributes']['data-mobile-collapse'] = isset($data_config->block_config->{'auto-mobile-collapse'}) ? $data_config->block_config->{'auto-mobile-collapse'} : 0;
$vars['attributes']['class'][] = $vars['attributes']['data-action'] == 'clicked' ? 'click-action' : 'hover-action';
}
/**
* Prepare variables for we_megamenu_ul.
*
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_ul(array &$vars, $backend = FALSE) {
$data_config = $vars['data_config'];
foreach ($vars['items'] as $key_li => $li) {
$vars['href'] = '';
if ($li['url'] == '<front>') {
$vars['attributes']['data-id'] = 'home';
$vars['href'] = Url::fromRoute('<front>')
->toString();
}
else {
$vars['attributes']['data-id'] = !empty($li['derivativeId']) ? $li['derivativeId'] : $li['url'];
$current_user = \Drupal::currentUser();
if (WeMegaMenuBuilder::routeExists($li['url'])) {
$vars['href'] = Url::fromRoute($li['url'])
->toString();
}
else {
$vars['href'] = $li['url'];
}
}
if (!UrlHelper::isExternal($vars['href'])) {
if ($backend === FALSE && Url::fromUri('internal:' . $vars['href'])
->access() == FALSE) {
continue;
}
}
if (strpos($vars['href'], 'user/logout') !== FALSE && $current_user
->id() == 0 && $vars['section'] != 'admin' && $backend === FALSE) {
continue;
}
if (strpos($vars['href'], 'user/login') !== FALSE && $current_user
->id() != 0 && $vars['section'] != 'admin' && $backend === FALSE) {
continue;
}
$vars['content'][] = [
'#theme' => [
'we_megamenu_li',
],
'#title' => $li['title'],
'#item' => $li,
'#subtree' => $li['subtree'],
'#data_config' => $vars['data_config'],
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
$vars['attributes'] = new Attribute();
$vars['attributes']['class'] = [];
$vars['attributes']['class'][] = 'we-mega-menu-ul';
$vars['attributes']['class'][] = 'nav';
$vars['attributes']['class'][] = 'nav-tabs';
}
/**
* Prepare variables for we_megamenu_li.
*
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_li(&$vars) {
$menu_name = $vars['menu_name'];
$block_theme = $vars['block_theme'];
$data_config = WeMegaMenuBuilder::loadConfig($menu_name, $block_theme);
$menu_config = isset($data_config->menu_config) ? $data_config->menu_config : [];
$vars['attributes'] = new Attribute();
$vars['attributes']['class'] = [];
$vars['attributes']['class'][] = 'we-mega-menu-li';
$vars['attributes']['class'][] = count($vars['subtree']) ? 'dropdown-menu' : '';
$vars['attributes']['data-level'] = $vars['item']['level'];
$vars['attributes']['data-element-type'] = 'we-mega-menu-li';
$vars['attributes']['description'] = isset($vars['item']['description']) ? $vars['item']['description'] : '';
$vars['href'] = '';
if ($vars['item']['url'] == '<front>') {
$vars['attributes']['data-id'] = 'home';
$vars['href'] = Url::fromRoute('<front>')
->toString();
}
else {
$vars['attributes']['data-id'] = !empty($vars['item']['derivativeId']) ? $vars['item']['derivativeId'] : $vars['item']['url'];
$vars['href'] = $vars['item']['url'];
}
$vars['attributes']['data-submenu'] = 0;
$vars['attributes']['hide-sub-when-collapse'] = 0;
if (isset($menu_config->{$vars['attributes']['data-id']})) {
$item_config = $menu_config->{$vars['attributes']['data-id']};
$vars['attributes']['class'][] = !empty($item_config->item_config->group) ? 'we-mega-menu-group' : '';
$vars['attributes']['class'][] = !empty($item_config->item_config->class) ? $item_config->item_config->class : '';
$vars['attributes']['class'][] = !empty($item_config->item_config->{'hide_sub_when_collapse'}) ? 'sub-hidden-collapse' : '';
$vars['attributes']['class'][] = !empty($item_config->item_config->{'data-alignsub'}) ? $item_config->item_config->{'data-alignsub'} : '';
$vars['attributes']['data-group'] = !empty($item_config->item_config->group) ? $item_config->item_config->group : 0;
$vars['attributes']['data-class'] = !empty($item_config->item_config->class) ? $item_config->item_config->class : '';
$vars['attributes']['data-icon'] = !empty($item_config->item_config->{'data-icon'}) ? $item_config->item_config->{'data-icon'} : '';
$vars['attributes']['data-caption'] = !empty($item_config->item_config->{'data-caption'}) ? $item_config->item_config->{'data-caption'} : '';
$vars['attributes']['hide-sub-when-collapse'] = !empty($item_config->item_config->{'hide_sub_when_collapse'}) ? $item_config->item_config->{'hide_sub_when_collapse'} : '';
$vars['attributes']['data-alignsub'] = !empty($item_config->item_config->{'data-alignsub'}) ? $item_config->item_config->{'data-alignsub'} : '';
$vars['attributes']['data-target'] = !empty($item_config->item_config->{'data-target'}) ? $item_config->item_config->{'data-target'} : '';
}
$trail = $vars['trail'];
$plugin_id = $vars['item']['plugin_id'];
if (isset($trail[$plugin_id])) {
$vars['attributes']['class'][] = 'active';
$vars['attributes']['class'][] = 'active-trail';
$vars['link']['attributes']['class'][] = 'active-trail';
}
if ($vars['subtree']) {
$vars['content'] = [
'#theme' => 'we_megamenu_submenu',
'#items' => $vars['subtree'],
'#item_config' => isset($menu_config->{$vars['attributes']['data-id']}) ? $menu_config->{$vars['attributes']['data-id']} : [],
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
$vars['items'] = $vars['subtree'];
$vars['attributes']['data-submenu'] = 1;
$vars['attributes']['data-group'] = !empty($item_config->item_config->group) ? $item_config->item_config->group : 0;
}
elseif (isset($menu_config->{$vars['attributes']['data-id']}) && count($menu_config->{$vars['attributes']['data-id']}->rows_content)) {
$item_config = $menu_config->{$vars['attributes']['data-id']};
$vars['content'] = [
'#theme' => 'we_megamenu_submenu',
'#items' => $vars['subtree'],
'#item_config' => $item_config,
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
$vars['attributes']['data-submenu'] = 1;
$vars['attributes']['class'][] = 'dropdown-menu';
}
}
/**
* Prepare variables for we_megamenu_submenu.
*
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_submenu(array &$vars) {
$item_config = $vars['item_config'];
if (isset($item_config->rows_content) && !empty($item_config->rows_content)) {
$rows_content = $item_config->rows_content;
foreach ($rows_content as $key_row => $row) {
$vars['row'][] = [
'#theme' => 'we_megamenu_row',
'#items' => $vars['items'],
'#item_config' => $row,
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
}
else {
$vars['row'] = [
'#theme' => 'we_megamenu_row',
'#items' => $vars['items'],
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
$submenu_config = isset($item_config->submenu_config) ? $item_config->submenu_config : [];
$vars['attributes'] = new Attribute();
$vars['attributes']['class'] = [];
$vars['attributes']['class'][] = 'we-mega-menu-submenu';
$vars['attributes']['class'][] = isset($submenu_config->class) ? $submenu_config->class : '';
$vars['attributes']['data-element-type'] = 'we-mega-menu-submenu';
$vars['attributes']['data-submenu-width'] = isset($submenu_config->width) ? $submenu_config->width : '';
$vars['attributes']['data-class'] = isset($submenu_config->class) ? $submenu_config->class : '';
$vars['attributes']['style'] = isset($submenu_config->width) ? 'width: ' . $submenu_config->width . 'px' : '';
}
/**
* Prepare variables for we_megamenu_row.
*
* @param array $vars variables are declared in theme.
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_row(array &$vars) {
$vars['attributes'] = new Attribute();
$vars['attributes']['class'] = [];
$vars['attributes']['class'][] = 'we-mega-menu-row';
$vars['attributes']['data-element-type'] = 'we-mega-menu-row';
if ($vars['item_config']) {
foreach ($vars['item_config'] as $key_col => $col) {
$vars['col'][] = [
'#theme' => 'we_megamenu_col',
'#items' => $vars['items'],
'#item_config' => $col,
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
$vars['attributes']['data-custom-row'] = isset(reset($col->col_content)->mlid) ? 0 : 1;
}
}
else {
$vars['col'] = [
'#theme' => 'we_megamenu_col',
'#items' => $vars['items'],
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
}
/**
* Prepare variables for we_megamenu_col.
*
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_col(array &$vars) {
$vars['attributes'] = new Attribute();
$vars['attributes']['class'] = [];
$vars['attributes']['class'][] = 'we-mega-menu-col';
$vars['attributes']['data-element-type'] = 'we-mega-menu-col';
if ($vars['item_config']) {
$col_items = $vars['item_config'];
$col_config = $col_items->col_config;
if (isset($col_config->block) && empty($col_config->block) && !empty($col_items->col_content)) {
$vars['subul'] = [
'#theme' => 'we_megamenu_subul',
'#items' => $vars['items'],
'#item_config' => $col_items->col_content,
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
if (isset($col_items->col_content->block_id)) {
$block_id = $col_items->col_content->block_id;
$title = isset($col_config->block_title) && $col_config->block_title == 1 ? TRUE : FALSE;
$vars['subul'] = [
'#theme' => 'we_megamenu_block',
'#block_content' => WeMegaMenuBuilder::renderBlock($block_id, $title, $vars['section']),
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
$vars['attributes']['class'][] = 'span' . $col_config->width;
$vars['attributes']['class'][] = isset($col_config->hidewhencollapse) && $col_config->hidewhencollapse && !empty($col_config->hidewhencollapse) ? 'hidden-collapse' : '';
$vars['attributes']['class'][] = isset($col_config->class) ? $col_config->class : '';
$vars['attributes']['data-width'] = $col_config->width;
$vars['attributes']['data-block'] = isset($col_config->block) ? $col_config->block : '';
$vars['attributes']['data-blocktitle'] = isset($col_config->block_title) ? $col_config->block_title : '';
$vars['attributes']['data-hidewhencollapse'] = isset($col_config->hidewhencollapse) ? $col_config->hidewhencollapse : '0';
$vars['attributes']['data-class'] = isset($col_config->class) ? $col_config->class : '';
$vars['attributes']['data-blocktitle'] = isset($col_config->block_title) ? $col_config->block_title : '';
}
else {
$vars['subul'] = [
'#theme' => 'we_megamenu_subul',
'#items' => $vars['items'],
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
];
}
}
/**
* Prepare variables for we_megamenu_subul.
*
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_subul(array &$vars) {
if ($vars['item_config']) {
foreach ($vars['items'] as $key_li => $li) {
$derivativeId = $li['derivativeId'];
foreach ($vars['item_config'] as $key_item => $item) {
if (isset($item->mlid)) {
if ($item->mlid == $derivativeId) {
$vars['lis'][] = [
'#theme' => [
'we_megamenu_li',
],
'#title' => $li['title'],
'#item' => $li,
'#subtree' => $li['subtree'],
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
}
}
}
}
else {
foreach ($vars['items'] as $key_li => $li) {
$vars['lis'][] = [
'#theme' => [
'we_megamenu_li',
],
'#title' => $li['title'],
'#item' => $li,
'#subtree' => $li['subtree'],
'#section' => $vars['section'],
'#block_theme' => $vars['block_theme'],
'#menu_name' => $vars['menu_name'],
'#trail' => $vars['trail'],
];
}
}
}
/**
* Prepare variables for we_megamenu_block.
*
* @param array $vars
* Variables are declared in theme.
*/
function template_preprocess_we_megamenu_block(array &$vars) {
$vars['block_content'] = $vars['block_content'];
}
/**
* Implements hook_block_view_BASE_BLOCK_ID_alter().
*
* Config Contextual link of Drupal 8 Mega Menu blocks.
*/
function we_megamenu_block_view_we_megamenu_block_alter(array &$build, BlockPluginInterface $block) {
$menus = menu_list_system_menus();
$menu_name = $block
->getDerivativeId();
if (isset($menus[$menu_name])) {
$build['#contextual_links']['menu'] = [
'route_parameters' => [
'menu' => $menu_name,
],
];
}
$build['#contextual_links']['we_megamenu_block']['route_parameters'] = [
'menu_name' => $build['#derivative_plugin_id'],
];
}
/**
* Implements hook_entity_insert().
*
* Reset menu to default status when origin menu changed.
*/
function we_megamenu_entity_insert(EntityInterface $entity) {
if (method_exists($entity, 'getTypedData') && method_exists($entity, 'getEntityTypeId')) {
$data = $entity
->getTypedData();
$data = $data
->toArray();
$entity_type = $entity
->getEntityTypeId();
switch ($entity_type) {
case 'menu':
break;
case 'menu_link_content':
if (isset($data['parent'][0]['value'])) {
$mid_parent = $data['parent'][0]['value'];
$mid_parent = str_replace('menu_link_content:', '', $mid_parent);
$menu_name = $data['menu_name'][0]['value'];
$config = \Drupal::config('system.theme');
$theme_name = $config
->get('default');
# Load main menu and insert to megamenu
$menu_item_obj = new stdClass();
$menu_item_obj->rows_content = [];
$menu_item_obj->submenu_config = new stdClass();
$menu_item_obj->submenu_config->width = '';
$menu_item_obj->submenu_config->height = '';
$menu_item_obj->submenu_config->type = '';
$menu_item_obj->item_config = new stdClass();
$menu_item_obj->item_config->level = 0;
$menu_item_obj->item_config->type = '';
$menu_item_obj->item_config->id = $entity
->get('uuid')
->getString();
$menu_item_obj->item_config->submenu = '';
$menu_item_obj->item_config->hide_sub_when_collapse = '';
$menu_item_obj->item_config->group = '';
$menu_item_obj->item_config->class = '';
$menu_item_obj->item_config->{'data-icon'} = '';
$menu_item_obj->item_config->{'data-caption'} = '';
$menu_item_obj->item_config->{'data-alignsub'} = '';
$menu_item_obj->item_config->{'data-target'} = '_self';
# Mega menu config
$menu_config = WeMegaMenuBuilder::loadConfig($menu_name, $theme_name);
foreach ($menu_config->menu_config as $key_menu => $menu) {
$mid_parent = $mid_parent == 'standard.front_page' ? base_path() : $mid_parent;
if ($key_menu == $mid_parent) {
$rows_content = $menu->rows_content;
$tmp_col_content = new stdClass();
$tmp_col_content->mlid = $entity
->get('uuid')
->getString();
$tmp_col_content->type = 'we-mega-menu-li';
$tmp_col_content->item_config = new stdClass();
$tmp_col_cfg = new stdClass();
$tmp_col_cfg->hidewhencollapse = '';
$tmp_col_cfg->type = 'we-mega-menu-col';
$tmp_col_cfg->width = 12;
$tmp_col_cfg->block = '';
$tmp_col_cfg->class = '';
$tmp_col_cfg->block_title = 0;
$child_item = [
'col_content' => $tmp_col_content,
'col_cfg' => $tmp_col_cfg,
];
$menu_position = WeMegaMenuBuilder::menuItemInsert($key_menu, $menu_config, $menu, $child_item);
}
}
$menu_config->menu_config->{$entity
->get('uuid')
->getString()} = $menu_item_obj;
$data_cfg = json_encode($menu_config);
WeMegaMenuBuilder::saveConfig($menu_name, $theme_name, $data_cfg);
}
break;
}
}
}
/**
* Implements hook_entity_delete().
*/
function we_megamenu_entity_delete(EntityInterface $entity) {
if (method_exists($entity, 'getTypedData') && method_exists($entity, 'getEntityTypeId')) {
$data = $entity
->getTypedData();
$data = $data
->toArray();
$entity_type = $entity
->getEntityTypeId();
switch ($entity_type) {
case 'menu':
break;
case 'menu_link_content':
$menu_name = $data['menu_name'][0]['value'];
$config = \Drupal::config('system.theme');
$theme_name = $config
->get('default');
$menu_uuid = $entity
->get('uuid')
->getString();
$menu_uuid = $menu_uuid == 'standard.front_page' ? base_path() : $menu_uuid;
$menu_config = WeMegaMenuBuilder::loadConfig($menu_name, $theme_name);
WeMegaMenuBuilder::menuItemDelete($menu_config, $menu_uuid);
WeMegaMenuBuilder::saveConfig($menu_name, $theme_name, json_encode($menu_config));
break;
}
}
}
/**
* Implements hook_entity_presave().
*
* Reset menu to default status when origin menu changed.
*/
function we_megamenu_entity_presave(EntityInterface $entity) {
if (method_exists($entity, 'getTypedData') && method_exists($entity, 'getEntityTypeId')) {
$data = $entity
->getTypedData();
$data = $data
->toArray();
$entity_type = $entity
->getEntityTypeId();
switch ($entity_type) {
case 'menu':
$menu_name = $data['id'];
$config = \Drupal::config('system.theme');
$theme_name = $config
->get('default');
$tmp_col_content = new stdClass();
$tmp_col_content->mlid = '';
$tmp_col_content->type = 'we-mega-menu-li';
$tmp_col_content->item_config = new stdClass();
$tmp_col_cfg = new stdClass();
$tmp_col_cfg->hidewhencollapse = '';
$tmp_col_cfg->type = 'we-mega-menu-col';
$tmp_col_cfg->width = 12;
$tmp_col_cfg->block = '';
$tmp_col_cfg->class = '';
$tmp_col_cfg->block_title = 0;
$child_item = [
'col_content' => $tmp_col_content,
'col_cfg' => $tmp_col_cfg,
];
$menu_config = WeMegaMenuBuilder::loadConfig($menu_name, $theme_name);
if (!empty($menu_config)) {
$menu_config->menu_update_flag = 1;
WeMegaMenuBuilder::saveConfig($menu_name, $theme_name, json_encode($menu_config));
}
break;
case 'menu_link_content':
break;
}
}
}
/**
* Function we_megamenu_preprocess_html.
*/
function we_megamenu_preprocess_html(&$vars) {
$current_path = \Drupal::service('path.current')
->getPath();
$need_path = 'admin/structure/we-mega-menu/';
if (strpos($current_path, $need_path) !== FALSE) {
$vars['attributes']['class'][] = 'we-mega-menu-backend';
$backend_style = \Drupal::state()
->get('we_megamenu_backend_style') ?: '';
if (!empty($backend_style)) {
$vars['attributes']['class'][] = $backend_style;
}
}
}
/**
* Function we_megamenu_flush_render_cache.
*/
function we_megamenu_flush_render_cache() {
$renderCache = \Drupal::service('cache.render');
$renderCache
->invalidateAll();
}