Switch style plugin of a view based on results I would like to create a view, which would display it's result with style plugin Unformatted if there is only 1 result, but would use Bootstrap Accordion style plugin, if there are more of them. I tried to do it like this:




    name == 'some_view_name') {
        if (count($view->result) > 1) {
          $view->style_plugin = 'ViewsBootstrapAccordionPluginStyle';
          $view->style_plugin->plugin_name = 'views_bootstrap_accordion_plugin_style';
          $view->style_plugin->options['title_field'] = 'title';
          $view->style_plugin->definition['path'] = 'sites/all/modules/views_bootstrap/plugins/accordion';
          $view->style_plugin->definition['theme path'] = 'sites/all/modules/views_bootstrap/templates/accordion';
          $view->style_plugin->definition['file'] = 'ViewsBootstrapAccordionPluginStyle.inc';
          $view->style_plugin->definition['theme file'] = 'theme.inc';

        }
      }
    }


The result is this error:

Fatal error: Call to a member function render() on a non-object in .../sites/all/modules/views/theme/theme.inc on line 49



What am I doing wrong?

EDIT: Probably almost everything. $view-&gt;style_plugin has to be an object, that was the reason of the error above. So I modified the code to this:

&lt;?php
function my_module_views_pre_render(&amp;$view) {
  if ($view-&gt;name == 'my_view') {
    if (count($view-&gt;result) &gt; 1) {
      $bs_accordion_style = new ViewsBootstrapAccordionPluginStyle;
      $bs_accordion_style-&gt;plugin_name = 'views_bootstrap_accordion_plugin_style';
      $bs_accordion_style-&gt;options['title_field'] = 'title';
      $bs_accordion_style-&gt;definition['path'] = 'sites/all/modules/views_bootstrap/plugins/accordion';
      $bs_accordion_style-&gt;definition['theme path'] = 'sites/all/modules/views_bootstrap/templates/accordion';
      $bs_accordion_style-&gt;definition['file'] = 'ViewsBootstrapAccordionPluginStyle.inc';
      $bs_accordion_style-&gt;definition['theme file'] = 'theme.inc';
      $bs_accordion_style-&gt;options['grouping'] = array();
      $bs_accordion_style-&gt;row_plugin = $view-&gt;style_plugin-&gt;row_plugin;
      $bs_accordion_style-&gt;definition['theme'] = 'views_bootstrap_accordion_plugin_style';
      $bs_accordion_style-&gt;view = $view-&gt;style_plugin-&gt;view;
      $bs_accordion_style-&gt;display = $view-&gt;style_plugin-&gt;display;
      $view-&gt;style_plugin = $bs_accordion_style;
      $view-&gt;style_options['title_field'] = 'title';
    }
  }
}


That almost works. It results in this error (1 per view result):

Recoverable fatal error: Object of class stdClass could not be converted to string in include() (line 21 of .../sites/all/modules/views_bootstrap/templates/accordion/views-bootstrap-accordion-plugin-style.tpl.php)

How can I correct this?
I have finally discovered a workaround, that may be the solution for some. I display the view using Panels. Using visibility rule, there is possibility to limit visibility of view based on number of results of that view. The steps could be:


Create a Contextual filter for your view for field Content:Nid. This should correspond to argument of your Ctools Page (so that path of a page is similar to something/%nid). You may need to add Relationship, if you want to display content, that is related to the content with nid.
Create a Context display for your view.
Create a 2 separate displays (Content panes) for your view - for one result and multiple results.
Create a new Relationship based on your view for your Panel (at the Contexts tab).  
Add both your content panes to the panel.
Add Visibility rules to both panes. You should choose PHP-based filtering and enter something like:


if (count($contexts['relationship_view_from_argument:name-of-your-view-ctools_context_1_1']-&gt;output['rows'])==1) {
return TRUE;
}