The module could do with an option to add <a href="#anchor-name"></a> for a 'back to top' link that you can create in a footer menu.

Currently creating a <nolink> just renders <span title="" class="nolink">top</span> where as it needs to be <a href="#main-content"></a>

The HTML.tpl for the bootstrap base theme for example has <a href="#main-content" class="element-invisible element-focusable"><?php print t('Skip to main content'); ?></a> so this will provide any easy back to top link on all site pages.

I cant change the setting for <nolink> markup as the top menu uses the span tag extensively.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

erg-web’s picture

Issue summary: View changes
erg-web’s picture

Issue summary: View changes
rooby’s picture

Version: 7.x-2.0 » 7.x-2.x-dev
Component: User interface » Code
Issue tags: -special menu items

I agree this needs to be separate from the ability to change the nolink markup.

There would need to be something like a menu path for '<anchor>' and then another entirely new field to store the anchor itself.

The menu_links database table has a column for options, which is for options to pass into l()/url(), which seems like a good place to actually store the data.

rooby’s picture

Here is a patch for this.

It must be applied over the top of the patch in #2325075: Add select options instead of making the user enter strange link paths

I'm wondering if maybe it should be named more specifically, like <current-anchor> or something seeing as it is specifically a current page anchor.

Although I think if we wanted to give full anchor support we are getting a little out of scope of this module and probably should be making a different module that just exposes fields for fragment & query.

rooby’s picture

Status: Active » Needs review
futuredesignUK’s picture

great! sorry new to Drupal - how do I apply a patch to a module?

rooby’s picture

It depends on your operating system.

These are the main docs for it: https://www.drupal.org/patch/apply and there are links off that page for windows/mac specific information.

If none of those solutions seem feasible for you or you can't get it to work or anything I can post a zip file of the whole module.

Sherbet’s picture

rooby, thanks for your effort in making this patch. It works great! Thus far there's only one suggestion I have, which is to strip whitespace from before the anchor text (e.g. ' anchorname' gets put out as '# anchorname').

Hopefully this gets added to the module soon, because it's a very welcome addition.

erg-web’s picture

Thanks

I applied both patches using git (as under GIT)

patch < file.patch

special_menu_items-menu_item_type_selection-2325075-1.patch

and then

special_menu_items-anchor_item-2317329-4.patch

But now my drupal site brings up the error?

Warning: include_once(C:\inetpub\wwwroot\test\sites\all\modules\contrib\special_menu_items\special_menu_items.module): failed to open stream: Permission denied in drupal_load() (line 1112 of C:\inetpub\wwwroot\test\includes\bootstrap.inc).
Warning: include_once(): Failed opening 'C:\inetpub\wwwroot\test/sites/all/modules/contrib/special_menu_items/special_menu_items.module' for inclusion (include_path='.;C:\php\pear') in drupal_load() (line 1112 of C:\inetpub\wwwroot\test\includes\bootstrap.inc).

das-peter’s picture

+++ b/special_menu_items.module
@@ -72,6 +78,11 @@ function special_menu_items_link(array $variables) {
+    // The path will be empty and we will just have an anchor.
+    $variables['path'] = '';

@@ -181,6 +228,18 @@ function special_menu_items_menu_edit_item_validate(&$form, &$form_state) {
+    if (isset($item['menu_item_type']) && $item['menu_item_type'] == '<anchor>') {
+      $item['options']['fragment'] = $item['fragment'];

The current implementation of anchors always points to the frontpage, is this really what we want?
url('', array('fragment' => 'frag')) = /#frag
We could point to the current page using this pattern:
url('', array('fragment' => 'frag', 'external' => TRUE)) = #frag

Edit: I might should mention that we use language negotiation based on language prefix. That might has an effect on url('').

rooby’s picture

Status: Needs review » Needs work

@das-peter:

True, there's no reason not to also allow a path.

I don't think that the external flag is necessary though, I think it should work like the path field normally does and have the user input the full URL if they want external, otherwise it is relative.

das-peter’s picture

@rooby Indeed, it would be nice to be able to use a path as well (e.g. node/123#video). On submit we can split the input using parse_url() to get the path / fragment separated.
However, if no path is given and which, in my eyes, should act as "on the current site" using url('') still will point to the frontpage - to change that we have to do some handling ( and I just figured setting the external flag is the easiest way to get the intended behaviour).

rooby’s picture

Yeah I totally forgot about the current page thing. Probably should have read the title haha.