Problem/Motivation

In D7 you could setup a view to taxonomy/term/% and pass it the term. Internally it loaded the term entity and automatically converted it to the TID itself.

At the same time it had the ability to set the title of the block to be the term name itself.
For some reason this entire functionality, both for users and for terms etc. just disappeared somehow in the meantime.

This appears to still work for page views.

Proposed resolution

  • Add it back (if possible in a general way for all entities)
  • Add proper test coverage

Remaining tasks

do it!

User interface changes

API changes

Comments

dawehner’s picture

Priority: Critical » Major

I do agree that this is not critical but its major and we really should fix it, in case possible before the release.

Jaesin’s picture

A temporary workaround in the theme layer for views blocks:


function theme_preprocess_block(&$elements) {
  // Check to see if this is a view block.
  if(!empty($elements['content']['#view']) 
    && $elements['content']['#view'] instanceof \Drupal\views\ViewExecutable
  ) {
    $view = $elements['content']['#view'];
    $view->execute();
    // Make sure the label is supposed to be displayed.
    if ($elements['configuration']['label_display']
      && !empty($view->build_info['title'])
      && !empty($view->build_info['substitutions'])
    ) {
      // Set the label.
      $elements['label'] = t($view->build_info['title'], $view->build_info['substitutions']);
  }
}
jhedstrom’s picture

Title: [regression] Argument validations doesn't set the title anymore » [regression] Argument validations doesn't set the block title anymore
Issue summary: View changes

Updating the IS as it took me a bit to figure out this was about setting block titles. I tested and it works for page titles.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

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

xjm’s picture

Title: [regression] Argument validations doesn't set the block title anymore » Argument validations doesn't set the block title anymore
Issue summary: View changes
Priority: Major » Normal
Issue tags: +Needs followup
StatusFileSize
new22.63 KB
new15.74 KB
new89.39 KB

@alexpott, @dawehner, @timplunkett, and I discussed this issue and agreed it does not need to be major priority. While this does indeed sound like a bug, its impact is limited to a specific usecase. (Also, it's technically not a regression for core.)

We did agree that this should be treated as a bug. Steps to reproduce:

  1. Add a taxonomy term (tid 1).
  2. Go to /admin/structure/views/view/taxonomy_term
  3. Add a block display (which will inherit the configuration for the page display).
  4. Click on the link to the contextual filter and confirm that it is configured to override the title.
  5. Change the default behavior for the block's contextual filter to provide a default value for the term (in order to test the argument being passed to the block easily).
  6. Go to /admin/structure/block and place the block.
  7. View the placed block on any page. it might seem to work (the term name appears at the top of the block) but this is actually part of the view header, not the block title.
  8. Go back to configure the view at /admin/structure/views/view/taxonomy_term and add a title for the view overall (which should be overridden).
  9. Visit /taxonomy/term/1 and see that the page title overrides the overall title, but the block does not.

I also checked whether there was test coverage for the page title case. It does not look like Views itself has test coverage for this functionality for arguments:

[mandelbrot:Tests | Thu 18:55:54] $ grep -r "assertTitle" *
Handler/AreaTitleWebTest.php:    $this->assertTitle('test_title_header | Drupal');
Handler/AreaTitleWebTest.php:    $this->assertTitle('test_title_empty | Drupal');
Handler/AreaTitleWebTest.php:    $this->assertTitle('test_title_header | Drupal');
Plugin/DisplayPageWebTest.php:    $this->assertTitle(t('Test default page | Drupal'));
Plugin/DisplayPageWebTest.php:    $this->assertTitle(t('Test local page | Drupal'));

So, adding tests for the bug would be a good next step.


The main usecase in core is the taxonomy term view, which would merit its own test coverage. In TermTest, there is some related coverage, but it is not sufficient:

    // View the term and check that it is correct.                              
    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertText($edit['name[0][value]'], 'The randomly generated term name is present.');
    $this->assertText($edit['description[0][value]'], 'The randomly generated term description is present.');

TaxonomyTermViewTest has similar assertions:

    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertText($term->label());
    $this->assertText($node->label());

Since the term page has the rendered term in the header as well, that means we could get a false positive even if the page title were not overridden. So we should add test coverage for that. Test coverage for a specific view is out of scope in this issue, so it should be done in a separate issue. (The followup would check assertTitle() and also assertNoText() for the overridden one.) Tagging for a followup for that.

xjm’s picture

I wonder if this bug has a similar cause as #2629566: Label is not shown for blocks with exposed forms.

dawehner’s picture

I wonder if this bug has a similar cause as #2629566: Label is not shown for blocks with exposed forms.

It could be, but I would rather think the problem is here:

  public function build() {
    $this->view->display_handler->preBlockBuild($this);

    // We ask ViewExecutable::buildRenderable() to avoid creating a render cache
    // entry for the view output by passing FALSE, because we're going to cache
    // the whole block instead.
    if ($output = $this->view->buildRenderable($this->displayID, [], FALSE)) {
      // Override the label to the dynamic title configured in the view.
      if (empty($this->configuration['views_label']) && $this->view->getTitle()) {

The buildRenderable() doesn't necessarily fire any kind of code in views, but rather just returns a lazy render array, see \Drupal\views\Plugin\views\display\DisplayPluginBase::buildRenderable

This basically means that getTitle() at that point cannot rely on anything dynamic, like arguments.

I think one valid approach here would be to not use a lazy render array for views blocks, because we already have render level caching on the block level. When we would not execute things lazily anymore, we would then process the arguments all the times and have the title, as expected.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

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

evanjenkins’s picture

Uploading patch that worked for me!

zekvyrin’s picture

Status: Active » Needs review

Marking it to needs review to trigger testbot

zekvyrin’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Dublin2016

patch #9 by evanjenkins is working and seems very reasonable to me.

Filling view_build array (which build the view with filters etc) are done in preRenderViewElement (line 42 currently) where the "executedisplay" happens. This is where the view title is filled, so the block title fill should happen after this. That's why I think this addresses the issue very well.

The alternative dawehner said (execute & build more things to buildRenderable) would require to process the view display earlier (running view->executeDisplay in buildrenderable instead ?).

Also, this bug is from 8.0 so I think we need to backport it as well.

I'm marking it to RTBC (although I do not know if tests are needed for this issue or are needed generally in this area)

Also adding the Dublin2016 tag as I'm in the sprints :P

zekvyrin’s picture

Note:

We have to be careful though with the related issue #2629566: Label is not shown for blocks with exposed forms (mentioned by xjm) as it also contains a change in the same line (a wrapper function for these lines).

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: core-argument_validation_no_title-2361481-8-8.2.x.patch, failed testing.

lendude’s picture

I do not know if tests are needed for this issue

Yeah, they are, so leaving at needs work for adding test coverage.

Frando’s picture

Status: Needs work » Needs review

I can confirm that the patch in #9 works as expected (while debugging, I came up with the exact same solution).

What else is needed to get this in? Can someone write a test to make sure that this won't get lost again?

dawehner’s picture

@Frando
Yes exactly, at this point, we need a test, that should be it. Maybe @Lendude would like to work on that?

lendude’s picture

Issue tags: -Needs tests
StatusFileSize
new1.43 KB
new3 KB

@dawehner Yeah I think I can work on that :)

Test only patch is the interdiff.

The last submitted patch, 17: 2361481-17-TEST_ONLY.patch, failed testing.

Frando’s picture

Status: Needs review » Reviewed & tested by the community

Nice, thanks for the tests. I came up with the same solution and tested the patch manually as well. Setting to RTBC.

dawehner’s picture

+++ b/core/modules/views/tests/src/Kernel/Plugin/ViewsBlockTest.php
@@ -51,4 +52,26 @@ public function testMachineNameSuggestion() {
+   * Tests that ViewsBlock::build() produces the right output with title tokens.
+   *
+   * @see \Drupal\views\Plugin\Block::build()
+   */
+  public function testBuildWithTitleToken() {
+    $view = Views::getView('test_view_block');
+    $view->setDisplay();
+

I'm highly confused that the test don't have arguments

lendude’s picture

Status: Reviewed & tested by the community » Needs work

I'm highly confused that the test don't have arguments

Well the fix has nothing to do with arguments either. I just looked at the fix and wrote a test for that, I didn't look to closely at the rest of the discussion here, my bad! But it seems that the bug here is more general then just argument validation not being able to set the block title.

Nothing that influences the view title in preRenderViewElement is transferred to the block title. In this case, at the least tokens and overrides.

But yeah, lets see if we can add some coverage for overrides too.

lendude’s picture

StatusFileSize
new1.54 KB
new4.25 KB

Here we go, setting an argument with a fixed value and a title override.

lendude’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you a lot lendude! Its always nice to increase test coverage.

  • catch committed 0d71430 on 8.3.x
    Issue #2361481 by Lendude, evanjenkins, xjm, dawehner, Zekvyrin:...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.3.x and cherry-picked to 8.2.x. Thanks!

  • catch committed 5f5e0f6 on 8.2.x
    Issue #2361481 by Lendude, evanjenkins, xjm, dawehner, Zekvyrin:...

  • xjm committed 2e81413 on 8.2.x
    Revert "Issue #2361481 by Lendude, evanjenkins, xjm, dawehner, Zekvyrin...

  • xjm committed 113f7b5 on 8.3.x
    Revert "Issue #2361481 by Lendude, evanjenkins, xjm, dawehner, Zekvyrin...
xjm’s picture

Status: Fixed » Needs work

This introduced a test failure in HEAD on PHP7/Postgres:
https://www.drupal.org/pift-ci-job/527093

I've reverted that -- let's run tests on all environments for this one before commit once we've sorted the postgres fail. Thanks!

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new940 bytes
new4.58 KB

Wonky sort order on Postgres. Setting a fixed sort order makes the test pass locally on Postgres.

catch’s picture

Status: Needs review » Reviewed & tested by the community

That looks great, back to RTBC.

  • xjm committed 469d128 on 8.3.x
    Issue #2361481 by Lendude, evanjenkins, xjm, dawehner, Zekvyrin:...

  • xjm committed e5c1843 on 8.2.x
    Issue #2361481 by Lendude, evanjenkins, xjm, dawehner, Zekvyrin:...
xjm’s picture

Looks good now. I also re-tested with my STR from #5 and confirmed it did at least resolve the issue. Committed 469d128 and pushed to 8.3.x and 8.2.x. Thanks!

catch’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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