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.
Comment | File | Size | Author |
---|---|---|---|
#7 | 2316949-fix-entity-title-7.patch | 1.32 KB | jhodgdon |
#4 | 2316949-fix-entity-title.patch | 1.17 KB | jhodgdon |
Comments
Comment #1
jhodgdonActually, I am not sure the title callback method for this would work either. I put this into my routing file:
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. :)
Comment #2
jhodgdonComment #3
yched CreditAttribution: yched commentedYes, 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)
Comment #4
jhodgdonI tried replacing the above section of code in EntityViewController with:
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.
Comment #6
yched CreditAttribution: yched commentedCan't look at the code in context, but looks like there's a $view_mode var we don't use anymore ?
Comment #7
jhodgdonThe 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.
Comment #8
jhodgdonComment #10
jhodgdonSo, 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.
Comment #14
tacituseu CreditAttribution: tacituseu commentedIt was rewritten in #2498849: Entity view controller title rendering is expensive.