Is it possible to add buttons (link) to a view in Drupal? - drupal

I am looking to add separate buttons that will be displayed at the top of the view that I am creating in Drupal.
So far all the fields that I have added appear inside of the table as they should. The only thing that shows outside of the table is the search filter above (as it should).
I would like to add 3 buttons(links) in a row that are positioned above the table and below the search area.
Is there any way to do this in Views? If so could someone describe the process? Thanks!

The most straightforward way to do this is through the theme layer. Edit the view, and go into the Header option under Basic settings. There, you can put in your custom HTML for the buttons.
However, as you might've noticed, this places the header above the exposed filters, whereas you want the header below them. To do this, go into the Theme information option under Basic settings, and find the Display output section.
There, you'll see a list of file name hints. Create a file within your theme using one of those filename hints. Then, click on Display output: you'll receive the default code you'll need to put within your newly created template file.
You'll notice in this new template of yours that the $header code block is before the $exposed code block. Just switch the positions and save your template. Back on the view configuration page, click on Rescan template files, save your view, and the header you've added will now be below the exposed filters.

Related

Embedding the view of a url inside the header of another view

Currently, I have a dynamically created custom pie chart located at mywebsite.com/customchart that is created through a module I made using Highcharts.
I have view, that is listed in the views module shown in mywebsite.com/admin/structure/views/view/ which I would like to embed the custom chart into but I cannot find it through Add Header>Global: View area.
How exactly would one be able to achieve this? Programatically? How?
You can do it programmatically.
First you have to find out what template file is used for rendering the header. Easiest way: go editing your view, select proper display and then unfold "Advanced" options block on the right column and click "Theme: information". Here you can see what templates files are used (assuming you are using Drupal 7). The used ones are in bold. You can collect actual template code there so you don't have to find template file at all if you want to override it. And you can see file naming suggestions, so there you have everything you need to override template file.
If your theme is not overriding standard templates then you should do that - it's not good idea to change directly templates provided by Views module.
So, the first part would be finding out where would you like to embed the view, at what template and at what position. Second part is using function views_embed_view() for doing that:
https://api.drupal.org/api/views/views.module/function/views_embed_view/7.x-3.x
As you can see there, first parameter is machine name of the view, second of display and then you can pass parameters if your view accepts them. So call that function from proper place and you'll have your view embedded.
Also, there is display type "embed" which you can use for embedding views like this. It will provide those "edit view" links when you hover your view content while logged in as admin...

ShareThis button directly in Drupal's tpl.php files

I only need to show custom button for sharing on nodes, but I need to put a different button on a lot of places (block quote, view result, image in node...) and to share just that part (probably with anchor tag suffix in the url). When the user clicks on the button, the modal/popup of ShareThis should be shown.
Is there any way to implement this directly into tpl.php file?
There is an example of the similar implementation of AddThis, something like this I need from ShareThis:
<div class="addthis_sharing_toolbox" data-url="THE URL" data-title="THE TITLE"></div>
It is not necessary for me to use the module, so any idea or possible aproach to this is welcome.
Looking at the following link: http://support.sharethis.com/customer/portal/articles/464452-integration-with-a-website you could add the javascript to the html.tpl.php file and the class in the node.tpl.php file or a custom block.
You can add any kind of code to template files of course. If it's just for specific content type add it to node template. And if it should be added for all (or many) content types (outer template) add it to page.tpl.php...or most outer if you want to place some content to i.e. page header - html.tpl.php
There you can put some if statement to i.e. display it on every node page except some...

Custom spaces editable from back office in Drupal

I've got a website in Drupal where in some parts of the site I need some editable spaces to put some banners and edit them from backoffice, (something similar to placeholders in Django).
Is there any module or a way to do this?
Thank you!
There is the block module, which is part of core. Are you using Drupal 6/7/8?
Anyway go into your admin section and look under structure, for blocks.
You can create custom blocks and place them in a region. If you want different regions then you can define these in your themes .info file.
Inside the blocks you can put whatever content you like.
Than you for your help , this did the job:
< ? php
 $block = module_invoke('block', 'block', 'view', "23(block number)"); 
 print $block['content']; 
? >
So, to create a custom (static) block you should go to "Blocks" admin page (Structure -> Blocks) and click "add block page" link. When creating the block pay attention on input format field (bellow "block body" field). If you use some format with filters drupal can (partially) destroy your block code - filter out some content. So, if you are sure that code you are using is safe and don't want it to be changed by drupal select "raw" format.
Every theme has defined regions that it supports. They are defined in:
/sites/all/themes/theme-name/theme-name.info file
So, if you want to change page template and print blocks on your own you must use some existing regions, defined there (it's needed to clear the cache after changing this file).
Region names can also be seen in Blocks admin page mentioned above. On that page you'll see the list of all available blocks and next to block name you'll see drop-down for placing block into some region.
Also, if you edit some block (click "configure" link next to that drop-down) you'll have some extra options to set block visibility (per user, per user role, per page..).
For more advanced block placement use "Context" module which have more powerful features for placing blocks and more:
https://www.drupal.org/project/context

Drupal 7 Theming a view

I've created a views called event which lists all 'event' content type pages, however I want to style the list by adding a "read more" button as well as other styles. I have created a blank template called views-view--event.tpl.php and I can see now that this is used because the view no longer lists the pages, instead it is blank (as expected). However, how do I now add the pages back in? I.e what php variables etc can I access from here? I've tried to echo a simple $title but this doesn't work. Is there a global variable that I can access? If so, what is it? I simply want to replicate what the view was doing before I created the template but with the ability to style it how I wish.
Go to your view display and look at the bottom of the "advanced" column. You'll see something like "Theme : Information". Try to locate your template name and click on it to get it's base code.

Drupal theming a row in a view's display

I'm trying to theme a single "display" in a view. What I'm trying to achieve is to have some control over how the row is rendered (eg. print the fields in the row, and maybe add some text in between. along with some divs for styling purpose). I've looked for days for the template to edit. I've used even the Theme Developer and the View's Theme Information, but no luck. All
My view's name is Calendar, and there are a bunch of displays. Could anyone tell me what I should do in order to customize the rows in this particular display? Does drupal even provide this flexibility?
The Name and Title of my display in the Calendar view is:
Name: List view
Title: List
Here's the theme information for row style:
Row style output: views-view-fields.tpl.php, views-view-fields--calendar.tpl.php, views-view-fields--page.tpl.php, views-view-fields--calendar--page.tpl.php, views-view-fields--calendar.tpl.php, views-view-fields--page-1.tpl.php, views-view-fields--calendar--page-1.tpl.php
Could someone tell me how to
Edit:
(Some additional information the OP posted in an answer)
I'm using Drupal 6. I'm not sure what page-1 indicates here The following is the list of display I have in this view:
Defaults
Calendar page
Calendar block
Month view
Week view
Block view
Upcoming
List view
Ongoing Block Feed
As you see, none of which has the string "page-1". I edited views-view-list.tpl.php, one of the file listed for "style output". The only variable I have access to is rows, which is an array containing all individual rendered "row". Like I mentioned before, the rows are generated somewhere else, and already contain the rendered fields. How can I customize these rows?
You shouldn't have to add anything to template.php if you are using Drupal 6.
The list of possible theme files that you have there goes in order from least to most specific. For instance, the first one, views-view-fields.tpl.php, will apply to all fields of all views on your site. The last one, views-view-fields--calendar--page-1.tpl.php, will only apply to the "page 1" display of the "calendar" view, so I'm guessing that's the one you want to overwrite.
When you click on Theme Information in the view, the display provided ("page 1" or whatever) applies to the display you are currently on. So if you want to theme fields in a certain display (the "blocky block" display of the "calendar" view, for instance), then you would click the "blocky block" display in the vertical menu on the left, then click Theme Information for that display.
So once you figure out which one you want to use, make a new file in your theme directory with that name, then copy of the contents of the current theme file (click the name of the theme files section on the Theme Information page) into your new theme file, then edit away. Remember, if you overwrite a theme file, you have to click "Rescan template files" in the Theme Information page for the new file to take effect.
Hope that helps!
The displays have custom names on the surface only, but internally they are referenced by id. 'page_1' will be your first display of type 'page' (blocks will become 'block_[id]', feeds will become 'feed_[id]' and so on).
So if you posted your displays in order, 'page_1' would be the 'Calendar' display. You can check this by looking at the links under the display entries in the display listing on the right of the views config page. They will have anchors named '#views-tab-page_1', '#views-tab-block_1' and so on.
As for overriding the templates, be aware that if you add a new *.tpl.php file, you need to reset the theme registry for it to be 'picked up' by Drupal.
Have you added the custom code needed in the template.php file? I know in drupal 5 you need to put code in 3 files:
template.php
views-views-fields--[name].tpl.php
views-views-fields--[name].css
Also you might try:
http://drupal.org/node/128741
answering the second question: the style output is not the template you're looking for. row style is the template that edits how fields are arranged within the row. if you want to edit how a particular field outputs its content, then the specific field template should be edited.

Resources