So häufig braucht man zusätzliche Informationen oder Optionen beim Rendern von Menus.
Für diese Speziale Bedürfnis soll man die Funktion „function menu_attributes_menu_attribute_info() " überschreiben bzw. mit Hilfe von „hook" erweitern.
Hier gibt es ein Beispiel, ich habe 2 neue Attributen zugefügt („topelement" , „bottomelement"):
function menu_attributes_menu_attribute_info() {
$info['title'] = array(
'label' => t('Title'),
'description' => t('The description displayed when hovering over the link.'),
'form' => array(
'#type' => 'textarea',
'#rows' => 2,
),
);
$info['id'] = array(
'label' => t('ID'),
'description' => t('Specifies a unique ID for the link.'),
);
$info['topelement'] = array(
'label' => t('Top Element'),
'description' => t('HTML-Element at top of anchor'),
);
$info['bottomelement'] = array(
'label' => t('Bottom Element'),
'description' => t('HTML-Element at bottom of anchor'),
);
$info['name'] = array(
'label' => t('Name'),
);
$info['rel'] = array(
'label' => t('Relationship'),
'description' => t("Specifies the relationship between the current page and the link. Enter 'nofollow' here to nofollow this link."),
);
$info['class'] = array(
'label' => t('Classes'),
'description' => t('Enter additional classes to be added to the link.'),
);
$info['style'] = array(
'label' => t('Style'),
'description' => t('Enter additional styles to be applied to the link.'),
);
$info['target'] = array(
'label' => t('Target'),
'description' => t('Specifies where to open the link. Using this attribute breaks XHTML validation.'),
'form' => array(
'#type' => 'select',
'#options' => array(
'' => 'None (i.e. same window)',
'_blank' => 'New window (_blank)',
'_top' => 'Top window (_top)',
'_self' => 'Same window (_self)',
'_parent' => 'Parent window (_parent)',
),
),
);
$info['accesskey'] = array(
'label' => t('Access Key'),
'description' => t('Specifies a <a href="@accesskey">keyboard shortcut</a> to access this link.', array('@accesskey' => url('http://en.wikipedia.org/wiki/Access_keys'))),
'form' => array(
'#maxlength' => 1,
'#size' => 1,
),
);
return $info;
}
Für Rendern des Menus soll man für jede theme eine Datei „template.php" hin fügen.
Hier wird man Ausgabe von Menus programmieren.
Bitte beachten Sie, dass jedes Attribut, das nicht „NULL" ist, standardmäßig in Anchor vorkommt. Deshalb muss man vor der Benutzung der Funktion „l" (Einbau des Anchors), die Werten überprüfen.
Hier meine Beispiel : In diesem Beispiel werden 2 Attributen („topelement" , „bottomelement") als 2 Elementen oben und unten von Anchor zwischen „<span></span>" gezeigt.
function colourise_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
if (!$link['page_callback'] && strpos( $link['href'], 'fake.link')) {
return '<a href="#" onclick=";return false" class="nolink">'. $link['title'] .'</a>';
}
else {
$myLink = '';
$topEl = $link['localized_options']['attributes']['topelement'];
$bottomEl = $link['localized_options']['attributes']['bottomelement'];
if($topEl) {
$myLink = '<span class="topImage">' . $topEl . '</span>';
}
if($link['localized_options']['attributes']['topelement'] || $link['localized_options']['attributes']['bottomelement']) {
unset($link['localized_options']['attributes']['topelement']) ; // Weil das Attribute muss nicht in Anchor vorkommen.
unset($link['localized_options']['attributes']['bottomelement']); // Weil das Attribute muss nicht in Anchor vorkommen.
}
$myLink .= l($link['title'], $link['href'], $link['localized_options']); // Standardfunktion von Drupal
if($bottomEl) {
$myLink .= '<span>'. $bottomEl . '</span>';
}
return $myLink;
}
}
Die Funktion „menu_item_link($link)" wird übergeschrieben. Für jedes Theme wird die Funktion mit dem Namen von Theme angefangen.
z.B hier habe ich theme „colourise" benutzt, deshalb die Funktion heißt „colourise_menu_item_link($link)".














