Category Archives: Drupal

Posts relacionados con DRUPAL.

Drupal 7: display language switcher in template [SOLVED]

Some days ago we learnt to display a search box in Drupal template files, and according to that post and the Drupal handbook rendering a language switcher block in your theme templates should be easy.

This should work:

<?php
$block = module_invoke('locale', 'block_view', 'language');
print render($block);
?>

Unfortunately, in my D7 it does not. What does work is this:

Add to template.php:

<?php
function block_render($module, $block_id) {
  $block = block_load($module, $block_id);
  $block_content = _block_render_blocks(array($block));
  $build = _block_get_renderable_array($block_content);
  $block_rendered = drupal_render($build);
  return $block_rendered;
}
?>

Add to page.tpl.php

<?php
  print block_render('locale', 'language');
?>

Now that the language switcher is rendering, you might want to customize its appeareance ;)

Drupal 7: Styling language switcher

Language switcher is a Drupal module which allows to display a block that allows visitors to choose between the languages in which your site is available (check /admin/config/regional/language to list the available languages in your Drupal site).

By default, it produces an unordered list with each item in a new line and a bullet before each item. Not a big fan of this look.

With the following CSS code you’ll have an inline language switcher, with li items separed by “/”.

/* language switcher */
ul.language-switcher-locale-url{
list-style: none;
display: inline;
}
 
ul.language-switcher-locale-url li{
display: inline;
}
 
ul.language-switcher-locale-url li:after{
   content: "/";
}
 
ul.language-switcher-locale-url li:last-child:after{
   content: "";
}

For more advanced styling options, check this link.

Drupal 7: Path breadcrumbs in Views page [SOLVED]

Path breadcrumbs module allows you to easily add breadcrumbs to your Drupal site. The module lacks of documentation, but they offer this image as an example:

path_breadcrumbs

And there is a videotutorial as well.

The above show how to configure breadcrumbs in nodes, but… how to display breadcrumbs in Views pages?

This struggled me for a while, because I was taking the (limited) instructions too literally.

To create a breadcrumb to a views page display that has a path like “/path/to/my-view” just use that as the path (as it isn’t an alias). To know the path, go to your view and edit it (admin/structure/views/view/VIEWNAME/edit) and refer to the Page Settings – Path value.

Also, leave the “Arguments” and “Selection rules” empty, then setup your breadcrumbs as you did with nodes.

Let’s see an example. First, the path to the view:

Path breadcrumbs in views

Path breadcrumbs in views

And the path breadcrumbs configuration:

drupal 7 Path breadcrumbs in views

drupal 7 Path breadcrumbs in views

And here is the resulting breadcrumb. Remember that your theme’s page.tpl.php must print $breadcrumb or you will not see breadcrumbs at all…

path_breadcrumbs_in_views_page_2

Drupal 7 “500 – Internal server error” [SOLVED]

When you want to install a new Drupal 7 in a shared server or some web hosts, specially those concerned with security settings, there is a good chance that all you get is a “500 internal server error”. This is usually due to the .htaccess file included with the installation, and more precisely to these lines:

# Follow symbolic links in this directory.
Options +FollowSymLinks

Some web hosts tighten up security settings and now forbid +FollowSymLinks option in .htaccess. This causes an 500 error when accesing the site.

The fix is quite simple. Just replace +FollowSymLinks with +SymLinksIfOwnerMatch. So edit the lines above to this:

# Follow symbolic links in this directory.
# comment this one...
# Options +FollowSymLinks
# add this new line...
Options +SymLinksIfOwnerMatch

The issue should be fixed.

Please note that there is not just one .htacess file in your Drupal 7 site. There is one in / and usually there are other .htaccess files that must be modified too, for instance in sites/default/files. You must change that one too or every image that you upload to your Drupal 7 site won’t be rendered…

Discussion about this topic available here: http://drupal.org/node/1269780

Drupal 7: Display search box in page.tpl.php or other template files [SOLVED]

Let’s assume you want to display a search box in your Drupal 7 theme. You can do it several ways: rendering the search form, rendering a block which contains it, using template.php or page.tpl.php.

Thirty-second intro to Drupal theming

If you are new to Drupal, let me explain how the themes work.

Each theme is a directory (under sites/all/themes/yourthemename) with at least three files:

* The first one, the .info file, called yourthemename.info, describes the theme (regions, path to stylesheet, javascript files, blablabla).

* The second one, which contains the logic (php code) is called template.php, and

* the third one is the html markup file (ideally, with just some php print’s) called page.tpl.php.

The concept is quite simple: you generate some variables in template.php and print them in page.tpl.php.

Now that we understand the basics, you will find this examples quite self-explaining:

The dirty way: display Drupal form in page.tpl.php

First way to show a search box in your Drupal theme works as expected, but it is not the way you are supposed to implement it.

This adds the logic to show a search form directly in your View file (page.tpl.php).

This practice should be avoided, as there must not be any logic inside .tpl.php files… Anyway, Drupal allows it, so:

Edit page.tpl.php (in your theme folder, in /sites/default/MYTHEME) and add these two lines, which will render a block containing a search form:

<?php
// this is page.tpl.php
 
// insert a search_form here...
$block = module_invoke('search','block_view','form');
//  'search' is the module invoking the block. 
//  'block_view' is the operation we want to call, that is, we want to view the block, and 
//  'form' is the machine name of the block you want to print.
 
// You can get this information by hovering over the edit link on the block overview page at admin/structure/block
// Edit links will be in the form
//       /admin/structure/block/manage/MODULENAME/BLOCK_MACHINENAME/configure
 
 
// now we print the block...
print render($block);
?>

You can read more about module_invoke and render function in drupal documentation.

The right way: show a search block in your Drupal 7 theme

We can set a variable containing a search block in template.php:

<?php
// this is template.php, where the logic goes...
// replace MYTHEME with your theme name (like the theme folder containing the theme)
function MYTHEME_preprocess_page(&$variables)
{
    $block = module_invoke('search','block_view','search');
    $rendered_block = render($block);
    $variables['mysearchblock'] = $rendered_block;
}
?>

And then print that variable in your page.tpl.php file:

<?php
// this is page.tpl.php, where the presentation (view) is generated:
   // ... custom html stuff ...
 
   // print our rendered search block...
   print $mysearchblock;
?>

The right way: display Drupal 7 search form in theme

Instead of rendering the block, we will be rendering the form (not a block, but the full search form instead):

Let’s put the logic where it should be, in template.php:

<?php
// this is template.php, where the logic goes...
// replace MYTHEME with your theme name 
function MYTHEME_preprocess_page(&$variables)
{
    $search_box = drupal_render(drupal_get_form('search_form'));
    $variables['my_search_box'] = $search_box; 
}
?>

And then, in our page.tpl.php, just print that $my_search_box variable we just set earlier…

<php
// this is page.tpl.php
print $my_search_box
?>

If done like so, it will work and render the search form, but it will spit out something like
Strict warning: Only variables should be passed by reference in

This has to be with variables being passed as reference, and not creating an instance for those first. Read this for further information.

So, let’s fix it.

In template.php:

<?php
// this is template.php
 
function MYTHEME_preprocess_page(&$variables){
  // para poder usar una caja de búsqueda en los templates .tpl.php de Drupal 7...	
  $form = drupal_get_form('search_form');
  $cajabusqueda = drupal_render($form);
  $variables['cajabusqueda'] = $cajabusqueda;   
}?>

Drupal 7: Pathauto url pattern page is empty [SOLVED]

If you install Pathauto 7.x-1.2 in the last Drupal stable version (7.21) and try to define some new url patterns for your content type’s (under ?q=admin/config/search/path/patterns or Administration > Search and metadata > Url aliases > Pattern tab) it is likely that an empty “ugly” page shows up…

This was driving me nuts but I finally found how to make it work.

Scenario:
Drupal 7.21
Token 7.x-1.5
Pathauto 7.x-1.2
Admin theme: Seven

The fix
It seems that Seven theme is responsible for this. This theme has some issues with js/jQuery and won’t show (out of the box) the Pathauto pattern’s.

To fix it, just go to Appearance, scroll down to the bottom of the page and select “Bartik” or “Garland” instead of “Seven“.

Now save, and reload ?q=admin/config/search/path/patterns. The pattern tab info should be nicely displayed now.

If it works, you can try to activate Seven theme for admin again. In my case, it worked.

*** UPDATE 20130427***
If the steps above won’t fix the issue, then edit your php.ini field and try to increase memory_limit to a higher value. For instance, in my case I changed from:

memory_limit = 32M

To:

memory_limit = 64M

After updating php.ini do not forget to restart your web server!

Further information: http://drupal.org/node/1267966

Drupal not showing contact form menu item [solved]

Drupal is not showing the contact form menu item?

Then you should check:

  1. Whether the menu item is enabled (admin/build/menu/): enable it, if it is disabled
  2. Whether the menu item is showing when the user is logged in: if you do the above, it will
  3. Allow annonymous user (not logged) to ‘access site-wide contact form’ (admin/user/permissions/)

Drupal: Override node.tpl.php using Zen theme [solved]

If you are familiar to drupal zen theme then you will know how to create zen subtheme’s. But, what if you want to override the drupal’s default node.tpl.php?

First of all, you should install the theme developer module and then check what file you must replace. In this example we will override the /sites/all/themes/zen/modules/node.tpl.php with my custom code for theming a CCK node (content type=’product’). So, follow these steps (the subtheme name is called ‘mysubtheme’)

  1. Copy sites/all/themes/zen/modules/node.tpl.php to sites/all/themes/mysubtheme/modules/node.tpl.php
  2. Copy sites/all/themes/zen/modules/node.tpl.php to sites/all/themes/mysubtheme/modules/node-product.tpl.php

Please note that you have to also copy node.tpl.php to your mysubtheme subtheme modules folder. If the node.tpl.php file from zen is not copied into the sub-theme then the content type variants i.e. node-[content-type].tpl.php are not seen. Once the node.tpl.php is copied to the sub theme then all is good :) (took me a while to figure this out!)

Translate “Contact form” module Drupal [SOLVED]

The crappy way

Delete the “additional information” text in admin/build/contact/settings

The easy way

Just download tContact module, and add these lines to your settings.php file.

$conf['i18n_variables'][] = 'contact_form_information';
$conf['i18n_variables'][] = 'contactforms_information';

The hard way

After hours of research, I’ve finally come up with a default core solution that I’d like to share with everyone on how to translate the Contact form module that comes with Drupal in Drupal 6.x.

Basic Instructions:

Make sure your Contact form module is enabled first, of course.

Go to drupal\modules\contact and open the file contact.pages.inc.
Go to line 41.

Replace the code…

$form['contact_information'] = array(‘#value’ => filter_xss_admin(variable_get(‘contact_form_information’, t(‘You can leave a message using the contact form below.’))));

…with:

$form['contact_information'] = array(‘#value’ => t(filter_xss_admin(variable_get(‘contact_form_information’, ‘You can leave a message using the contact form below.’))));

Once replaced, save the page.
Next, install the module String Overrides.
Once installed, go to the String Overrides main page on your Drupal site under admin/settings/stringoverrides and click on the preferred language you would like to translate your contact form text to.
Make sure you originally replace “Contact” with the appropriate translation for “Contact” under your preferred language, and also for the text “You can leave a message using the contact form below.” Keep in mind that if you have changed that text, then you should translate the text that you changed it to instead.

After all this has been done, you’ll have a sweet translated contact form.

Refer to this link and this other for further details.

Drupal: customize language switcher block

If you are runnning a drupal multilingual site, you might be using i18n with language icons modules.

I’ve received some mails asking how to customize the aspect of the multilang switcher block. Easy!

Log in as admin and navigate to /admin/settings/language/icons
There you will be able to
1) customize the Icon placement (before link (after link) means that the language icon will be placed before (after) the language text, or just replace link which means no text will be displayed, just the language icon ).
2) customize the path to the language icons (default is sites/default/modules/languageicons/flags/*.png
3) customize the size of language icons (default is 16×12)

But you can also customize the way in which those icons are shown. For instance, you can change from vertical list to horizontal list by editing your CSS file and

.block-locale-0 li{
	display:inline;
	list-style-image: none;
}

In my drupal, the language icons are showing in the sidebar, so for displaying an horizontal list, I set:

.sidebar ul li{
/*border-top:1px solid #979ea8;*/
border:0px;
margin-left:5px;
margin-right:5px;
padding-top:6px;
padding-bottom:6px;
list-style-type: none;
list-style-image: none;
/* para que los language icons salgan en horizontal */
display:inline;
}

For not showing the block’s title (inside an h2):

.sidebar h2{
display:none;
}