2013-10-14 28 views
0

我是在drupal上新建的,並希望在以下方法中添加唯一標識在Drupal主菜單的錨標記中, 假設我有一個「websiteurl/team」頁面 和我想錨定標記爲如何在Drupal主菜單的錨標記中添加唯一標識

<a id="team" href="/team">Team</a> 

我使用此代碼來顯示Drupal的主菜單。

<div<?php print $attributes; ?>> 
    <div<?php print $content_attributes; ?>> 
    <?php if ($main_menu || $secondary_menu): ?> 
    <nav class="navigation"> 
     <?php print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix', 'main-menu')), 'heading' => array('text' => t('Main menu'),'level' => 'h2','class' => array('element-invisible')))); ?> 
     <?php print theme('links__system_secondary_menu', array('links' => $secondary_menu, 'attributes' => array('id' => 'secondary-menu', 'class' => array('links', 'inline', 'clearfix', 'secondary-menu')), 'heading' => array('text' => t('Secondary menu'),'level' => 'h2','class' => array('element-invisible')))); ?> 
    </nav> 
    <?php endif; ?> 
    <?php print $content; ?> 
    </div> 
</div> 

請讓我知道這是怎麼可能

回答

1

你可以做的是,在preprocess_links勾你的主題Drupal的一部分。以下是一些使用超鏈接文本生成唯一ID的示例代碼。如果一個ID已經存在,它只會追加 - {%d}(增量數字)到ID值。

在您的活動主題的template.php裏面。

function YOURTHEME_preprocess_links(&$variables) 
{ 
    // Verify if an ID exist on the links wrapper 
    if (!isset($variables['attributes']['id'])) { 
     return false; 
    } 

    // Only generate ID's on the Main Menu 
    if ($variables['attributes']['id'] !== 'main-menu') { 
     return false; 
    } 

    // Array holding the generated ID's 
    $ids = array(); 
    foreach ($variables['links'] as &$link) { 
     // Loop throug each link and generate unique ID 
     $link['attributes']['id'] = _YOURTHEME_generate_unique_id($link['title'], $ids); 
    } 
} 

// Generate unique ID, recursive call when ID already exists 
function _YOURTHEME_generate_unique_id($id, &$haystack, $index = 0) 
{ 
    $slug = _YOURTHEME_slugify($id); 

    if (in_array($slug, $haystack)) { 
     return _YOURTHEME_generate_unique_id(
      sprintf('%s %d', $id, ++$index), $haystack, $index); 
    } 

    $haystack[] = $slug; 

    return $slug; 
} 

// Generate a 'slug' based on a given string value 
function _YOURTHEME_slugify($text) 
{ 
    // Replace non letter or digits by - 
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text); 

    // Trim 
    $text = trim($text, '-'); 

    // Transliterate 
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); 

    // Lowercase 
    $text = strtolower($text); 

    // Remove unwanted characters 
    $text = preg_replace('~[^-\w]+~', '', $text); 

    if (empty($text)) { 
     return 'n-a'; 
    } 

    return $text; 
} 

不要忘記清除緩存,以便將YOURTHEME_preprocess_links考慮在內。

相關問題