I'm using Drupal 7 and I'm trying to control some links are displayed through template.php via preprocessing. The links are built using various data, such as a first and last name field, the UID of these users, etc. I didn't want to break apart the theme file because I should be able to do this through template.php and I'm not going to bloat my installation up with more and more modules... SO...

In the template.php file, I have the following code:

function foobar_preprocess_html(&$vars) {
	$temp;
	$user_links = array();

	if(isset($vars['page']['content']['system_main']['nodes'])){
		$temp = $vars['page']['content']['system_main']['nodes'][16]['body'];
		if($vars['page']['#type'] == 'page' && $temp['#bundle'] == 'product_page'){
			if(isset($temp['#object']->field_product_foobar_contacts)){
				foreach($temp['#object']->field_product_foobar_contacts as $a){
					foreach($a as $b){
//Load user object, reset user cache to get the latest first, middle, and last names.
						$user_links['temp'][] = user_load($b['entity']->uid, $reset = TRUE);

                                                //Create the links.
						$user_links = array(
							'title'	=> $user_links['temp'][0]->field_first_name['und'][0]['value'].' '.$user_links['temp'][0]->field_middle_name['und'][0]['value'].''.$user_links['temp'][0]->field_last_name['und'][0]['value'],
							'href'	=> 'user/'.$b['entity']->uid,
							'attributes'	=> array('title' => $user_links['temp'][0]->field_first_name['und'][0]['value'].' '.$user_links['temp'][0]->field_middle_name['und'][0]['value'].''.$user_links['temp'][0]->field_last_name['und'][0]['value']),
						);

						unset($user_id, $user_links['temp']);

print '<pre>';
var_dump($user_links);
print '</pre>';

                                                //Printing the links doesn't show anything. (I tried to print to test.)
						print theme('links', $user_links);
						}
					}
					unset($a, $b, $user_links);
				}
			}
			unset($temp);
		}
}

All this is *supposed* to do is grab the first name, middle name (or initial), last name, and UID; and then create a link based off this data to be used in the "product" content type page. I didn't think this would be overly difficult to do, but no matter what I do, I can't get the theme() function to output anything. When I use that var_dump above, I see the following array structure for $user_links:

array(3) {
  ["title"]=>
  string(12) "Mark Smith"
  ["href"]=>
  string(7) "user/42"
  ["attributes"]=>
  array(1) {
    ["title"]=>
    string(12) "Mark Smith"
  }
}

array(3) {
  ["title"]=>
  string(11) "Tom Jones"
  ["href"]=>
  string(7) "user/41"
  ["attributes"]=>
  array(1) {
    ["title"]=>
    string(11) "Tom Jones"
  }
}

Any insight into this is appreciated. Simply put, I'm just trying to output some links programmatically based off 3 fields and the UID without breaking into the theme file all from the template.php preprocessing. I've never been able to find any array structure examples for Drupal 7 on this, so if you have any links to information about what I'm trying to do, feel free to include them.

Comments

wolf_22’s picture

In case someone comes across this in the future:

I used hook_preprocess_node(&$vars) to override the markup of the user profile links. I didn't need to use theme() or theme_links() but instead, when I generated the final link (based on all the values I keyed from using $vars to create the final link), I made sure to put the link data through l() and t() before re-assigning the link(s) back to where the markup was within $vars, which was "$vars['content']['group_basic_info']['field_contacts'][$i]['#markup']".
(The $i is a sentinel variable to iterate through the items I had.)

Now it's working like a charm. I hope this helps and feel free to contact me if any help is needed with using any of this inside template.php.