Skip to content

Instantly share code, notes, and snippets.

@Jaesin
Created April 7, 2014 23:11
Show Gist options
  • Save Jaesin/10072056 to your computer and use it in GitHub Desktop.
Save Jaesin/10072056 to your computer and use it in GitHub Desktop.
Load, cache and use fields in drupal views 7.x-3.x
// Get the view that grabs the data for this tableselect.
$view = views_get_view('view_name');
$view->init();
$view->set_display('default');
$view->set_items_per_page(0);
$view->set_arguments(array('123+1234')); // mass multiple node id's
$view->pre_execute();
$view->execute();
// Render and cache the fields for later use at $view->style_plugin->rendered_fields[$index][$field_name].
$view->style_plugin->render_fields($view->result);
// Create a tableselect out and use the rendered views data.
$header = array(
'title' => '',
'type_image' => '',
);
$options = array();
foreach ($view->result as $index => $row){
$view->row_index = $index;
$options[$row->nid] = array(
'title' => $view->style_plugin->rendered_fields[$index]['field_name'],
'type_image' => $view->style_plugin->rendered_fields[$index]['type'],
);
$form['tableselect_form_element'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment