There are some instances that we need to alter the value of Views' field and add markup on its rendered output programmatically. The following codes shows how to achieve these tasks using hook_views_pre_render()
.
/**
* Implements hook_views_pre_render().
*/
function mycustommodule_views_pre_render(&$view) {
if ($view->current_display == 'front_shoplist_pane') {
$status = t('(open)');
$icon = '<i class="fa fa-check" aria-hidden="true"></i> ';
if (isset($_GET['type'])) {
$status = t('(closed)');
$icon = '<i class="fa fa-close" aria-hidden="true"></i> ';
}
foreach ($view->result as $index => &$result) {
// Alter the title field value
$result->node_title .= " $status";
}
$view->style_plugin->render_fields($view->result);
foreach ($view->style_plugin->rendered_fields as $index => &$rendered_result) {
// Add HTML markup icon on rendered title field
$rendered_result['title'] = $icon . $rendered_result['title'];
}
}
}
This is tested on both regular Views and Search API result Views.
Comments
You're welcome. I'm glad the…
You're welcome. I'm glad the codes helped you.
thanks :-)
thanks :-)
Very Helpful
Thank you !
I am begginer of Drupal. This is very helpful topic.
Really awesome
Superb. Thank you so much. Its helpful for me.
Great
Great!! Thank you!!
Thank you
I am looking for this kind of code part, since last 4 to 5 days. This is exactly a perfect piece of code. Thank you so much :)
Getting Only last row values
Hi,
I Want to display user block unblock icon based on the user status, but unfortunately I am getting always last row user status, and it's apply to the all row, for example if my last row user status is 1 then remaining rows will keep as same status 1
MY CODE :
function MODULENAME_views_pre_render(&$view) {
if ($view->name == 'MY_user_management') {
foreach ($view->result as $index => &$result) {
$icon = 'glyphicon-ban-circle';
$title = 'Block user';
if($result->_field_data['uid']['entity']->status == 0) {
$icon = 'glyphicon-ok-circle';
$title = 'Unblock user';
}
$result->_field_data['uid']['entity']->MYFIELD =
'<a href="#" data-uid="[uid]" data-name="[field_firstname]" title="'. $title. '" class="my-block-user [status]">
<span class="glyphicon '. $icon. '"></span></a>';
}
$view->style_plugin->render_fields($view->result);
foreach ($view->style_plugin->rendered_fields as $index => &$rendered_result) {
$rendered_result['MYFIELD'] = $result->_field_data['uid']['entity']->MYFIELD;
}
}
Hi, try not to wrap the …
Hi, try not to wrap the foreach ($view->result as $index => &$result)
on variables that assigns the label and icon. I suggest to follow exactly the structure of the article's codes above.
Thanks it's working
function MODULENAME_views_pre_render(&$view)
{
if ($view->name == VIEWNAME)
{
foreach ($view->result as $result)
{
$status[] = $result->_field_data['uid']['entity']->status;
}
//Load and render the user fields
$view->style_plugin->render_fields($view->result);
foreach ($view->style_plugin->rendered_fields as $index => &$rendered_result)
{
$icon = 'glyphicon-ban-circle';
$title = 'Block';
if($status[$index] == 0) {
$icon = 'glyphicon-ok-circle';
$title = 'Unblock';
}
<a href="#" title="'. $title. '"
class="block-user">
<span class="glyphicon '. $icon. '"></span></a>';
}
}
}
You're welcome. I'm glad you…
You're welcome. I'm glad you've got it to work :)
OMG THANK YOU
I've been researching and trying to figure out how to do this for 2 days. Something as simple as hiding (unsetting) a view global text field based on the value of another field in the row in a module (not with theming) should not be this difficult.
Because it's a global text field the usual hooks (pre_render, post_execute, etc) are useless. The easy way is to enable the php filter module and use the views_php filter-- but that's not acceptable for all sorts of reasons. Using your code above, i was able to simply unset it in the $rendered_result array-- basically 3 lines of code. brilliant!
Thanks!