Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal Add Static link to a view

I have a Drupal view that lists a node called publication. At the top of the page I'd like to have a link 'new publication' instead of having it in the navigation block. Can somebody direct me on how to go about this? There seems to be several ways but I'm having little luck.

like image 219
slotishtype Avatar asked Nov 30 '09 14:11

slotishtype


People also ask

How do I add a link in Drupal?

Locate a menu to edit and click add link. In the Menu link title field, enter the title as it will be displayed in the menu. In the Link field, enter a path for the link. This can be an internal Drupal path such as /node/add (see "Before you begin" above) or an external URL such as http://example.com .


3 Answers

I also struggled a bit in getting the exact meaning of your question.

I understand your question like this: "I have various pages of node listing generated by my view, I would like to have on top of each of these pages a link that allows me to create a new node of the type listed in the views. How do I do it?"

If I got your question right, a possible method is to insert the link in the template for your view. In fact the link to create a new node of that type will be a static URL of the type

http://example.com/node/add/mynodetype

so you can safely put in your template file something like:

<?php print(l('Create a new mynodetype article!', 'node/add/mynodetype')) ?>

This is substantially equivalent to what Henrik proposed (+1) by modifying the header of your view. What method you choose will probably depend on if you consider this link more of a core feature of your view or more of a theming element. If you add it to the view, you will have it in all your themes (=feature). If you put it in the template, you will only have it for the theme the template is used by (=theming element). Of course - even if it is a theme element - it makes more sense to include it to the view if you use a standard template file, so you will not have to crate an entire template file just for a link.

From your comment below it seems that you are not already using a template for your view, and therefore - unless you need it - you could go with Henrik's suggestion of adding the information in the header of the view. I however prepared screenshots to illustrate both methods:

Adding to the header:

You have to click on "header", then insert the code in the textfield and set the input filter to "PHP" (Otherwise your code will be escaped and displayed instead of being run).

Screenshot of views UI

Creating a template:

You have to click on "information: theme" and then you will have a list of possible template names. The one in bold is the one currently used. You then have then to copy the file with the bold name (from the views directory) to your theme directory, and rename it in one of the proposed ways. Then you can enter the file and edit it adding the PHP code you need.

As stated before: if you do not need a template for anything else, then creating a theme only for this is overshooting, IMO.

Screenshot of views UI

like image 135
mac Avatar answered Oct 16 '22 11:10

mac


You can also use the following module: https://www.drupal.org/project/views_linkarea.

As stated in its description,

"By making it a real link rather than just a blob of HTML in a header text area it gets all of the special handling and clean URL friendliness and base-path portability that any Drupal link gets."

like image 27
murillobit Avatar answered Oct 16 '22 13:10

murillobit


Edit: Just realized that I probably misunderstood your question. You want a link on the top of every page, pointing to your view?


(Note: probably not what the OP wants, as it would put the link at the top of the view itself)

One of the easiest ways would be to put it in the 'header' section of the view (under 'basic settings' on your views configuration page). You can set a filter format for this, so make sure to use one that allows links.

like image 3
Henrik Opel Avatar answered Oct 16 '22 12:10

Henrik Opel