Hi there,

I'd like to show/not show fields in a view depening on a condition (language)
I've found out I need to use the hook :
hook_views_pre_view
But can't find out how to show/hide fields using the hook.

Thx in advance!

Comments

dco’s picture

Use a views template suggestion and then hide unnacessary fields in it.

byenary’s picture

That could be a solution indeed, maybe its just easy'r then to create a sperate view for each language

xandeadx’s picture

for Views 3:

/**
 * Implements hook_views_pre_render().
 */
function MODULENAME_views_pre_render(&$view) {
  if ($view->name == 'VIEW_NAME' && $view->current_display == 'DISPLAY_NAME' && OTHER_CONDITIONS) {
    $view->field['FIELD_NAME']->options['exclude'] = TRUE;
  }
}
sunwukong’s picture

Subscribed

krystalcode’s picture

Nice one, would you know how you can do exclude the field but only for some of the results? For example:

/**
* Implements hook_views_pre_render().
*/
function MODULENAME_views_pre_render(&$view) {
  if ($view->name == 'VIEW_NAME' && $view->current_display == 'DISPLAY_NAME' && OTHER_CONDITIONS) {
    foreach ($view->result as $item) {
      if (CONDITIONS) {
        $view->field['FIELD_NAME']->options['exclude'] = TRUE; // exclude field for only this result ???
      }
    }
  }
}
Sebastian Hagens’s picture

Good question.

I've made a solution with the views_conditional module (drupal.org/project/views_conditional).
Have you found a code-based solution (which is better in my case)?

ricobanga’s picture

Old thread, but as i found a simple solution :

function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($whatever)
    $fields = $view->display_handler->get_option('fields');
    $fields["YOUR_FIELD"]['exclude'] = 1;
    $view->display_handler->override_option('fields', $fields);
  }
}
zoltanb’s picture

Nice, i could use the same logic in Drupal 8.

/**
 * Implements hook_views_pre_view().
 */
function MYMODULE_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if ($WHATEVER) {
    $fields = $view->display_handler->getOption('fields');
    $fields['YOUR_FIELD']['exclude'] = TRUE;
    $view->display_handler->overrideOption('fields', $fields);
  }
}
FlutterStack’s picture

/**
* Implements hook_views_pre_render().
*/
function MODULENAME_views_pre_render(&$view) {
if ($view->name == 'VIEW_NAME' && $view->current_display == 'DISPLAY_NAME' && OTHER_CONDITIONS) {
$view->field['FIELD_NAME']->options['exclude'] = TRUE;
}
}

dunot’s picture

foreach ($view->result as $row) {
  $custom_status = (int)$row->_entity->field_custom_status->getValue()[0]['value'];
  if ($custom_status == 0){
      $row->_entity->{'variations'}->set(0,0);
  }
}
oltus’s picture

Anyone can advise me how to make a condition base on the views field? I want to get the views field value and assign create a condition with the value on the user profile.
 

Ex:
  if (User Profile State  ==  Views field state){

    $view->field['field state']->options['exclude'] = TRUE;

 }

else{
  $view->field['field state']->options['exclude'] = FALSE;
}

Jaypan’s picture

For Drupal 8+:

function hook_views_pre_render(ViewExecutable $view) {
  $view_id = 'some_view_id';
  $current_display = 'id_of_display_on_that_view';
  $field_to_remove = 'some_views_field_name_in_that_display';
  if ($view->id() == $view_id && $view->current_display == $current_display) {
    if (function_that_returns_a_boolean()) {
      $view->field[$field_to_remove]->options['exclude'] = TRUE;
    }
  }
}