There is some code in EntityViewController::view() that attempts to set the page title:

    if ($_entity instanceof ContentEntityInterface) {
      $label_field = $_entity->getEntityType()->getKey('label');
      if ($label_field && $_entity->getFieldDefinition($label_field)->getDisplayOptions('view')) {
        $build = $this->entityManager->getTranslationFromContext($_entity)
          ->get($label_field)
          ->view($view_mode);
        $page['#title'] = drupal_render($build, TRUE);

What this is doing is getting the label field's current field display settings for the current view mode, and using it to set the #title in $page, which is the overall render array that this method is returning.

So, there are two cases:

a) You have this field currently set to be displayed with some formatter. In this case, it is rendered and put into $page[#title]. Then it is also rendered again during the field rendering, and (as you have specified in the field settings), it is displayed again. So if the entity is being displayed on its own page, you're going to get the title field displayed once in an H1 as the Drupal page title (this is also the HTML title of the page, which is what you want). But then you will get the title displayed again in the fields section, as some kind of div as a field. This is undesirable -- you don't want it displayed twice.

Not only that, but if for some reason you had a label set up for the title field in your display settings, it would put something like "Title: Foo" as the HTML page title, the H1 Drupal page title, and then again in the field area. Ooops.

b) You have this field currently set to be hidden. In this case, when it is rendered using the field settings, the result is an empty string. So if the entity is being displayed on its own page, you get nothing for the HTML page title, and also nothing for the H1 Drupal page title. And it isn't displayed as its own field in the fields section. This is also undesirable.

So... I think that currently all you are left with is that you need to have a title callback in your route for entity viewing that sets the page title in a different way, not involving the field formatting settings.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Actually, I am not sure the title callback method for this would work either. I put this into my routing file:

mymodule.myentity.page:
  path: '/myentity/{myentity}'
  defaults:
    _entity_view: 'myentity.full'
    _title_callback: '\Drupal\mymodule\Controller\MyUrlController::entityTitle'
  requirements:
    _entity_access: 'myentity.view'

My entity type is set up to use the default view controller... It doesn't look like the title callback function is even getting called. Probably doing something wrong again. :)

jhodgdon’s picture

Issue summary: View changes
yched’s picture

Yes, not sure the callback should use the display configuration. Instead, it should do $entity->$title[0]-view(array('type' => 'string')) ?

(I cannot check the actual formatter name at the moment)

jhodgdon’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
1.17 KB

I tried replacing the above section of code in EntityViewController with:

        $build = $this->entityManager->getTranslationFromContext($_entity)
          ->get($label_field)
          ->view(array('type' => 'string'));

That sort of worked.. needs to hide the field label... let's see. Bingo! Here's a patch. Probably needs a test... have to run out but let's see what it breaks.

Status: Needs review » Needs work

The last submitted patch, 4: 2316949-fix-entity-title.patch, failed testing.

yched’s picture

Can't look at the code in context, but looks like there's a $view_mode var we don't use anymore ?

jhodgdon’s picture

The local variable $view_mode is still used in the rest of this method, to render the rest of the page/entity/fields/etc. It is just not used in the title rendering any more, because we do not want to use the view mode's settings for the title -- the whole point of this issue, right?

Anyway... This patch should fix the PHP error in one test from the last patch, which occurred because something in the chain of that $build = ... statement in comment #4 returned NULL. I think it was the getTranslationFromContext part.

Very small patch, didn't bother with interdiff, hope that is OK.

jhodgdon’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 7: 2316949-fix-entity-title-7.patch, failed testing.

jhodgdon’s picture

So, on that failing test: It looks like the Quick Edit module is relying on the formatter for the title getting into the title portion of the build. The failing assert's message is:

Title with data-quickedit-field-id attribute found.

So... This is not a good thing to do for regular entity viewing, but it is apparently behavior that QuickEdit is relying on.

I'm not sure how to resolve this... I think someone else will need to take it over.

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.

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.

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

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

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

tacituseu’s picture

Status: Needs work » Closed (outdated)
Issue tags: -