zt_megamenu-8.x-1.x-dev/zt_megamenu.module

zt_megamenu.module
<?php

/**
 * @file
 * Contains zt_megamenu.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;

/**
 * Implements hook_help().
 */
function zt_megamenu_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the zt_megamenu module.
    case 'help.page.zt_megamenu':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Creative menu style for the drupal 8 menus.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_menu().
 */
function zt_megamenu_menu() {
  $items['admin/config/content/zt_megamenu'] = [
    'title' => 'Multimenu config form',
    'description' => 'Configure ZT Megamenu elements.',
    'route_name' => 'zt_megamenu.settings',
  ];

  return $items;
}

/**
 * Implements hook_page_attachments().
 */
function zt_megamenu_page_attachments(&$page) {
  /*
   * Add module's main library, which includes JS and CSS.
   */
  $page['#attached']['library'][] = 'zt_megamenu/zt_megamenu-assets';
}

/**
 * Implements hook_theme().
 */
function zt_megamenu_theme() {
  return [
    'zt_megamenu_formatter_default' => [
      'variables' => [
        'content' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function zt_megamenu_theme_suggestions_menu_alter(&$suggestions, $vars, $hook) {
  $config = \Drupal::config('zt_megamenu.settings');
  $menu = $config->get('zt_megamenu_menu_id');

  if (isset($vars['menu_name']) &&  $vars['menu_name'] == $menu) {
    $suggestions[] = 'zt_megamenu_formatter_default';
  }
}

/**
 * Custom function to load the submenus and add the node ids.
 */
function parse_submenu($menu_item) {
  if ($menu_item['url']->isRouted()) {
    $url = $menu_item['url']->getInternalPath();
    if ($url != '') {
      $nodes_parse = explode("/", $url);
      $nid = end($nodes_parse);
    }
    return $nid;
  }
  return FALSE;
}

/**
 * Custom function to load the submenus and add the node ids.
 */
function _zt_megamenu_fetch_node_data($node_data) {
  $node_array = [];
  $config = \Drupal::config('zt_megamenu.settings');
  $image_field = $config->get('zt_megamenu_image_machine_name');

  foreach ($node_data as $node) {
    $nid = $title = $body = '';
    $image_url = 'http://cdn3.zyxware.com/files/sliders/main_slider/z2-1447307644-1455262731.jpg';
    if ($node->get('nid')->value) {
      $nid = $node->get('nid')->value;
    }
    if ($node->get('title')->value) {
      $title = $node->get('title')->value;
    }
    if (!empty($node->body)) {
      if ($node->get('body')->value) {
        $body = $node->get('body')->value;
        $body = substr($body, 0, 100) . '...';
        $body = str_ireplace('<p>', '', $body);
        $body = str_ireplace('</p>', '', $body);
      }
    }
    if ($node->hasField($image_field)) {
      if ($node->get($image_field)->target_id) {
        $file = File::load($node->get($image_field)->target_id);
        $image_url = file_create_url($file->getFileUri());
      }
    }
    $node_array[] = [
      'nid' => $nid,
      'title' => $title,
      'body' => $body,
      'image' => $image_url,
    ];
  }
  return $node_array;
}

/**
 * Preprocess zt_megamenu default formatter.
 */
function zt_megamenu_preprocess_zt_megamenu_formatter_default(&$variables) {
  $node_array = [];
  $config = \Drupal::config('zt_megamenu.settings');
  $variables['items']['bgcolor'] = '';
  $variables['items']['txtcolor'] = '';
  $variables['items']['opacity'] = '';
  $variables['items']['content_bgcolor'] = '';

  if ($config->get('zt_megamenu_bgcolor')) {
    if ($config->get('zt_megamenu_bgcolor') != NULL) {
      $variables['items']['bgcolor'] = 'background-color:#' . $config->get('zt_megamenu_bgcolor') . ';';
    }
  }
  if ($config->get('zt_megamenu_txtcolor')) {
    if ($config->get('zt_megamenu_txtcolor') != NULL) {
      $variables['items']['txtcolor'] = 'color:#' . $config->get('zt_megamenu_txtcolor') . ';';
    }
  }
  if ($config->get('zt_megamenu_opacity')) {
    if ($config->get('zt_megamenu_opacity') != NULL) {
      $variables['items']['opacity'] = 'opacity:' . $config->get('zt_megamenu_opacity') . ';';
    }
  }
  if ($config->get('zt_megamenu_content_bgcolor')) {
    if ($config->get('zt_megamenu_content_bgcolor') != NULL) {
      $variables['items']['content_bgcolor'] = 'background-color:#' . $config->get('zt_megamenu_content_bgcolor') . ';';
    }
  }

  foreach ($variables['items'] as $item) {
    if ($item['below']) {
      foreach ($item['below'] as $submenu) {
        $submenu_data = parse_submenu($submenu);
        if ($submenu_data) {
          $node_array[$submenu_data] = $submenu_data;
        }
        if ($submenu['below']) {
          foreach ($submenu['below'] as $submenu_item) {
            $submenu_item_data = parse_submenu($submenu_item);
            if ($submenu_item_data) {
              $node_array[$submenu_item_data] = $submenu_item_data;
            }
            if ($submenu_item['below']) {
              foreach ($submenu_item['below'] as $micromenu_item) {
                $micromenu_data = parse_submenu($micromenu_item);
                if ($micromenu_data) {
                  $node_array[$micromenu_data] = $micromenu_data;
                }
              }
            }
          }
        }
      }
    }
  }
  $node_array = array_filter($node_array);
  $nodes = Node::loadMultiple($node_array);
  $variables['items']['node_details'] = _zt_megamenu_fetch_node_data($nodes);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc