2

Main question: How to change the value for "Exclude from display" on runtime, programmatically ?

Background: I am rendering a View programmatically. The View has attached Data Export to it (exporting to CSV) The view has multiple fields (42 actually).

On some action (hook_node_update, etc.), I load a node object. The node object has 2 fields of interest.

  • field_views_filter = field that is using the Field Embed Views module and stores exposed filter settings
  • field_export_select_fields = list(text) field that selects which of those 42 fields should be exported to csv. Sometimes all fields will need to be displayed, sometimes only 5 fields will have to be displayed.

Scenario: What I have tried:

<?php    
function test () {
      // Loading arbitrary node
      $node = node_load(44165);

      $select_fields = array();

      $displayed_fields = array();
      foreach ($node->field_export_select_fields[LANGUAGE_NONE] as $key => $value) {
        $select_fields[] = $value['value'];
      }

      $name = 'MY_VIEW_NAME';
      $display = 'views_data_export_1';
      $view = views_get_view($name);
      $view->set_display($display);

      // Gets the exposed filters.
      $exposed_filters = $node->field_views_filter[LANGUAGE_NONE][0]['settings'];
      $view->set_exposed_input($exposed_filters);


      foreach ($view->display['views_data_export_1']->display_options['fields'] as $key_field => $value_field) {
        // This sets the value of "Exclude field from display to 1", but it is not working.
        $view->display['views_data_export_1']->display_options['fields'][$key_field]['exclude'] = 1;
      }
      $view->display['views_data_export_1']->display_options['fields']['title']['exclude'] = 0;


      $output = $view->preview();

      $file_string = 'sites/default/files/private/' . time() . '.xls';
      file_unmanaged_save_data($output, $file_string, FILE_EXISTS_REPLACE);

      $view->destroy();
    }
2
  • 1
    I'm not sure what you're trying to do here, do you want to hide some field depending of some condition? If that the case why not using api.drupal.org/api/views/views.api.php/group/views_hooks/7? look for hook_views_pre_view or others that are called before rendering.
    – GwenM
    Commented Dec 30, 2015 at 15:49
  • Why do you need to change the value of exclude from display at runt time?
    – No Sssweat
    Commented Jan 5, 2016 at 4:52

2 Answers 2

1

You need to save your view with hidden fields and then load it again

<?php

function test () {
  // Loading arbitrary node
  $node = node_load(44165);

  $select_fields = array();
  foreach ($node->field_export_select_fields[LANGUAGE_NONE] as $key => $value) {
    $select_fields[] = $value['value'];
  }

  $name = 'MY_VIEW_NAME';
  $display = 'views_data_export_1';
  $view = views_get_view($name, TRUE);

  // Hide selected fields.
  foreach ($view->display['views_data_export_1']->display_options['fields'] as $key_field => $value_field) {
    if (in_array($key_field, $select_fields)) {
      $view->display['views_data_export_1']->display_options['fields'][$key_field]['exclude'] = 1;
    }
    else {
      $view->display['views_data_export_1']->display_options['fields'][$key_field]['exclude'] = 0;
    }
  }

  // Save view.
  views_save_view($view);
  // Load updated view.
  $view = views_get_view($name, TRUE);
  $view->set_display($display);

  // Gets the exposed filters.
  $exposed_filters = $node->field_views_filter[LANGUAGE_NONE][0]['settings'];
  $view->set_exposed_input($exposed_filters);


  $output = $view->preview();

  $file_string = 'sites/default/files/private/' . time() . '.xls';
  file_unmanaged_save_data($output, $file_string, FILE_EXISTS_REPLACE);

  $view->destroy();
}
1
  • 1
    Hi, I will check this one soon and I will post if that solves my issue. Thanks for now. Commented Dec 31, 2015 at 12:19
0

Your question is still unclear to me, but anyway I recommend to use different theme_hook_suggestion for the view and based on the condition use the appropriate suggested hook. in template.php:

function YOURTHEME_preprocess_views_view_fields(&$variables)
{
    $view = $variables['view'];
    if($view->name == "your_view_name")
    {
          if($view->current_display == "some_custom_display"){  
            $variables['somefield'] = $variables['fields_somefield'->content;
            if($condition_1 == true)    
                $variables['theme_hook_suggestions'][]='views_view_fields__1ththeme';
            if($condition_2 == true)    
                $variables['theme_hook_suggestions'][]='views_view_fields__2ththeme';
              ...
    }
} 

now create files views-view-fields--1ththeme.tpl.php and views-view-fields--2ththeme.tpl.php in your theme directory. the $somefield is available in there and it returns the value of the fields_somefield field. In these files just print any field you want and no matter if they are set to be excluded from display or not, The output of the view is what you print in the theme file.

The is it possible to have one views-view--VIEWNAME--page.tpl for multiple views? is also helpful.

As I said I didn't fully understand your question, if some thing is wrong with my answer just let me on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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