This issue is to create a new “Views Responsive Grid” format in Drupal 10 core.

Background

We've fixed this in Olivero, and the 10.x solution is fantastic. It injects the number of columns into the markup via inline CSS variables, and also sets a minimum width for the grid cells.

The browser will automatically readjust the column count taking into account the inputted number of columns (which it treats as a maximum value) and the minimum width of the grid cell.

Views UI Settings

Responsive horizontal style in action (in Stark theme)

Responsive vertical style in action (in Stark theme)

Testing instructions

  1. Apply patch (or pull branch) and clear cache
  2. Create or modify a View that uses the new “Responsive Grid” format (available in the “Format” section of the Views UI
  3. Go into the settings for this format, and adjust the various options. There are four:
    1. Maximum number of columns: This is where the grid “wants” to be, if the grid container has space enough so that the grid cells are not under the “Minimum grid cell width” setting.
    2. Minimum grid cell width: As the grid container is resized, the grid cells with grow shrink to maintain the maximum number of columns. However, once the grid cells reach the “Minimum grid cell width” value, the number of columns will be reduced to maintain this minimum width.
    3. Grid gutter spacing: This is the space that will be between the grid cells (in pixels). This maps to the CSS gap property.
    4. Alignment: these two options are exactly the same as the old “Grid” format. If set to “Horizontal” the Views rows will be ordered left to right (or RTL if needed) and appear in horizontal rows. If set to “Vertical” the grid items will be formatted using CSS columns and be ordered from top to bottom and then left to right (or RTL).
  4. View the grid page and resize the browser and play around and verify all the settings. Verify it works as expected.
  5. Test in multiple browsers such as Chromium, Safari, Firefox.

Issue fork drupal-3151553

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

mherchel created an issue. See original summary.

mandclu’s picture

I agree, we basically never use the grid view style in core because of its limitations. Something responsive would be a huge improvement.

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

xjm’s picture

Title: Make core views grid styles responsive » Make core views grid styles responsive and/or deprecate the row and class features for the grid view
xjm’s picture

Tagging for Views maintainer feedback on whether we can deprecate the row and column classes feature outright, which would be a simpler step than adding the responsive grid display feature (useful in its own right, but more work).

tstoeckler’s picture

Linking a related issue here. Not sure, it might be a duplicate of this but the issue summary here mentions Olivero whereas that issue is a problem in all core themes (if I remember correctly). But it seems resolving this would resolve that as well, so maybe closing that would be fine?

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mherchel’s picture

Issue summary: View changes
FileSize
5.82 MB
Lendude’s picture

In my opinion you are much better off adding a new display. Doing the BC dance with the old one will be much more work and updates tend to end up being fragile.

And this being Views, I'm certain somebody has cooked up something weird with the settings we think are useless and should be removed.

mherchel’s picture

Thanks for the clarification!

mandclu’s picture

Status: Active » Needs review

With all tests passing, including a new unit test for the plugin added, moving this to Needs Review.

Gauravvvv’s picture

I tested on live preview, both grid view horizontal and grid view vertical. Added the screen-recording for reference. views grid styles are responsive.

Can be move to RTBC.

mherchel’s picture

Title: Make core views grid styles responsive and/or deprecate the row and class features for the grid view » Create new “Views Responsive Grid” format for Views Core
Issue summary: View changes
FileSize
85.42 KB
92.87 KB
4.83 MB
4.46 MB

Updating summary

mherchel’s picture

Thanks for the review @Gauravmahlawat, but before RTBC we'll probably need subsystem maintainer review.

mglaman’s picture

I forgot to reply here, myself, but I gave it a look over and thumbs up from me. This is exciting!

lauriii’s picture

Gave the MR quick peek and it looks awesome! I hope that we get this in soon 🎉

mherchel’s picture

mherchel’s picture

catch’s picture

Priority: Normal » Major

I think this is a bit more than normal.

Should we also look into deprecating the non-responsive grid eventually, or are there still use-cases for that?

mherchel’s picture

Should we also look into deprecating the non-responsive grid eventually, or are there still use-cases for that?

I can't see any use cases for the current grid system anymore. I think it should be deprecated.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

I bet there's use-cases and deprecating the format users need to know how to replace it

catch’s picture

I'm removing 'needs subsystem maintainer review' since @lendude has commented on here.

To actually mark the existing grid deprecated, we'll need #3039240: Create a way to declare a plugin as deprecated which is not in yet.

I'm going to tag this for product manager review - are we OK to add a new plugin without deprecating the old one?

Gábor Hojtsy’s picture

I think its ok to add a new one without depreacting the old one, since the features are not 1-1. In case of the existing core grid, the columns setup stays the same regardless of viewport. I would not suggest anyone to use the non-responsive grid and I would not add that feature to core these days, but it is already there. It is important to gently deprecate it with proper tooling and I don't think this needs that deprecated to be added.

catch’s picture

Issue tags: +Needs follow-up

#26 makes sense to me - we can open a follow-up to discuss deprecating the old grid layout once this has landed.

mherchel’s picture

Status: Needs work » Needs review
mherchel’s picture

Brought back up to date with 10.0.x

mherchel’s picture

Lendude’s picture

It looks amazing on tugboat.

I'd like to see the test coverage expanded here, we are only testing with 1 set of values and it would be nice to run some different sets. Also the defaults aren't being tested currently.

I would suggest rewriting the test to use a @dataProvider so it becomes easier to test different data sets (one of which could be empty to test the defaults).

mherchel’s picture

Status: Needs review » Needs work

Thanks for the reviews! We'll address these shortly :)

darvanen’s picture

Checked out the CSS, my only hesitation is the negative margin to bring the bounding box in line with the end of the bottom row for the vertical grid because it will likely surprise newer themers.... but then I realised the other option was fielding hundreds of "how do I remove this gap?" questions which is way harder to understand than why a gap is bigger than you intended after setting a margin.... so in short: no further comment on the CSS

mandclu’s picture

Status: Needs work » Needs review

Responding to some earlier feedback:

We can get rid of this $alignmentsTested variable and any references to it. It is in the StyleGridTest because $this->assertGrid is called multiple times on each alignment. In this new StyleGridResponsiveTest, $this->assertGrid is only called once per alignment.

There's no value in including it because it's only used twice? I'm not sure I follow the logic there. Also, the test associated with this property does validate that the expected class is being applied. That speaks to this comment:

Could you add an assertion to confirm that the items are being rendered with the right markup?

And to respond to the comment by @Lendude in #31:

I'd like to see the test coverage expanded here, we are only testing with 1 set of values and it would be nice to run some different sets. Also the defaults aren't being tested currently.

I would suggest rewriting the test to use a @dataProvider so it becomes easier to test different data sets (one of which could be empty to test the defaults).

I'm not against trying to improve what we have here, but I think it's worth pointing out that what is here already is equivalent to what has already been implemented (and accepted into core) for the existing grid view. Could we accept what's here and open a child issue for the expanded test coverage?

danflanagan8’s picture

There's no value in including it because it's only used twice? I'm not sure I follow the logic there.

Here's what I mean. The new test clearly began as a copy/paste StyleGridTest.php. That test ran assertGrid for many more configurations:

  public function testGrid() {
    $view = Views::getView('test_grid');
    foreach (['horizontal', 'vertical'] as $alignment) {
      $this->assertGrid($view, $alignment, 5);
      $this->assertGrid($view, $alignment, 4);
      $this->assertGrid($view, $alignment, 3);
      $this->assertGrid($view, $alignment, 2);
      $this->assertGrid($view, $alignment, 1);
    }
  }

Some of the assertions only need to be done once per alignment. That's why StyleGridTest.php. has $alignmentsTested: to prevent running redundant assertions.

For the new test, there's only one horizontal configuration and one vertical configuration:

  public function testGrid() {
    $view = Views::getView('test_grid_responsive');
    foreach (['horizontal', 'vertical'] as $alignment) {
      $this->assertGrid($view, $alignment);
    }
  }

So there aren't going to be redundant assertions and we don't need $alignmentsTested.

But we can't simply remove the declaration of $alignmentsTested. We also have to modify

    // Validate test values.
    if (!in_array($alignment, $this->alignmentsTested)) {
      $result = $this->xpath('//div[contains(@class, "views-view-responsive-grid") and contains(@class, :alignment)]', [':alignment' => 'views-view-responsive-grid--' . $alignment]);
      $this->assertGreaterThan(0, count($result), ucfirst($alignment) . " grid markup detected.");
      $this->alignmentsTested[] = $alignment;
    }

such that any logic related to $alignmentsTested is removed.

Alternatively, we could add more cases to testGrid() and then $alignmentsTested would be useful.

mandclu’s picture

Status: Needs review » Needs work

@danflanagan8 you are spot on that the new test is adapted from the StyleGridTest.php. Unfortunately, writing unit tests isn't one of my strengths, so being able to leverage existing code is a big help. Can you recommend one or more tests elsewhere in core that I could use as examples to write the additional tests / cases needed to provided sufficient coverage here?

danflanagan8’s picture

Status: Needs work » Needs review

After communicating with @mandclu in Slack, I decided to take off my Reviewer Hat and put on my Test Writing Hat. I have pushed some updates that address my concerns with the tests as well as the comments from @lendude in #31.

I also did a rebase where I had to resolve a conflict in Stable9LibrariesOverrideTest. I think I got it right, but the git history looks kind of funky for the rebase. Hopefully I didn't mess anything up. I really struggle with the MR workflow.

Anyone want to review? :)

mherchel’s picture

Thanks for the work on this @danflanagan8!

For some reason, Gitlab said this had a merge error. I merged 10.0.x into it to bring it up to date (Git did not show me any merge conflicts)

danflanagan8’s picture

Thank you, @mherchel

For some reason, Gitlab said this had a merge error.

I must have done something wrong with the rebase I attempted. I'm very thankful you were able to fix it.

Hopefully we can get this across the finish line! As far as I know, the only outstanding task was to enhance the test coverage, which I have addressed. I will be happy to make additional changes to the test coverage if they are requested.

mherchel’s picture

Removed the pointless comments and responded to the thought on testing the CSS layout.

mherchel’s picture

Issue tags: -Needs follow-up
mherchel’s picture

Lendude’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record

Removing the 'Needs change record' since that was from a time when we were still thinking about deprecating the old grid here. That has been moved to the follow up so removing the tag here.

Nice to see the discussion on the possible test coverage, interesting to see what happens there.

I don't see anything else to nitpick, so moving to RTBC

catch’s picture

fwiw I've been running this patch on a site for 3-4 months with no problems (except keeping up with re-rolls). Not doing anything extreme with it, but it's working very well.

  • lauriii committed 6aed2e9 on 10.1.x
    Issue #3151553 by mherchel, mandclu, danflanagan8, catch, Lendude, Gábor...
lauriii’s picture

Committed 6aed2e9 and pushed to 10.1.x. Thanks!

Leaving open for a backport to 10.0.x once the freeze is over. I don't think this can be backported to 9.x given that this is fundamentally not compatible with IE 11.

  • lauriii committed 9eda852 on 10.0.x
    Issue #3151553 by mherchel, mandclu, danflanagan8, catch, Lendude, Gábor...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Cherry-picked to 10.0.x.

ressa’s picture

Thank you so much @mherchel, @mandclu, @danflanagan8, @catch, @Lendude, @lauriii and @Gábor for making this great improvement. It looks amazing. Especially the vertical style with the images automatically resizing and realigning looks almost magical :-)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.