Posts

Drupal 6 – strtotime() [function.strtotime]: It is not safe to rely on the system’s timezone settings

Recently we came across this red/pink error message on an old(ish) Drupal 6 site that is running on shared hosting:

strtotime() [function.strtotime]: It is not safe to rely on
the system's timezone settings. You are *required* to use
the date.timezone setting or the date_default_timezone_set()
... blah blah blah ...

It seems that this PHP warning is new to PHP 5.3 so we reckoned that the shared hosting must have upgraded their PHP to 5.3, anyway it turned out that adding the following line to the site’s php.ini file got rid of the red error message:

date.timezone = 'Europe/Dublin'

This explicitly sets the time zone to ‘Europe/Dublin’

 

SimpleXMLElement Minus / Hyphon in Element Name

Just came across a bit of a time-waster in the PHP SimpleXMLElement lib.  Usually if you’re accessing an element called, say ‘username’ you use the follwoing syntax:

[code]

$username = $element->username;

[/code]

However if the element has a minus sign in its name, like ‘user-name’ you have to use the following syntax instead:

[code]

$username = $element->{‘user-name’};

[/code]

If you use the original syntax no data will be returned, mad stuff, I would have wasted much more time on this if it wasn’t for this post – Thanks for the dig-out!

Changing the ‘Home’ breadcrumb in Drupal 6

Here’s something I was googling last week – How do you change the text of Drupal’s ‘Home’ breadcrumb to something else? Well, it turns out that a small mod to your theme may be required…

 

While using Drupal 6 to develop a web application we ran into a little snag with Drupal’s breadcrumbs, the app is to integrate in with our client’s existing web site, it will look and feel the same and the visitor should not really know that it is separate from the main website (which is not implemented with drupal).  With drupal this was all easy enough to achieve, but we hit a speed bump when it came to the web app’s breadcrumbs.  Breadcrumb navigation is very important for the new web application, but Drupal always calls the first breadcrumb ‘Home’ – this was a problem as ‘Home’ did not refer to the larger web site’s home page as would be expected, but instead to the first page of the web application.

 

So how do we change the ‘Home’ breadcrumb text to something else, say the title of our web application?  It turns out that there’s no easy way to do this through settings, but thanks to this thread we can see that it is possible via a small change to the active theme. The modification hooks into phptemplate_breadcrumb(), it removes the existing primary breadcrimb (‘Home’) and then inserts a replacement with the new text.

 

So to make the change, edit your theme’s template.php file, and search for the function called:

 

[code lang="php"]phptemplate_breadcrumb() [/code]

rename it to:

[code lang="php"]original_phptemplate_breadcrumb() [/code]

Then paste in the following, changing ‘New Home Text’ to what ever you want your ‘Home’ breadcrumb to be called.

[code lang="php"]
function phptemplate_breadcrumb($breadcrumb)
{
  if (!empty($breadcrumb))
  {
    // remove the exisitng Home link
    $old_home_link = array_shift($breadcrumb);
    // insert the new link
    array_unshift($breadcrumb,
                   l(t('New Home Text'), ''));
    // Get the original function to
    // output the breadcrumb
    return original_phptemplate_breadcrumb($breadcrumb);
  }
}
[/code]

With this theme modification in place the breadcrumbs should display with the new text in place, bit of a pain but at least it’s do-able!

 

 

Hats off to gwen for the tip & code!

Drupal – Debug Output not Displaying?

Have you ever noticed that sometimes no debug output whatsoever appears when you call a debug output function like dprint_r() from the PHP code that you’re working on?  For example, if you call one of the debug dump functions from the default argument handler for a block view (see earlier post) you won’t see a blessed thing! It seems that there are plenty of situations in the drupal processing lifecycle where output debug information will not get through for display on a page…

 

So what’s to be done? life without debug information can get very difficult and frustrating, but not to worry as in these difficult cases we can always log our debug info to a file using file_put_contents() (as suggested here), for example if you wanted to dump information about a variable called $my_variable you could call:

[code lang="php"]
file_put_contents("./drupal.debug",
                 print_r($my_variable, TRUE),
                 FILE_APPEND);
[/code]

Then simply read the contents of the file drupal.debug, not pretty and not perfect but a whole lot better than nothing!

neat_trim() – Handy for tailored drupal teasers

In drupal there are many ways to go about putting together teasers for custom content types created with the Content Creation Kit (CCK). The quickest to get up and running with is often to use the Content Templates (Contemplate) module, this allows you to specify in-line markup and PHP with code to get at and display the node’s data fields – formatted just as you require.

 

Often when putting together a teaser you may want to neatly truncate some long text to a summary and append ‘…’ or the like to the end – well there’s a great little PHP function which does just this called neat_trim() it is provided by Justin Cook and does the job  nicely!

 

One thing to note however is that HTML mark-up as well as newlines and carriage-returns in the input string can confuse this function.  To get around this problem you can arrange to remove the offending material either by modifying the function or by stripping it out of the input string before calling the function.  Use can use strip_tags() to remove HTML mark-up.

Passing an argument to a Block View in Drupal 6

Q: How can I pass an argument to a ‘block view’ in Drupal 6?

A: There is no way to pass an argument to a block view in Drupal 6, but don’t panic as there is a way to achieve the same result through some slight-of-hand.

The use of arguments with Drupal views are vital for getting the most out of the views functionality. How are the arguments normal passed to a view?  Well, if a view is configured to produce a page then the arguments are easily passed as part of the requesting URL,  while if a view is embedded using code, then the arguments are passed in as part of the call to embed the view.  But if a view is configured to produce a block, how do you pass arguments to it?  The bad news is you can’t – the good news is that there is a way fake it and achieve the same result.

The trick involves providing a PHP handler within the view which will be called when the view is invoked without an expected argument (this is what happens when the block is displayed!).  We just arrange for this handler to retrieve and return the argument’s value and then the view will behave as required – just as if the argument had been passed to it in the first place.

To do this create the block view as normal and configure the required argument(s).  For each argument we choose the ‘Provide default argument’ option, and select the ‘PHP code’ sub-option.  We then provide some PHP code which will ‘get’ and return the argument’s value, it doesn’t really matter where or how the PHP code gets the argument once it returns the correct value.   Have a look at screen shot below:

Adding a default parameter

The example above is a bit simplistic as the PHP code just returns a static value – not very useful at all!  A more realistic or useful example  (inspired by one of the posts referenced below) would be to return the argument that was passed to the page that contains and displays the block.  Consider the mythical paths:

www.somedomain.com/content/projects/web-design

and

www.somedomain.com/content/projects/illustration

Here things are set-up so that ‘web-design’ and ‘illustration’ are arguments to the ‘projects’ page, they result in only the projects of that type being displayed.   Assuming we are using the pathauto module for nice clean URLs (as we almost always are!) then the following PHP code when provided as the default argument handler will get the URL, parse it and return the argument part to the view.

[code lang="php"]
$path = drupal_get_path_alias($_GET["q"]); //get URL alias
$path = explode("/", $path); //break path into an array
if ($path[0] == "projects" && $path[1] != "")
{
  return $path[1];
} [/code]

So there is is, it’s definitely not the easiest method in the world but at least it does provide a mechanism of getting those arguments to the view…

Sources: http://drupalsn.com/learn-drupal/drupal-questions/question-2650, http://drupal.org/node/332521

Technical Support Service for irish Web Designers Launched

We have finally got around to launching our new Technical Support Service for Irish Web Designers – a service designed to take care of the occasional programming and enginering aspects of a Web Designer’s Web development work, leaving them free to concentrate on the actual Web Design and Delivery.

 

This is a local service provided by experts in Ireland – so fast response times with the minimum of  administrative overhead are guaranteed.  This service is designed to be as useful for very small tasks (perhaps just an hour’s duration) as it is to larger tasks.

More details can be found here…