Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
It would be great if pseudo fields added via hook_field_extra_fields() could have their label actually displayed within the context of the node (or entity) being viewed in the same way "normal" fields have their label displayed. Currently, the simplest way that I have found to display an inline label for an extra field that fits in with the rest of my fields is to do something like:
<?php
$node->content['mymodule_pseudo_field'] = array(
'#prefix' => '<div class="field field-label-inline clearfix"><div class="field-label">My Pseudo Field</div>',
'#suffix' => '</div>',
'#markup' => '<div class="field-items">' . $my_pseudo_value . '</div>',
);
?>
This is a pretty messy way of doing it.
Comment | File | Size | Author |
---|---|---|---|
#15 | drupal8.field-system.1471706-15.patch | 3.25 KB | ao2 |
#5 | drupal8.field-system.1471706-5.patch | 3.09 KB | Pol |
|
Comments
Comment #1
mkadin CreditAttribution: mkadin commented+1 for this feature. It would be useful for EVA: Entity Views Attachment to implement.
Comment #2
charlie-s CreditAttribution: charlie-s commentedI ended up implementing a custom theme wrapper that does this in 1 place and is reused as I add custom fields to a particular node view. Here's an example:
This could be done in many places, but I think you get the idea. Still might be nice to have an extra field be displayed more like a traditional field without the need for the "mimic" code above.
Comment #3
Boobaa+1 for this feature; additionally, it would be great if one could select different formatters for extra fields as well.
Comment #4
PolI'm also looking for this.
We have to look into
\Drupal\field_ui\DisplayOverview::buildExtraFieldRow
I've replaced:
with
But I'm not able to 'save' the configuration of it.
I've looked in the Overview Form here:
\Drupal\field_ui\DisplayOverviewBase::submitForm
Modifying the loop for extra fields like this:
Allows me to save the 'label' property correctly.
We could also save the whole array, like that:
What do you think ?
Also, don't forget to change:
\Drupal\entity\EntityDisplayBase::getComponent
:Instead of returning the array containing the weight, I return the whole element/definition.
Now, to render that extra field correctly, it's here
\Drupal\node\NodeRenderController::buildContent
:$display->getComponent('language')
returns an array with the 'label' and the 'weight' as key and their respective values, so it's cool, we can retrieve the values we saved in the manage form display screen.Now, we need to find a way to use the field template, I'm still digging...
BTW, what happend when I render multiple node on a page, does the language field has always the same ID ?
Comment #5
PolAttaching a patch, it's easier to see the changes.
Comment #6
swentel CreditAttribution: swentel commentedI guess the best place is probably going to be EntityFormController::processForm()
- edit - at least for the forms side - should check where we assign/change the weights on the display side
- edit - I just realized that on the forms side there's no label, so that doesn't matter at all, scrap my comment ...
Comment #7
maijs CreditAttribution: maijs as a volunteer commentedIf you're interested in easily making extra fields look like real fields, check out Pseudo field contrib module.
Comment #8
kenorb CreditAttribution: kenorb commentedComment #10
johnvI tried to investigate how the title of extra_field is displayed on a view mode, using the Bartik theme on D8-rc2. I did not succeed :-(
I compared my own field from Field UI with the core 'Language' property (which is the only one exposed for view_modes).
In the display settings fo the Node type, you can set the label placing.
When viewing a node, Core/Field/FormatterBase.php::view() collects all info for each field. Both field and property are the same:
So the hook_field_extra_fields (title display and weight) are recollected correctly.
(Apparently, '#title_display' is for forms, '#label_display' is for view_modes.)
Core/Entity/EntityViewBuilder::buildMultiple() collects all info from hooks.
There, the '#label_display' has vanished for Language! (Why, where?) I added a hardcoded
$build_list[$key]['langcode']['#label_display'] = "inline";
I investigated the differences between the both formatters:
- Drupal\options\Plugin\Field\FieldFormatter\OptionsDefaultFormatter
- Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter
There are no diffences in handling the title/label.
This is all passed through Drupal\Core\Render\Renderer->doRender(), also with no apparent differences.
In modules/system/templates/field.html.twig, a variable 'label_display' is passed, but not used.
So, two questions remain:
- Why/where is the settings '#label_display' removed?
- When restored, why/where is it not used? Somewhere, there is a difference between fields and basefields/properties.
Comment #11
johnv@Pol, the first part of your search is already fulfilled: Core stores the title settings corectly in the Entity Configuration.
BTW. I'm interested in this issue for diaplsy properties of custom entities, not the language property per se.
Comment #15
ao2 CreditAttribution: ao2 as a volunteer commentedHi, I rebased the patch to 8.3.x just to see the effect, It looks like it allows to set the label behavior, but obviously this is not enough or render the label.
@Pol did you look into the rendering part?
I am going to look at the "Pseudo field" plugin next, but I was also wondering if in the long run having pseudo fields as a new FieldType and adding them using the Plugin API would make sense. Not an experiment I can do myself I am afraid.
Comment #19
iyyappan.govindHi All, I like this feature. I have initiated the testing with Drupal 8.9. Also this will fix the other issue - Implement visibility options for "Real name" field label.
Thanks
Comment #20
apadernoComment #22
vensires CreditAttribution: vensires commentedI don't know if we really need something more than the following:
I also have it as a snippet for D7 and for D8+ in https://gitlab.com/-/snippets/1749165.
Comment #23
junaidpvThis issue can be solved by using extra_field_plus module. See the example module within it.