1

So I made a view and Im trying to pull the results into an array (which I am json encoding and sending to javascript but thats neither here nor there) but some of the node information isnt being pulled. Specifically the node_path.

When I preview the view in the edit views page in the backend, the path shows up. But when I pull the view on the front end from my preprocess_page function the node path is not outputted.

In my view, I'm displaying the following fields:

  • Node: Post Date
  • Node: Title
  • Node: Teaser
  • Node: Type
  • Node: Path

And each of these fields shows up in the preview. But when I try to pull the view using the code below the path isnt output:

$blog_posts = json_encode(theme_get_view_result('front_page_blog_blocks'));

function theme_get_view_result($viewname) {
  // views_get_views_result($viewname) produces the same result as $view->result;
  // Im just showing that I tried both
 //return views_get_view_result($viewname);

  $view = views_get_view($viewname);
  if(!empty($view)){
    $view->preview();
    return $view->result;  
  }else{
    return 'empty';
  }
}

what I also noticed, is that the date was output as a unix timestamp, and not the format I selected (custom, 'M j').

So the result produced in my console is:

    {"nid":"1","node_created":"1312470739","node_title":"blah blah blah this is my title","node_revisions_teaser": 
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce auctor vestibulum gravida. Proin ornare nisi non orci feugiat ut hendrerit mi venenatis. Etiam diam ligula, pellentesque et vehicula sed, ultrices at lorem. Vivamus consectetur risus nisl. Vivamus luctus volutpat dolor,","node_revisions_format":"2","node_type":"blog"},

So notice: there is in incorrect timestamp, and the node path is missing. Im a total Drupal noob so I have no idea whats going on.

0

aha! turns out render() only produces data saved in the database, and node paths are not saved in the DB. So my solution was to grab the node id from the rendered view, use that to get the node paths, and created a new field with that path like so:

function theme_get_view_result($viewname) {
  // views_get_views_result($viewname) produces the same result as $view->result;
  // Im just showing that I tried both
 //return views_get_view_result($viewname);

  $view = views_get_view($viewname);
  if(!empty($view)){
    $view->preview();
    return $view->result;  
  }else{
    return 'empty';
  }
}

$blocks = theme_get_view_result('front_page_blog_blocks');
foreach($blocks as $k=>$v)
{
  $node_path = drupal_lookup_path('alias','node/'.$blocks[$k]->nid);
  $blocks[$k]->node_path=$node_path;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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