Require and place block in a drupal theme? - php

In a theme I'm creating I'm wanting to place blocks in locations that are different from the default ones. Eg place the search block in the header.
Is there any way to do this from a theme: Be able to require a block and place it somewhere?

The theming system of Drupal is flexible enough to turn exactly that part of the page into a region where you want your block to appear. For this you need to add the region to the theme's .info file as described on Assigning contents to regions, e.g regions[foobar] = Foobar. This will make a new variable $page['foobar'] available to your page.tpl.php. You can then assign content to the region the same way as with Drupal's builtin regions.
If you want to circumvent this system and display a block ad-hoc, have a look at how _block_render_blocks renders blocks.

It defeats the idea of configurable blocks but nevertheless it's possible: You have to create empty block templates for all but the allowed region. Or you provide/override the theme function for this very block and return nothing for prohibited regions.

In D7 you can add a placeholder region in your themes .info file, clear the cache and put your block in there you can then output the region's content.
print render(block_get_blocks_by_region('region_name'));

Related

Drupal: find footer code

I'm editing a Drupal that was created by a company who didn't follow Drupal best practices with the idea that only them can edit the theme. I'm having some problems with the footer and I would like to know where is the file that contains the footer html code, because in the theme I have donwloaded (Boostrap Drupal7) it shows a footer rendered by <?php print render($page['footer']); ?>
The problem I have is that the footer has a block and I don't want it, but in appearance the block is not assigned to the footer, and I had tried to remove it.
Where is the code called by this function?
First, login as admin and make sure what theme is been used, check that, here (see what is the default theme)
/admin/appearance/
Once you know what is the default theme, go to the directory and find the following template: page.tpl.php
/sites/all/themes/theme_name/templates/page.tpl.php
Find the code:
But, remember that you can find searching any string using any IDE like phpstorm or sublime. It's very simple using it.
Well, some of Brute-force approaches:
Do a grep of footer content in your theme folder. (sites/all/themes/xyz)
OR
Do a grep of footer content in your module(sites/all/modules) directory. In case, the developer has put custom module or put custom code in some module.
OR
Do a grep in your drupal root folder except sites folder.
OR
If you are able to find in which region your footer is being shown, Look in every block in that region (under admin/structure/block). Click on edit of every block in that region. See, if you are able to find the footer content.
OR
You can see each view (admin/structure/views)

Is it possible to hook a text box into a WordPress theme, via the functions.php file?

I am currently using a WordPress theme to create an ecommerce website. I am looking to create a horizontal text box, with 3 columns, to appear directly beneath the Main Menu navigation. Rather than edit the header.php file, and risk breaking the theme, is it possible to achieve this by hooking into the theme, via a functions.php file in the child theme or would thus be bad practice?
So adding "right after the main menu" is not something WordPress will be able to give you directly, no. Because, by definition, the structure of a page is a theme's responsibility.
That would be a perfect case for a child theme, and it would be my first choice. There you can safely override the index file (or header file, depending on how the theme is built) and add your html to it.
Another option a child theme might give you, is adding html through the theme's own filters and actions - but that will totally depend on your theme giving you such hooks.
Finally, if truly you want to add right after the main navigation, you might look at the wp_nav_menu filter: using that, it should be possible to first detect if you are looking at the main navigation or not, and if you are, append your own html. But frankly, I think the risk of breaking your layout is greater with that method.
Hope this helps!

Placing a Module to a Certain Block in Drupal 7

I'm new to drupal and familiar with Joomla and Wordpress. I'm little bit confused with how drupal works when placing some kind of module to a certain block in drupal 7. Say that I want to place the search box in a footer block of the theme, what are the steps for doing that?. Or may be is it depending on the theme how modules will be placed. Please guide...
Thanks
Drupal 7 have "Regions" , it's like a grid , you can add block in regions. You can add block via admin interface /admin/structure/block/add , you a part for regions who can receive your block.
Also if you need to create a custom block programmatically, you can follow this link https://drupal.stackexchange.com/questions/5582/easy-way-to-create-blocks-programmatically
Drupal uses blocks and they can be placed in regions. Regions are defined in your theme info file and printed from your theme page template.
You can place existing blocks to some regions at "Structure -> Blocks" admin page.
When you are on blocks page there is a table where every row is for a single block. You can place block to some region by dragging it to some region or by changing drop-down in it's Region column (don't forget to save after that).
Also, you can configure every block to be displayed by specific criteria (page, user role...). On same blocks page there is "configure" link for every block and on configure page there is "Visibility" section at bottom of edit page.
But you can use additional modules like Context to place blocks to region with more choices.

How to add a block in magento

I am working on a magento theme but stuck with adding social links in header.
I just want to put three Social links on to my site in header section of every page.
It will depend on the template your using.
The file you'll need to edit will more than likely be in /APP/DESIGN/Somehwere . The somewhere will depend on you the theme you're using.
To see which file you need to edit, turn on template hints: From your admin panel, go to System >> Configuration >> Developer
Depending on your setup, you may have to set it for your default view or a specific store view.
A quick tutorial of template hints is here : http://www.westwideweb.com/wp/2009/08/04/magento-how-to-turn-on-template-path-hints-and-inline-translation/
The base header template is located in:
/app/design/frontend/base/default/template/page/html/header.phtml
But it's not good practice to directly edit this file, while you can do so.
The proper way would be to make a new theme folder, setup your store to use the right theme within admin then to edit the header.phtml file from that theme, which will override the default base theme - eg:
/app/design/frontend/base/yourtheme/template/page/html/header.phtml
make template path hints on for your store view. System->Configuration->Developer. (You can use your specific IP in allowed IPs not to effect the view)
Disable the cache.
Refresh the home page.
You can view the file there, go to that file.
You can directly add it there or you can create a static block and use that in your phtml file according to your wish.

How to add PHP or Javascript code to a custom content type

I am working with Drupal 7.17.
I have a custom content type I created called Sidebar Ad (machine name sidebar_ad) that I want to add Javascript and PHP to. From what I understand, in the templates directory of the theme I am working with, I need to create a file called 'page--sidebar_ad.tpl.php' and I can put PHP and Javascript code in there, and it will only run in blocks and pages that contain the custom content type Sidebar Ad.
I am trying this out with one of the stock themes, Bartik, and it is not working for me. I tried it by copying the page.tpl.php file, renaming it page--sidebar_ad.tpl.php, and adding a bit of text in a few places - at the top of the php file, and in the content div. But I don't see anything change on the pages on my site that contain that custom content type.
Is there something I am missing?
Most likely the thing you're missing is flushing Drupal's cache...any time you add a hook or template file, you need to flush the cache so the various registries can pick it up.
In the case of an overridden page template file you also need a copy of the standard page.tpl.php file in your theme. Same goes for the node template and most likely others, you need the original in there for the overrides to work.

Categories