Category Archives: Informática – Web

Utilities, cheats and solved issues regarding wordpress, drupal and joomla, as well as other python and php programs to make your life easier.

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

WordPress: smooth slider and qTranslate [solved]

WordPress extension Smooth Slider is a great choice for implementing a post+thumbnail slider in your site.

However, by default it does not work with qTranslate.

If you want smooth slider to show the post in several languages (the ones you defined used qTranslate), edit /wp-content/plugins/smooth-slider/slider_versions/j.php.

Change from (around line 50):

$rs_post = $wpdb->get_results("SELECT * FROM $posts_table WHERE ID = $id", OBJECT);
$data = $rs_post[0];
$post_title = stripslashes($data->post_title);
$post_title = str_replace('"', '', $post_title);
$slider_content = $data->post_content;

To:

$rs_post = $wpdb->get_results("SELECT * FROM $posts_table WHERE ID = $id", OBJECT);
$data = $rs_post[0];
 
// make multilang titles with qtranslate
$post_title = stripslashes(qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($data->post_title));
$post_title = str_replace('"', '', $post_title);
// make multilang excerpts(contents) with qtranslate
$slider_content = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($data->post_content);

And if your permalinks are not working, use the qtrans_convertURL($permalink) to convert permalinks too.

Sustituir caracteres en tablas MySQL [RESUELTO]

En ocasiones es necesario realizar sustituciones de caracteres en tablas MySQL.

Por ejemplo, para reemplazar todas las apariciones de “foo” por “bar” en la tabla nombretabla, hacemos:

UPDATE nombretabla SET campo = REPLACE(campo, "foo", "bar");

Esto es especialmente útil cuando queremos arreglar problemas con tildes…

UPDATE email_templates_data SET subject = REPLACE(subject, "&oacute;", "ó");

Get CKEditor textarea content using javascript [SOLVED]

There is this useful getData() function to retrieve the contents of a ckEDITOR text area element:

If you insert a ckEDITOR called ‘my_editor‘ then you can read the value that the user typed inside the editor’s textarea using this JS

       var editorText = CKEDITOR.instances.my_editor.getData();

Hope it helps! :)

WordPress: WP Category post list and qTranslate issues [SOLVED]

WP Category post list and qTranslate are two of the plugins I usually use when developing websites with WordPress.

When using both of them together, all different post titles are concatenated to one big title (which contains the titles in all languages concatenated).

To fix this, simply edit wp-content/plugins/wp-category-posts-list/includes/wp_cpl_output_get.php and change from:

/** Add up the actual permalink and post title */
                $post_output .= '<a href="' . get_permalink($post->ID) . '" title="' . __('Permalink to: ', 'wp-cat-list-itg') . $post->post_title . '" target="' . ((false == $op['open_in'])? '_blank' : '_self') . '">' . $post->post_title . '</a>';

to

if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {
				$post->post_title = esc_html(qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($post->post_title));
			}
 
                /** Add up the actual permalink and post title */
                $post_output .= '<a href="' . get_permalink($post->ID) . '" title="' . __('Permalink to: ', 'wp-cat-list-itg') . $post->post_title . '" target="' . ((false == $op['open_in'])? '_blank' : '_self') . '">' . $post->post_title . '</a>';

And your titles will be displayed correctly in each language :)