Spin off from #2403873-7: FileFormatterBase does not retain unsaved entities (files).

Core has an entity_reference field type, but no formatters for it.
Only entity_reference.module provides formatters, there's no reason for that, they could be in Core.

Same reasoning applies to widgets (only provided by entity_ref.module), but the current implementations are tied to the notion of "selection handlers", so they can't be moved to Core so simply.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task: code reorg to allow greater reusability (provide a generic base class about EntityRef field type in Core rather than in a non-required module)
Issue priority Normal
Disruption Disruptive for contributed and custom modules that define custom formatters for ER fields (base class changes namespace)
Note : As mentioned in #26 / #27, this is part of a larger trend to get rid of entity_reference.module completely

Comments

yched’s picture

Title: entity_reference widgets and formatters should be in Core » entity_reference formatters should be in Core
Issue summary: View changes
yched’s picture

Status: Active » Needs review
StatusFileSize
new4.54 KB

Attached patch moves the formatters and their base class to Core

Status: Needs review » Needs work

The last submitted patch, 2: 2404021-e_r_formatters_Core-2.patch, failed testing.

yched’s picture

Status: Needs work » Needs review
StatusFileSize
new5.34 KB
new819 bytes

Need to update the remaining formatter that is based on EntityReferenceFormatterBase

yched’s picture

dawehner’s picture

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php
@@ -2,14 +2,15 @@
+// @todo

mh?

yched’s picture

Assigned: Unassigned » amateescu

Oh, right, that :-)

Yeah, I guess we need to move RecursiveRenderingException into Core as well, but having that exception class, dedicated to one very specific case in one specific formatter for one specific field type, lie around among the other formatter classes in Drupal\Core\Field\Plugin\Field\FieldFormatter feels a bit weird.

@amateescu, do you think we could get rid of that specific exception and use a more generic one ?
Or even : do we really want to throw an exception when rendering a reference tree too deep ? Shouldn't we rather silently stop rendering down the treee ?

Assigning to @amateescu for feedback

amateescu’s picture

Assigned: amateescu » Unassigned

I think that this specific exception is useful to keep because

1) it's not really tied to a single field type, file/image references could have the same problem via the file_entity module
and 2) it forces the site builder/content editor to address the problem instead of letting it go potentially unnoticed and maybe melting down the server if the "silently stop rendering down the tree" threshold is not low enough.

Otherwise, +1 to this issue!

jibran’s picture

Can we also move the ER formatters tests to core as well here?

larowlan’s picture

Yeah EntityReferenceItemTest is also in the er module

yched’s picture

@amateescu :

[the exception] it forces the site builder/content editor to address the problem instead of letting it go potentially unnoticed and maybe melting down the server if the "silently stop rendering down the tree" threshold is not low enough

I don't get the reasoning. Either "Exception when threshold reached" or "silently stop rendering down when threshold reached" do not change anything about "if threshold too high, server melts" ?

Throwing an exception means "site broken" on rare but random conditions (deep reference chain in the user submitted content) that are hard to prevent or fix.
Silently stopping the rendering at least means that nothing breaks in the rare case where this happens ?

amateescu’s picture

Silently stopping the rendering at least means that nothing breaks in the rare case where this happens ?

Sure, nothing will "break" in that case, except that it will be much harder to figure out why some content is not displayed, while the exception clearly states (in the dblog) what happened. We need to keep in mind that production sites will not display the exception itself (if configured properly), but a nice message that something went wrong and the site admin can easily see the problem in watchdog.

amateescu’s picture

I think my reply above is actually making a case to at least emit a warning in watchdog if we decide to get rid of the exception :)

amateescu’s picture

Thought a bit more about this and I realized that I was considering only the site builder/admin perspective, but this issue can also come up when a regular site user has access to edit an entity form with a reference field, in which case he cannot easily "do something about it".

So yes, let's drop the exception and switch to "silently stop rendering down when threshold reached" + logging.

Should we do it in this issue or in #2073753: Fix and add tests for the recursive rendering protection of the 'Rendered entity' formatter?

yched’s picture

StatusFileSize
new8.77 KB
new854 bytes

Something like this ?

[edit: wrong interdiff, see next comment]

yched’s picture

StatusFileSize
new4.06 KB

The correct interdiff :-)

yched’s picture

Status: Needs review » Needs work

The last submitted patch, 15: 2404021-e_r_formatters_Core-15.patch, failed testing.

yched’s picture

Status: Needs work » Active
StatusFileSize
new8.91 KB
new1.78 KB

ContainerFactoryPluginInterface, silly !

yched’s picture

Status: Active » Needs review
jibran’s picture

This looks good. Can you answer #9 and #10? Other then that it is RTBC.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new10.67 KB
new1.77 KB

Something like this ?

Yes, exactly! Looks perfect to me. Let's move those tests as well and get this in.

yched’s picture

Thanks @amateescu, I couldn't get to back to it these last couple days :-)

Note - two things I wasn't fully sure about the logging are:
- what to use for "channel"; patch goes with 'entity', but I'm not sure if there is an existing channel that would be more appropriate
- which logging level to use (alert / error / warning / notice...); patch does 'error'

amateescu’s picture

For the logging level, 'error' looked appropriate to me because the output of some pages is potentially broken. As for the channel, I don't have anything better than 'entity' in mind :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

This issue is a normal task so we need to outline how it fits within the allowable Drupal 8 beta criteria. Can someone add Drupal 8 beta phase evaluation template to the issue summary.

berdir’s picture

this issue makes me wonder how long we will actually continue to have entity_reference.module ;)

I think we're just left with widgets, the selection plugin, views integration and support for configurable entity reference fields in general (I recently noticed that it is not possible to have one, e.g. in a kernel test, due to missing/incomplete config schema support in core).

amateescu’s picture

this issue makes me wonder how long we will actually continue to have entity_reference.module ;)

Not too long in fact :) I'll finish that patch for #entity_autocomplete and #entity_select Form API elements soon which means the selection plugins will also be moved to Core, and after that moving everything else should be trivial.

yched’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community

Drupal 8 beta phase evaluation template added

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

Should any of entity reference's config schema be moving too?

yched’s picture

StatusFileSize
new12.91 KB
new2.23 KB

@alexpott : indeed, good call.

jibran’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update

Thanks back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This makes things easier for contrib and core - less dependencies.

Committed 3fe7cb5 and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation to the issue summary.

  • alexpott committed 3fe7cb5 on 8.0.x
    Issue #2404021 by yched, amateescu: entity_reference formatters should...

Status: Fixed » Closed (fixed)

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

dave reid’s picture

Count me as part of the very confused party that I thought I needed a dependency on entity_reference from contrib D8 code, but 8 months later have realized that core provides the field type now.