Right now ERR fields can be displayed using the format "Rendered Entity".

Normal entity reference fields, on the other hand, can be rendered using "Rendered entity", "ID", and "Label".

The feature request is to add field formatters of ID and Label to the ERR field types.

I'm working on this.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ashrafabed created an issue. See original summary.

ashrafabed’s picture

Assigned: Unassigned » ashrafabed
Issue summary: View changes
ashrafabed’s picture

The attached patch adds support for an ERR label formatter, linked or unlinked.

It shows the label for the appropriate entity revision, and it links to the appropriate revision as well. The matches core's Entity Reference Label Formatter almost exactly.

Notes for a code reviewer:
The differences from core's label formatter are the namespace, most mentions of 'entity reference' are replaced with 'entity reference revisions', and "$uri = $entity->urlInfo();" was changed to "$uri = $entity->urlInfo('revision');" so that the URL points to the appropriate entity revision.

ashrafabed’s picture

Status: Active » Needs review
ashrafabed’s picture

Assigned: ashrafabed » Unassigned
miro_dietiker’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Hello, thank you for the submission.

Such a new feature needs full test coverage.

Also even such a simple implementation adds maintenance complexity. We need a real world usecase explained where this functionality is used.

The most common use case „Paragraphs“ does not apply here as it doesn‘t have a label.

ashrafabed’s picture

StatusFileSize
new8.16 KB

I've attached a patch which tests the label functionality. It is based off of core's tests for the entity reference field's label formatter.

"We need a real world usecase explained where this functionality is used."
https://debugacademy.com needs this functionality today:
- "Class" entities are created
- They have ERR homework and classwork fields which point to the "assignment" entities
- When a class happens, we reference the homework + classwork + etc using the ERR fields
- On the display side, we display links to the homework + classwork + etc from the ERR fields. we do not want to display the entire homework and classwork contents on the same page.
- The reason we are using ERR fields is because homework and classwork assignments are modified every semester. We want people to be able to look at their "Class" entities, and see links to the appropriate revision of the homework/classwork assignments. This way they are always taken to their semester's homework/classwork revisions, rather than the latest

I expect there are many other use cases for this as well. If the module is to be used outside of paragraphs, having a 'label' formatter seems to be very necessary.

ashrafabed’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 7: entity_reference_revisions-field_formatter_label-2937835-5_2-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ashrafabed’s picture

StatusFileSize
new8.42 KB

Taking another stab at getting the automated tests to pass.

ashrafabed’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 10: entity_reference_revisions-field_formatter_label-2937835-10-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ashrafabed’s picture

StatusFileSize
new9.43 KB

Let's see if this will work.

ashrafabed’s picture

Status: Needs work » Needs review
ashrafabed’s picture

StatusFileSize
new9.46 KB

Correction.

The last submitted patch, 13: entity_reference_revisions-field_formatter_label-2937835-12-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 15: entity_reference_revisions-field_formatter_label-2937835-15-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ashrafabed’s picture

StatusFileSize
new10.03 KB

More progress on the tests.

ashrafabed’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 18: entity_reference_revisions-field_formatter_label-2937835-18-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ashrafabed’s picture

StatusFileSize
new10.29 KB

Getting much closer.

ashrafabed’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 21: entity_reference_revisions-field_formatter_label-2937835-21_2-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ashrafabed’s picture

StatusFileSize
new10.28 KB

Sorry for the patch spam. I believe it's almost ready.

ashrafabed’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 24: entity_reference_revisions-field_formatter_label-2937835-24-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ashrafabed’s picture

I wonder if there's something wrong with the test runner right now. The error messages have increased in number and are now along the lines of:
"Caused by
PDOException: SQLSTATE[HY000] [1049] Unknown database 'jenkins_drupal8_contrib_patches_20547'"

I'll try running the test again later.

Edit: It does seem like these errors are related to the testing environment rather than the tests themselves ( https://www.drupal.org/project/drupal/issues/2906317 )

ashrafabed’s picture

StatusFileSize
new10.49 KB

Fingers crossed..

ashrafabed’s picture

StatusFileSize
new10.7 KB

Another test issue fixed.

ashrafabed’s picture

StatusFileSize
new10.68 KB
ashrafabed’s picture

StatusFileSize
new9.19 KB

Should (hopefully) pass now.

ashrafabed’s picture

Status: Needs work » Needs review
mpolishchuck’s picture

I think need improvement here.
In some cases we have entity_reference_revisions field, but we need to show link (and label as well) to the current entity revision.
This patch add this capability. It's based on the patch from comment #31.

So label formatter now have "Ignore referenced revision" setting. If it's set to true - the formatter will show label/link to the current entity revision (instead of actually linked).

Also the patch adds overridden EntityReferenceRevisionsFormatterBase::getEntitiesToView() which can be used to get the same behavior for other for other formatters.

2pha’s picture

I am just putting this here for anyone who comes here looking for a quick fix to get field formatters for entity_reference_revisions.
I implement the standard entity reference formatters on entity reference revisions with this simple function in a module:

/**
 * Apply entity_reference formatters to entity_reference_revisions.
 * @param array $info
 */
function MYMODULE_field_formatter_info_alter(array &$info) {
  foreach($info as &$field) {
    if(in_array('entity_reference', $field['field_types'])) {
      $field['field_types'][] = 'entity_reference_revisions';
    }
  }
  // Remove reference.
  unset($field);
}

Though, this wont take the revision into account when outputting the field (I don't think).

Maybe this could be implemented into the module and then formatters specific to this module could be implemented which just extend the entity_reference formatters and override which entity is loaded.

Just a thought and I haven't really looked into it.

sinn’s picture

Updated patch 31.

Configuration for formatter has been added, fixed Drupal coding standards, renamed deprecated functions. Test has been rewritten - composite entities (like paragraph) don't have own pages, so no need to add routes to them.

sinn’s picture

Fix test for D9.

joevagyok’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed and tested, moving it to RTBC.

urvashi_vora made their first commit to this issue’s fork.

vensires made their first commit to this issue’s fork.

Berdir made their first commit to this issue’s fork.

  • Berdir committed 1ed6a5a6 on 8.x-1.x authored by urvashi_vora
    Issue #2937835 by ashrafabed, sinn, mpolishchuck: Add additional field...
berdir’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs tests

Merged, thanks.

Status: Fixed » Closed (fixed)

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

ab.shakir changed the visibility of the branch 2937835-add-additional-field to hidden.

ab.shakir changed the visibility of the branch 2937835-add-additional-field to active.

ab.shakir’s picture

Assigned: Unassigned » ab.shakir

The ID formatter also has multiple real world use cases although it does not make much sense for Paragraphs. In my case, I have a reference field using the ERR and I want to use the ID in views re-write results while in another scenario I'm trying to embed another view with the help of Views Field View and passing the ID value as a contextual filter.

ab.shakir’s picture

Assigned: ab.shakir » Unassigned
Related issues: +#3499568: Add the ID field formatter