Exemplo n.º 1
0
/**
 * @file
 * template.php
 */
function bootstrap2_menu_link(array $variables)
{
    $element = $variables['element'];
    $sub_menu = '';
    if ($element['#below']) {
        // Prevent dropdown functions from being added to management menu so it
        // does not affect the navbar module.
        if ($element['#original_link']['menu_name'] == 'management' && module_exists('navbar')) {
            $sub_menu = drupal_render($element['#below']);
        } elseif (!empty($element['#original_link']['depth']) && $element['#original_link']['depth'] == 1) {
            // Add our own wrapper.
            unset($element['#below']['#theme_wrappers']);
            $sub_menu = '<ul class="dropdown-menu">' . drupal_render($element['#below']) . '</ul>';
            // Generate as standard dropdown.
            $element['#title'] .= ' <span class="caret"></span>';
            $element['#attributes']['class'][] = 'dropdown';
            $element['#localized_options']['html'] = TRUE;
            // Set dropdown trigger element to # to prevent inadvertant page loading
            // when a submenu link is clicked.
            $element['#localized_options']['attributes']['data-target'] = '#';
            $element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
            $element['#localized_options']['attributes']['data-toggle'] = 'dropdown';
        }
    }
    // On primary navigation menu, class 'active' is not set on active menu item.
    // @see https://drupal.org/node/1896674
    if (($element['#href'] == $_GET['q'] || $element['#href'] == '<front>' && drupal_is_front_page()) && empty($element['#localized_options']['language'])) {
        $element['#attributes']['class'][] = 'active';
    }
    // comportement standard du theme bootstrap
    $output = l($element['#title'], $element['#href'], $element['#localized_options']);
    // Chargement de la conf du module single_page_site
    if (module_exists('single_page_site')) {
        $single_page_site_settings = variable_get('single_page_site_settings', array());
        if (array_key_exists('menu', $single_page_site_settings) && $element['#original_link']['menu_name'] == $single_page_site_settings['menu']) {
            // remplacement des liens avec ancres
            if (in_array($single_page_site_settings['class'], $element['#localized_options']['attributes']['class'])) {
                $element['#localized_options']['fragment'] = _single_page_site_generate_anchor($element['#href']);
                $element['#localized_options']['attributes']['class'][] = 'page-scroll';
                $element['#localized_options']['external'] = TRUE;
                // si page en cours est l'accueil + la one page est aussi l'accueil
                if (variable_get('site_frontpage') == 'single-page-site') {
                    $output = l($element['#title'], NULL, $element['#localized_options']);
                } else {
                    $output = l($element['#title'], 'single-page-site', $element['#localized_options']);
                }
            }
        }
    }
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
 /**
  * Loads single page.
  * @return type
  */
 public function load()
 {
     if (empty($this->settings->get('menu'))) {
         // If settings aren't set.
         return array('#markup' => $this->t('You have to !configure single page before you can use it.', array('!configure' => \Drupal::l(t('configure'), Url::fromRoute('single_page_site.config')))));
     }
     $items = array();
     $current_item_count = 1;
     $messages = drupal_get_messages();
     $menu_name = $this->settings->get('menu');
     $tree = _single_page_site_get_menu_children($menu_name);
     foreach ($tree as $object) {
         $output = NULL;
         // Check if menu item has to be rendered.
         $render_menu_item = FALSE;
         $itemPluginDefinition = $object->link->getPluginDefinition();
         if ($itemPluginDefinition['route_name'] != '<front>') {
             if (empty($this->settings->get('class'))) {
                 // If class is empty => all menu items.
                 $render_menu_item = TRUE;
             }
             // TODO: if menu attributes is ported, add check if class hide is applied.
         }
         if ($render_menu_item) {
             // Collect generated messages.
             $messages += drupal_get_messages();
             // Get params.
             $params = $itemPluginDefinition['route_parameters'];
             // Generate href.
             $href = Url::fromRoute($itemPluginDefinition['route_name'], $params)->toString();
             // Filter href.
             $anchor = _single_page_site_generate_anchor($href);
             $item_title = $itemPluginDefinition['title'];
             $item_tag = $this->settings->get('tag');
             // Handle sub request.
             $render = $this->handleSubRequest($href);
             $output = is_array($render) ? $this->renderer->render($render) : $render;
             // Let other modules makes changes to $output.
             \Drupal::moduleHandler()->alter('single_page_site_output', $output, $current_item_count);
             // Build renderable array.
             $item = array('output' => $output, 'anchor' => $anchor, 'title' => $item_title, 'tag' => $item_tag);
             $items[] = $item;
             $current_item_count++;
         }
     }
     // Reinject the messages.
     foreach ($messages as $type => $data) {
         foreach ($data as $message) {
             drupal_set_message($message, $type);
         }
     }
     // Render output and attach JS files.
     $js_settings = array('menuClass' => $this->settings->get('menuclass'), 'distanceUp' => $this->settings->get('up'), 'distanceDown' => $this->settings->get('down'), 'updateHash' => $this->settings->get('updatehash'), 'offsetSelector' => $this->settings->get('offsetselector'));
     $page_content = array('#theme' => 'single_page_site', '#items' => $items, '#title' => $this->setTitle(), '#attached' => array('library' => array('single_page_site/single_page_site.scrollspy')));
     if ($this->settings->get('smoothscrolling')) {
         // Add smotth scrolling.
         $page_content['#attached']['library'][] = 'single_page_site/single_page_site.scroll';
     }
     $page_content['#attached']['drupalSettings']['singlePage'] = $js_settings;
     return $page_content;
 }