I've got a use case I'm working on where I want the results of a view (displaying content) to display differently if there's one result, or if there's many. The way I considered doing this was by altering the Content View Mode so that if the count of the results was 1 or less, then it used a different view mode for the content than the default. Then I could use twig to theme to my heart's content.

However, I can't quite seem to make this work. I can correctly pull the number of rows from hook_views_pre_render, and while I can access the view mode (There are several permutations, but I suspect it's $view->rowPlugin->options['view_mode'] is correct), changing it during pre-render doesn't seem to have any effect).

Is there another hook I should be looking at? None of the other hook_views_pre_* seem to work. Is there another way of accomplishing what I'm trying to do? (Display a different view if the result count is a certain number).

How can I programatically alter the view mode of a view? (And can I do it after I have result rows?)

Thanks in advance!

Issue fork drupal-2888266

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TrevorBradley created an issue. See original summary.

TrevorBradley’s picture

For reference, it appears that this is possible with hook_views_pre_build, using a call to:

$view->rowPlugin->options['view_mode'] = 'my_different_view_mode';

Unfortunately, this is before the view runs, so there's no way to for me to select the right condition (number of returned rows).

Version: 8.3.4 » 8.3.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.3.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
cilefen’s picture

Status: Active » Closed (outdated)

I am closing this support request because there have been no recent comments.

Mohammad-Fayoumi made their first commit to this issue’s fork.