1

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->style_plugin has to be an object, that was the reason of the error above. So I modified the code to this:

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

  • Have you checked if style class exists in a file you are claiming it to be? And with that exact name? – Mołot Oct 8 '15 at 21:09
  • The reasoning above is most probably completely wrong, as $view->style_plugin has to be an object, not a string. I got further and will edit the question. Thanks! – Miloš Kroulík Oct 9 '15 at 5:56
  • Hope I helped, even if only by pointing you in right direction - because I admit I failed to spot string <-> object issue. – Mołot Oct 9 '15 at 7:22
0

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:

  1. 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.
  2. Create a Context display for your view.
  3. Create a 2 separate displays (Content panes) for your view - for one result and multiple results.
  4. Create a new Relationship based on your view for your Panel (at the Contexts tab).
  5. Add both your content panes to the panel.
  6. 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']->output['rows'])==1) { return TRUE; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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