Archive for the Drupal Category

If you are developing a module for Drupal you might need to use tabs to simplify a bit a screen which has too much functionality. Typically a settings page that might need too many options to comfortably fit on a single screen. Since I had to search and experiment a bit to find out how to do it I decided to write a short post with my findings. In this article I assume you have a basic understanding on writing Drupal modules.

(more…)

Drupal has a sophisticated module system that allows developers to extend the CMSs functionality very easily. Each module “hooks” into Drupal by implementing hook functions. On way to use this feature is to hook different URLs to module functions. Essentially the developer in his module declares an array of url paths and what function should be called when that path is requested. This small post will show you how you can determine which module handles a specific URL. This can be quite useful for debugging and/or browsing other peoples source code to learn how to do something.

Every module in Drupal that hooks to a URL path needs to implement the hook_menu function. This hook basically tells Drupal what paths will be handled by this module. This information is read when the module is enabled and stored in a database table. In Drupal 6 this information is cached so if you modify the array returned by the hook_menu function you need to clear the cache. To determine what module is handling what path you simply need to query the menu_router table. A simple query to do this is:

SELECT
  path
  , file
  , page_callback
  , type
  , title
FROM
  menu_router m
ORDER BY
  path

This table has more information that might be useful: access_callback and access_arguments store information related to what access function is called to determine who has access to that path, page_arguments stores information related to what arguments to pass to the callback function. This is specially relevant when the page_callback function is drupal_get_form in which case Drupal will render a form and this field stores the name of the form to render.

An example usage might be:

$ drush sql cli
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1859
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SELECT path, file, page_callback FROM menu_router ORDER BY path;

+-----------------------------------+---------------------------------+------------------------------+
| path                              | file                            | page_callback                |
+-----------------------------------+---------------------------------+------------------------------+
| admin                             | modules/system/system.admin.inc | system_main_admin_page       |
| admin/build                       | modules/system/system.admin.inc | system_admin_menu_block_page |
| admin/build/block                 | modules/block/block.admin.inc   | block_admin_display          |
| admin/build/block/add             | modules/block/block.admin.inc   | drupal_get_form              |
| admin/build/block/configure       | modules/block/block.admin.inc   | drupal_get_form              |
| admin/build/block/delete          | modules/block/block.admin.inc   | drupal_get_form              |
| admin/build/block/list            | modules/block/block.admin.inc   | block_admin_display          |
| admin/build/block/list/bluemarine | modules/block/block.admin.inc   | block_admin_display          |
| admin/build/block/list/chameleon  | modules/block/block.admin.inc   | block_admin_display          |
| admin/build/block/list/donline    | modules/block/block.admin.inc   | block_admin_display          |
  .....

mysql>

Cuando generamos un View, y queremos darle un formato en particular tenemos que agregar una llamada desde nuestro templates a la función views_embed_view, pasando por parámetros el nombre de la vista, el display_id.

<?php echo views_embed_view( 'nombre_vista' , $display_id = 'default' ); ?>

Cuando hacemos esta llamada nos genera un html con el contenido de la vista. Si queremos darle un formato html especial tenemos que generar un archivo con el nombre view_view–nombre_vista.tpl.phpA esta función llega un objeto con el nombre $view->result, que contiene todo el contenido de nuestra vista.

Por ejemplo si quisieramos mostrar el contenido de la vista en una listado con el title y el teaser, el código seria el siguiente.

<ul class="destacados_libros">
  <?php if (!empty($title)): ?>
  <li class="top_titulo"><?php print $title; ?></li>
  <?php endif; ?>
  <?php foreach ($view->result as $row): ?>
    <li><?php echo $row->node_revisions_teaser; ?></li>
    <li><?php echo $row->node_title;?></li>
  <?php endforeach; ?>
</ul>

While working on the administration section of a medium to large site, you may need to allow different roles to create and edit different content. Typically you want Role X to be able to create and view some content types only. The creation part is easy: go to the Access control section and give Role X the Create and Edit permission for the content types you want this role to be able to create and edit. The next thing you’ll want to do is give the role a way to list all nodes for the content type it is allowed to view/edit and be able to administer these nodes. The logical thing would be to give the role access to the admin/content/node by giving this role the administer nodes permission. The problem is that the filter for this screen will show you nodes of all types regardless of what the role should be able to edit! This hack will guide you to another trick: creating a views page for Role X to be able to administer the nodes he should have access to. Read on for the rest of this hack.

A page to edit and create content types

 

(more…)

A missing feature in Drupal is the ability to preview changes made to the home page. If you have a custom page as your home and you promote several pieces of content to it there is no way for you to preview how it will look. If the content breaks the layout or doesn’t look as good as you expect it to do then you’ll have to make several changes to fine-tune your home. Wouldn’t it be great if you could get a preview of your home and when you are satisfied with the all the changes hit a publish button to see all your changes commit at once? Well you can! Read on to see how.

(more…)

A problem which often arises when writing Drupal themes, modules, etc. is making sure there are no ‘hard-coded’ paths in our code. To help new users on their quest to writing quality Drupal code I list here the most used API functions to get paths in a portable way:

  • global $base_url - A global variable which stores your sites URL, typically http://www.<your_site>.com. This variable is set in your settings.php configuration file. This is not very portable and maybe in the future Drupal has a function for this.
  • base_path() - Returns the base URL path of the Drupal installation. At the very least, this will always default to /. This is used, for example when you want to add a resource relative to your base_url. For example like so: drupal_set_html_head(’<style type=”text/css” media=”all”>@import “‘. base_path() . drupal_get_path(’module’, ’system’) .’/system.css”;</style>’);
  • conf_path() - Based on your sites URL tries to find the most appropiate settings.php (site configuration file). To read how it works see Drupals API documentation
  • drupal_get_path($type, $name) - Returns the path to a system item (module, theme, etc.). The $type argument can be ‘theme’, ‘theme_engine’ or ‘module’ and $name the name of the item for which you want the path. To get an absolute path you need to prefix the $DOCUMENT_ROOT (the document root of your drupal installation).
  • path_to_theme() - This points to your theme’s path on the local filesystem. Usually $DOCUMENT_ROOT/themes/<your_theme> or $DOCUMENT_ROOT/sites/<your_site>/themes/<your_theme>
  • path_to_engine() - Returns the path to the currently selected engine.

To learn a bit more search for this terms in the API search box or ‘use the source, Luke!’.

At Easytech we are using Drupal for projects which require a CMS (Content Management System). It’s very powerful, full of modules to extend the functionality and written in PHP which is a Language for which we have a lot of programming experience. For a project we are working on we used the Related Content module. This module allows you to link a particular piece of content (a node in Drupal parlance) to other content such as images, videos, audios, etc. One problem with the current implementation of the module is that it wasn’t designed to work when you have lots of content on the site. The patch we currently submitted to Drupal fixes this by separating the Related Content into a tab and adding a filter to be able to quickly locate content items. Here’s a screenshot:

This is how the new features looks like

If you are interested in getting the patch its here. In case you want to download a patched version of the module I have included a tar file with the latest version as an attachment in this posting. Try it out and send feedback to: miquel <at> easytech <dot> com <dot> ar.

Here’s a tar file with the patched version to try out: relatedcontent-5.x-1.1-patch.tar.gz. To install it just download it and untar it in the modules/contrib directory of your Drupal installation.