doghouse_menu-3.0.x-dev/doghouse_menu.module
doghouse_menu.module
<?php
/**
* @file
* Contains doghouse_menu.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Extension\Extension;
/**
* Implements hook_help().
*/
function doghouse_menu_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the doghouse_menu module.
case 'help.page.doghouse_menu':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Custom menu functionality') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function doghouse_menu_theme($existing, $type, $theme, $path) {
return [
'region__doghouse_menu' => [
'template' => 'region--doghouse-menu',
'base hook' => 'region',
],
'doghouse_menu' => [
'variables' => [
'items' => NULL,
],
],
'doghouse_menu_accordion' => [
'variables' => [
'items' => NULL,
],
],
'doghouse_menu_toggle' => [
'variables' => [
'text' => 'Toggle',
],
],
'doghouse_menu_close' => [
'variables' => [
'text' => 'Close',
],
],
];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function doghouse_menu_form_menu_link_content_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('menu_link_attributes')) {
$form['actions']['submit']['#submit'][] = 'doghouse_menu_menu_link_content_form_submit';
}
}
/**
* Submit handler for menu link edit form.
*
* The menu_link_attributes module does not store classes as an array, this
* makes it not possible to extend on these classes before rendering.
*/
function doghouse_menu_menu_link_content_form_submit($form, FormStateInterface $form_state) {
$attrs = $form_state->getValue('attributes');
foreach ($attrs as $attr => $value) {
if ($attr === 'class') {
$attrs[$attr] = [$value];
}
}
$form_state->setValue('attributes', $attrs);
}
/**
* Create a new region for the doghouse mobile menu.
*/
function doghouse_menu_system_info_alter(array &$info, Extension $file, $type) {
$theme = \Drupal::service('theme_handler')->getDefault();
if ($type === 'theme' && $file->getName() === $theme) {
$info['regions'] = array_merge($info['regions'], ['doghouse_menu' => 'Doghouse Menu']);
}
}
/**
* Attach menu toggle library to the doghouse_menu region.
*/
function doghouse_menu_preprocess_region__doghouse_menu(array &$variables) {
$variables['#attached']['library'][] = 'doghouse_menu/doghouse_menu_toggle';
}
