Community Documentation

Get a contextual array for your node-links

Last updated April 8, 2011. Created by Wolfflow on July 5, 2005.
Edited by silverwing, martingh, davidneedham, ronald_istos. Log in to edit this page.

Often if you are annoyed by the fact that links under the nodes are only availble as plain text strings, for example when you only want the "read more" link, or only the "comments" links. (example of usage)

This PHPtemplate-specific snipped creates an additional variable: an array with much nicer link data.

function _phptemplate_variables($hook, $vars) {
  switch ($hook) {
    case 'node':
      foreach ($vars[node]->links as $link) {
        preg_match("/<a\s*.*?href\s*=\s*['\"]([^\"'>]*).*?>(.*?)<\/a>/i",$link, $matches);
        $vars["nodelinks"][] = array('url'=>$matches[1], 'text' => $matches[2]);
    }
      print_r($vars);
    break;
  }
  return $vars;
}

Note: The code above creates an array containing the 2 keys.

Other Solution to this task

In http://drupal.org/node/21538#comment-81773 is another solution that gets a contextual array for nodelinks, which does not require the _phptemplate_variables function.

By using an ungreedy modifier to the regexp, the expression can be much simpler.
See: http://drupal.org/node/43888#comment-81797 describes a similar approach for comment links.

Note: some links change text (eg x comments) and can't be used in the associative array.

Comments

I know this thread is old,

I know this thread is old, but this is issue is much easier in D7 with granular rendering. http://drupal.org/update/themes/6/7#granular

Page status

About this page

Drupal version
Drupal 5.x
Audience
Designers/themers

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.