I'm building a one page website, but I'm having trouble navigating through the page. I can't figure out how to add an attribute ID to a block or other elements on the page. In same cases there is already an ID, but in most cases not. So when I set up the menu, I don't know where to link the item to. For example: I have a block entitled "news" halfway the page and a menu item "news". How do I link this menu item to the title of the block?

I use the "One-page navigation" module, but I can't find any functionality there to help me out. I am a beginner with Backdrop CMS, although I have build some websites with Drupal 7. Help would be much appreciated.

Accepted answer

Hi Alex, welcome to the Backdrop Forum! As far as I know, there isn't a standard solution to create attribute IDs for Backdrop elements. So, depending on your elements, there are different options, or workarounds. For example:

  • Create a custom block with text format "Raw HTML", put the ID in the text area, and place the block near your element.
  • Add a custom text element with your ID to the header of your views block (see screenshot).
     
  • I'm sure there are more workarounds.

    If I were to build a one-page website, I'd use a custom layout template and put my IDs in the HTML. (This will hopefully be easier in the future, if Backdrop allows to add custom IDs in flexible layout templates.)

     

Most helpful answers

I guess the task can be solved using the Entity Reference module.

If the main page of the site is built as fields with nodes that are displayed in it by Entity Reference, then each individual node has an ID that corresponds to its NID.

It is also very convenient that the corresponding ID is an attribute of the article tag, and immediately after it follows the title of the node / section of the main page.

This allows these IDs to be used in the navigation to scroll to the main page. However, it must be constructed as a new content type that supports fields of type Entity Reference.

Adding these [paragraphs with ID] to a standard menu might be tricky.

@yorkshirepudding – There is a related issue report: github.com/backdrop/backdrop-issues/issues/4063

As mentioned there, it is possible to add a link which points to an ID in a standard menu, if it has a certain form, e.g. <front>#about or node/123#about.

I did something similar using Jquery, replacing when the page load
1. each menu item set attribute 'href' to '#sectiom-[number]',
2. adding the id="#sectiom-[number]" attribute to each block.
And when you click on a menu item, there is an animated scroll to id="#sectiom-[number]".

Literally 23 lines of code and you're done. 
Demo https://burger.wmetod.ru

In Backdrop, each block has a unique 'class', just in case. You can scroll to class name, adding to each menu item a 'data-attr' with the class of the target block, for example. It all depends on your imagination.

UPDATE: 'One-page navigation' simplifies the task, you just need to create an HTML menu with links like href="#my-block-id". 
You can override "block.tpl.php" so that all blocks are output with a unique "id".
'One-page navigation' is an easier way. I can give explanations if something is not clear.

Comments

I did something similar using Jquery, replacing when the page load
1. each menu item set attribute 'href' to '#sectiom-[number]',
2. adding the id="#sectiom-[number]" attribute to each block.
And when you click on a menu item, there is an animated scroll to id="#sectiom-[number]".

Literally 23 lines of code and you're done. 
Demo https://burger.wmetod.ru

In Backdrop, each block has a unique 'class', just in case. You can scroll to class name, adding to each menu item a 'data-attr' with the class of the target block, for example. It all depends on your imagination.

UPDATE: 'One-page navigation' simplifies the task, you just need to create an HTML menu with links like href="#my-block-id". 
You can override "block.tpl.php" so that all blocks are output with a unique "id".
'One-page navigation' is an easier way. I can give explanations if something is not clear.

Hi Enthusiast,

This is probably a very good method, but at the moment a bit too complex for me. I might take you up on your offer to explain further in the future.

Thanks!

Kind regards, Alex

Olafski's picture

Hi Alex, welcome to the Backdrop Forum! As far as I know, there isn't a standard solution to create attribute IDs for Backdrop elements. So, depending on your elements, there are different options, or workarounds. For example:

  • Create a custom block with text format "Raw HTML", put the ID in the text area, and place the block near your element.
  • Add a custom text element with your ID to the header of your views block (see screenshot).
     
  • I'm sure there are more workarounds.

    If I were to build a one-page website, I'd use a custom layout template and put my IDs in the HTML. (This will hopefully be easier in the future, if Backdrop allows to add custom IDs in flexible layout templates.)

     

One possibility is Paragraphs.  Added relatively recently (by me), each paragraph has a unique HTML ID that can be referenced. Paragraph types can include any type of field including block reference, entity reference displayed as a page. Adding these to a standard menu might be tricky.  I have built menus using these with a View though like a Table of contents at the top of a newsletter.  In theory, you could build a view using an unordered list, expose as a block in the header and then style it how you need it, but I haven't tried this combination myself.

Olafski's picture

Adding these [paragraphs with ID] to a standard menu might be tricky.

@yorkshirepudding – There is a related issue report: github.com/backdrop/backdrop-issues/issues/4063

As mentioned there, it is possible to add a link which points to an ID in a standard menu, if it has a certain form, e.g. <front>#about or node/123#about.

I guess the task can be solved using the Entity Reference module.

If the main page of the site is built as fields with nodes that are displayed in it by Entity Reference, then each individual node has an ID that corresponds to its NID.

It is also very convenient that the corresponding ID is an attribute of the article tag, and immediately after it follows the title of the node / section of the main page.

This allows these IDs to be used in the navigation to scroll to the main page. However, it must be constructed as a new content type that supports fields of type Entity Reference.

Thanks everyone! This was really helpful.

Regards, Alex