drupal

Drupal developer links of the week (01-02-2013)

Some interesting links that I have found this week.

Problems in which i've been involved this week:

Debug Cron white screen in drupal

Debugging cron issues and errors is one of the most tricky things in Drupal. If you are dealing with a big site, with people adding nodes, inserting content which you can't allways force to be commited in the right way, and things like that, it is not unusual to find a cron broken by some "misterious" node.

Well, it is quite easy to find this "misterious" offender, just logging the cron activity. How, very easy, with my new module "cron_killing_finder".

It is published in my "Drupal SandBox": http://drupal.org/sandbox/programadoresweb/1895746

Just download it and follow the really simple instructions:

"Just activate it and go to /admin/reports/dblog. You'll find a "Node hook update_index: NID" string with the cron executions which tells you in last term which node is executed each time. If cron breaks, the last node reflected in the log will be the offender node, very simple :-)."

Hopefully it will be a new "complete" project, so you'll don't have to use git to download it.

Validation file in Drupal without theme / template

 

Sometimes you need to upload files to, for example, validate an affiliate with whom you want to start to work.

The problem, is that these affiliate files need to be "clean", no template, no theme, just the code or validation string which they send.

The solution can be very complicated, like creating a new theme just for a group or a contect of pages... or as simple as creating a node with the string which we need and executing a php code with an exit command. Something like this:

 

valicationStringLikeUZasdBNf6asdfT2asdfafjpdadf===58QYg==

<?php

exit;

?>

 

so, when you open this node, you'll see a blank page like the one in the screenshot.

Well, i don't like to use php inside nodes, this practice must be avoided, but for this case, the code is enough simple and, the most important thing, it doesn't break the cron execution. So, everyone happy :-).

Do you have a different or improved idea for this? Please, share :-)

 

Get Drupal fields for a particular content type

 

Sometimes in Drupal you need to get the fields for a particular content types. 

"$type = 'mytype';

$fields = content_fields();

$type_fields = array();

foreach ($fields as $field_name => $field_data)

{

  if ($field_data['type_name'] == $type)

  {

    $type_fields[$field_name] = $field_data;

  }

}"

 

in drupal7 is easier, since we have a direct function:

field_info_instances($entity_type = NULL, $bundle_name = NULL)

 

 

get current drupal theme

very simple:

        global $theme_key;

      echo $theme_key;

 

if we need more information, we can use this:

       $themes = list_themes();

       $theme_object = $themes[$theme_key];

 

        // print all the object fields

       var_dump($theme_object);

 

via http://drupal.org/node/46961#comment-88354

Apache Solr + Drupal

Download the apachesolr module from drupal.org, and put it in sites/all/modules:

http://drupal.org/project/apachesolr

download solr-php-client, version 22

http://code.google.com/p/solr-php-client/downloads/list

and uncompress it in the apachesolr module directory.

Ready to activate the module, but we will did not installed the Apache server, so the module will not work.

Now, as you guessed, it's time to install the apache solr server. Download one of the latests releases:

http://www.apache.org/dyn/closer.cgi/lucene/solr/

Remember to download the binary files, not the src directory. You'll know that you have the correct tar if you have an example directory with a .jar file.

 

Now, time to move all these files to a .bak version in conf directory

apache-solr-4.0.0/example/solr/collection1/conf

 

  • schema.xml,
  • solrconfig.xml,
  • protwords.xml

 

then, copy these files from your drupal module directory.

 

 

 

 

 

categorias: 

paging results in Drupal

Paginating the results in Drupal is incredible incredible easy. You just have to remember the two "magic functions":

 

  • pager_query($sql, $count);
  • theme('pager',10);

 

pager_query substitutes the typical db_query. Just something like that: 

 

  • // $resource = db_query($sql);  // -> old sentence without pager
  • $resource = pager_query($sql, $count); // -> pager

 

Then, we simple come back to the function that is called by our hook_menu, and we change:

  • return theme('list_products')

with

  • return theme('list_products') . theme('pager',10);

 

It couldn't be easier, could it? :-). In fact it is harder to undertand the documentation and the different howtos and tutorials which you can find than the process itself.

Theming you module

Docs are not allways the best part in programming languages, frameworks or CMS. Sometimes you try to make something and you find problems, just because poor documentation.

It's my situation just now. I was trying to create tpl.php files for a module, and it is really easy... if you follow exactly all the steps.

First, hook_menu:

 

 

/**

 * Implements hook_menu().

 */

function mymodule_menu() {

$items = array();

 

/*

* Category reviews section 

*/

$items['reviews'] = array(

'title'             => 'Bounty reviews',

'description'       => 'Show review categories.',

'page callback'     => 'listofProducts',

  'page arguments'    => array('bounty_settings'),

'access callback'   => 'user_access',

'access arguments'  => array('administer site configuration'),

'type'              => MENU_NORMAL_ITEM,

'file'              => 'includes/bounty_reviews.inc',

);

 

return $items;

}

 

 

Secondly, you could think that just creating the listofProducts should be enought. Well, not at all, you need the theme hook, to show the content using the tpl.php of you choose:

 

/**

 * Implements hook_theme().

 */

function mymodule_theme($existing, $type, $theme, $path){

$path = drupal_get_path('module', 'mymodule'); // . '/templates';

// ADD THE templates DIRECTORY

$path = $path . '/templates'; 

return array(

// as in 'theme('verbose_method',...)

'main_reviews' => array(

'template' => 'main_page_reviews',

'path' => $path,

'variables' => array('forums' => NULL, 'topics' => NULL,'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),

),

//...

);

}

 

And, ending with the magic:

 

/**

 * List of  reviewable products

 *

 */

function listofProducts(){

$output = theme('list_products');

return $output;

}

That's all. If you forget the _theme hook you can be struggling for hours, just receiving a beauty WSOD (White Screen Of the Death) and no more information.

You wellcome :-).

 

Minimal Apache2 modules for Drupal

This days i've been having problems with my server, until i arrived to a point on I was considering seriously disable Apache and install ngnix, a light server I am using in my laptop and some small projects.

The point is that I don't have noticed a real big performance increment in my laptop (difficult with a few users). Anyway, having to install a new server, disable Apache, tuning everything to work correctly is a good challenge... unless you are at the doors of taking a plane and don't want to be all the trip thinking about the server...

Solution? Ok, let's tuning the Apache2 server, very easy because where are only going to use a2dismod to disable the modules and a2enmod to activate if they give us some error... Easy, right?

Let's go, this is my minimal (by now) apache modules list:

actions.conf  alias.conf  authn_file.load authz_host.load  dir.load    fcgid.load  mime.load  php5.load  suexec.load

actions.load  alias.load  authz_groupfile.load dir.conf fcgid.conf  mime.conf php5.conf  rewrite.load

You can disable the rest with a2dismod [MODULENAME], for example:

a2dismod cgi

I recommend you to restart your server once disabled. If apache cannot restart, enable the module again with a2enmod [MODULENAME] and try again the restart.

I have to say that last week I've been having a lot of problems... The last thing I did was to tune in that way Apache... and simply magic, sites load incredibly fast with this simply modification.

Now, I can fly withour worries :-).

categorias: