ShareThis button directly in Drupal's tpl.php files - drupal

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...

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...

Drupal page content

I'm new to drupal and reading through docs, but hoping to get an explanation for something.
I have a page called page--type-home-page.tpl.php.
Part of this page prints render($page['content'])). I want to remove something that is rendered as part of the page content from the page, but don't understand where this comes from and where/how to look.
Thanks!
Assuming you're talking about Drupal-7
Well the $page['content'] contains a string, which is a rendered version of what's injected into the content region of your theme.
By default, the only block in this region is the "Main page content" block that is generated by the Drupal core. Many things can generate this content but it always pass through the menu API. For instance, if you're viewing a node, the URL used is: node/12. The node module declares a menu entry for node/%node, this menu entry contains a callback function that will render whatever the module wants to render. The module, then, may use different strategy to render it's content from a simple function to a complex imbrication of templates.
The key to alter what's in the box, sorry, what's in the $page['content'], is to know what is rendered and to understand how it's rendered.
If it's a node, first you want to look if you can achieve your goal through the display settings of the content type. admin/structure/types/manage/page/display: And this is true for all entities (users, comments, taxonomy term etc.) Because this is the first thing the module of these entities will put together when they'll try to render your content.
If this is not enough to achieve your goal, you can look into the module that renders the path to see if it hasn't a .tpl.php. You'll be able to re-use it in your theme. You'll want to copy/paste the file in your theme and edit it.
If the module do not have a tpl file to override, try a template suggestion: here's a list from Drupal.org
Ex: node--type.tpl.php
If all this doesn't satisfy your need, you'll have to dig into preprocess functions; Those functions allow you to modify what's in the variables passed to .tpl.php files. That's a little more advanced and I recommend you to read this previous stackoverflow question/answer
Simply, don't use that $page['content'] which prints all content, but place your custom template code instead and print separate field values where you need them like:
<?php print render($content['your_field_name']); ?>
https://drupal.stackexchange.com/questions/30063/how-to-print-fields-in-node-tpl-php
If you want to do just simple styling, like excluding some field you can use content type display options like mgadrat explained, but if you want to use some complex styling, with totally custom html this solution is easier.

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 Views2 Exposed Form

A while back you commented on a poster's question for how to alter the Drupal Views2 Exposed Form. In the post you mentioned:
The proper way of changing the theme would be to override the views-exposed-form.tpl file in your theme's folder. Bear in mind that this will apply to all exposed filter forms, to theme a specific one, you will need to use a different name for that filename, like:
views-exposed-form--TITLE--DISPLAY.tpl.php views-exposed-form--TITLE.tpl.php
and some others, you can check the Theme: Information section of your views for template naming conventions.
Can you expand on what the 'TITLE' and 'DISPLAY' are exactly? Are both template files needed or just one of them?
Thanks.
I am not the author of the post you are referring to, but I can answer this question.
The way Views theming works is that there is a base template for a specific part of the output and this base template can be made more specific by adding additional qualifiers to it.
If you copied /sites/all/modules/views/theme/views-exposed-form.tpl.php to your theme folder (or from wherever you have Views installed), made changes to the copy, and then edited your view and chose Theme: Information and clicked Rescan Template Files, Views would now use this template in your theme folder for rendering the exposed filters form. This is the base template name and so it will be used for every view's exposed filters form.
If the change you're making should be applied to every exposed filters form then you're done. However you probably want to limit this overriden template file to specific views or even specific displays of specific views. If you want to apply the template only to a view named testview then you could rename your theme's views-exposed-form.tpl.php to views-exposed-form--testview.tpl.php. Don't forget to use the Rescan Template Files button after each rename or new file. Now that template only applies to the view testview and all of its displays.
A view can have multiple displays - maybe a block, a page, a page for admins, etc. These are listed vertically down the left side of the edit page for a view. You can get more specific and target a particular display of a particular view. To do this, you might name this template file views-exposed-form--testview--page-1.tpl.php which would cause it only to be used for the testview view and only for the first page display of that view. You can find the display name by hovering over the tab - it will appear at the end of the URL your browser shows in the status (default, page_1, etc). Just remember to turn underscores to hyphens in the template filenames: use page-1 not page_1.
You can also target all pages for example: views-exposed-form--testview--page.tpl.php.
These naming conventions as well as a list of templates which can be overriden are available here: http://views.doc.logrus.com/ (click Views Template Files).
So, if you have a specific display or view where you want to override the default template then you only need a single file targeted for that combination. If you need to target additional views or display combinations, you will have more files.
You can try Views exposed form layout module for views exposed form theming.

Is it possible to add buttons (link) to a view in 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.

Resources