I created a new field for URL. What would the best way to replace certain strings in the URL when outputting the field into the field--field_url.tpl.php file be?

For example, in http://example.com/master/m/ I need to remove the "http://" part, and replace master with slave.

It is a single field; there will be no foreach() loops.

==== edit after using preprocess === i got a undefined index: field_url

with the devel i get this


i put in the preprocess

$link = $variables['field_url'][LANGUAGE_NONE][0]['value'];

$link = str_replace('http://', "", $link);

$link = str_replace('master', 'slave', $link);

$variables['link'] = $link;

is this right?

====== entire hook =======

function lessxless_preprocess_field(&$variables)
    $link = $variables['field_url'][LANGUAGE_NONE][0]['value']; // grab value

    $link = str_replace('https://', "", $link); // trim http://

    $link = str_replace('master', 'slave', $link);

    $variables['link'] = $link;


1 Answer 1


You probably want a preprocess function in your template.php for this. In your case, hook_preprocess_field() will be easiest to work with - for an example of how this is implemented, check out the API documentation for template_preprocess_field().

Remember, $variables is passed into preprocess hooks by reference. From within your hook_preprocess_field(), instead of modifying the field data directly (in case you need it elsewhere), you could do something like this after checking that you're operating on the right field:

/* devel's dpr() is handy for seeing where in the $variables array it lives if you 
   don't have a debugger handy. If you see 'und', that's the value for the LANGUAGE_NONE
   constant. It's considered best practice to use the constant instead. */
$slave_link = $variables['field_url'][LANGUAGE_NONE][0]['value'];  
$slave_link = str_replace("http://", '', $slave_link); // Trim http://
$slave_link = str_replace("master", "slave", $slave_link); // s/master/slave
$variables['slave_link'] = $slave_link;  /* You can now access this variable from your
                                            field--field_url.tpl.php as $slave_link */

You may also need to clear your cache after making these changes.

  • thanks David, can you give me an example what i should write in $foo that i should be seeing with devel themer info
    – devric
    Mar 1, 2012 at 5:38
  • You wouldn't find it in Devel Themer to the best of my knowledge. After creating your hook_preprocess_field(), download/enable the <a href="drupal.org/project/devel">Devel</a> module, and put "dpr($variables);" in your new hook. It will print the data structure of $variables when the function is called, and you'll reference the value you're looking for by its keys in your function. Note that arrays may be nested, so it may look something like "$slave_link = $variables['foo']['bar']['baz'];" when all is said and done. Mar 1, 2012 at 5:55
  • got a undefined index, i edited the question
    – devric
    Mar 1, 2012 at 9:32
  • Updated my answer (apologies for the poorly formatted link above; didn't notice until it was too late to edit). Would you mind posting your entire hook_preprocess_field() and the path to the file it's in relative to your Drupal root? It may help me help you debug the undefined index issue. Mar 1, 2012 at 14:19
  • sure, here it is, in the edits
    – devric
    Mar 2, 2012 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.