I'm trying to display the title of certain views at the top of where they are displayed using viewreference. I've looked around a lot, but haven't come up with a way to do this. Currently, I can't seem to get the title of any view displayed through viewreference. Any help would be appreciated!

thanks!

Comments

danielb’s picture

Do you want just the title ? This can be done through the 'display fields' section in CCK.
If you want the title and the executed view you may have to override the theme function as I don't think there is a 'full' formatter atm.

<?php

/**
 * Theme function for 'default' viewreference field formatter.
 *
 */
function theme_viewreference_formatter_default($element) {
  $output = '';
  if (!empty($element['#item']['view_id']) && is_numeric($element['#item']['view_id'])) {
    $view = db_fetch_object(db_query("SELECT name, position FROM {viewreference} WHERE view_id = '%d'", $element['#item']['view_id']));
    $args = viewreference_get_element_args($element);
    $view_object = views_get_view($view->name);
    $title = $view_object->get_title() ? $view_object->get_title() : $view->title;
    $output .= '<h3 class="title viewsreference-title">'. check_plain($view->title) .'</h3>';
    $output .= $view_object->preview($view->position, $args);
  }
  return $output;
}

?>
petrelharp’s picture

Status: Active » Closed (fixed)

Great! Thanks a ton! I do want the title along with the full view, and couldn't figure out the right place to do that. It seems like that'd be a good thing to have a checkbox to do, somewhere. Perhaps I'll try to figure out how to add that option.

There is just one minor correction to the code, fixed below -- it should be check_plain($title), not check_plain($view->title:

<?php

/**
* Theme function for 'default' viewreference field formatter.
*
*/
function theme_viewreference_formatter_default($element) {
  $output = '';
  if (!empty($element['#item']['view_id']) && is_numeric($element['#item']['view_id'])) {
    $view = db_fetch_object(db_query("SELECT name, position FROM {viewreference} WHERE view_id = '%d'", $element['#item']['view_id']));
    $args = viewreference_get_element_args($element);
    $view_object = views_get_view($view->name);
    $title = $view_object->get_title() ? $view_object->get_title() : $view->title;
    $output .= '<h3 class="title viewsreference-title">'. check_plain($title) .'</h3>';
    $output .= $view_object->preview($view->position, $args);
  }
  return $output;
}

?>
danielb’s picture

Status: Closed (fixed) » Active

i'll put this into the module so it won't be an issue for others in the future :P

petrelharp’s picture

Rad. =)

danielb’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.