Steps to repeat:

1. Add a Text (plain) field to a config entity (Contact Form)
2. Edit the display of the field and check "Link to the Content" setting in the format settings.
3. Display the field in the default view mode
3. Submit such form .

Expected Results:

User successfully submits contact form.

Actual Results:

PHP Error: Drupal\Core\Entity\EntityMalformedException: The "contact_message" entity cannot have a URI as it does not have an ID in Drupal\Core\Entity\EntityBase->toUrl() (line 191 of /app/core/lib/Drupal/Core/Entity/EntityBase.php).

CommentFileSizeAuthor
#25 interdiff-3094366-19-25.txt5.06 KBmohit_aghera
#25 3094366-link-to-entity-error-25.patch4.68 KBmohit_aghera
#19 3094366-link-to-entity-error-19--test-only.patch2.71 KBmohit_aghera
#19 interdiff-3094366-link-to-entity-error-13-19.txt2.71 KBmohit_aghera
#19 3094366-link-to-entity-error-19.patch5.04 KBmohit_aghera
#18 interdiff-3094366-link-to-entity-error-13-18.txt2.71 KBmohit_aghera
#18 3094366-link-to-entity-error-18.patch5.04 KBmohit_aghera
#18 3094366-link-to-entity-error-17--test-only.patch2.98 KBmohit_aghera
#13 3094366-link-to-entity-error-13.patch2.33 KBKittenDestroyer
#11 3094366-link-to-entity-error-11.patch4.65 KBKittenDestroyer
#8 3094366-link-to-entity-error-8.patch841 bytesKittenDestroyer
#7 3094366-link-to-entity-error-7.patch1.63 KBKittenDestroyer
#5 3094366-link-to-entity-error-5.patch1.68 KBKittenDestroyer
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

KittenDestroyer created an issue. See original summary.

KittenDestroyer’s picture

I think the solution must be implemented in \Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter::viewElements()
This error happens also in case of Preview of an new entity as seen in:
This issue

ravi.shankar’s picture

Status: Needs work » Active
KittenDestroyer’s picture

Version: 8.7.9 » 8.8.x-dev
ravi.shankar’s picture

Status: Needs review » Needs work

Patch #5 didn't pass the test so move to need work.

KittenDestroyer’s picture

Status: Needs work » Needs review

There was some issue with file formating on my side. Fixed.

rensingh99’s picture

Status: Needs review » Needs work

Hi @KittenDestroyer,

The message entity doesn't have the links like canonical that is being used in getEntityUrl.

protected function getEntityUrl(EntityInterface $entity) {
    // For the default revision, the 'revision' link template falls back to
    // 'canonical'.
    // @see \Drupal\Core\Entity\Entity::toUrl()
    $rel = $entity->getEntityType()->hasLinkTemplate('revision') ? 'revision' : 'canonical';
    return $entity->toUrl($rel);
  }

That's why this error is coming.

if ($this->getSetting('link_to_entity') && !$entity->isNew()) {
      $url = $this->getEntityUrl($entity);
    }

The condition which you added "!$entity->isNew()" will affect all entity. So, it will be good to hide the "Link to the Content" option for that entity, which doesn't have canonical links.

Thanks,
Ren

KittenDestroyer’s picture

Hi @rensingh99,

Yes you are right. I've added checks for this case in StringFormatter

KittenDestroyer’s picture

Status: Needs work » Needs review
edmund.dunn’s picture

@KittenDestroyer #13 worked for me.

rensingh99’s picture

Hi @KittenDestroyer

$entity = $items->getEntity();
    $entity_type = $entity->getEntityType();

    if ($this->getSetting('link_to_entity') && !$entity->isNew() && $entity_type->hasLinkTemplate('canonical')) {
      $url = $this->getEntityUrl($entity);
    }

There are no need to check the entity is a new condition. So, it should be removed.

Thanks,
Ren

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

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

Berdir’s picture

Status: Needs review » Needs work
Issue tags: -fields, -contact form +Needs tests

Yes, we do need the !isNew() check, that fixes node preview with a field is configured like that.

This does need a test.

mohit_aghera’s picture

@Berdir
I've kept isNew() check as it is.
Added following items:
- Add test only patch
- Add actual test cases.

mohit_aghera’s picture

Status: Needs review » Needs work

The last submitted patch, 19: 3094366-link-to-entity-error-19--test-only.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
Berdir’s picture

+++ b/core/modules/field/tests/src/Functional/String/StringFieldTest.php
@@ -112,4 +113,55 @@ public function _testTextfieldWidgets($field_type, $widget_type) {
+   * Test "link_to_entity" feature on fields which are added to config entity.
+   */
+  public function testLinkToContentForConfigEntity() {
+    $field_storage = FieldStorageConfig::create([
+      'type' => 'string',
+      'entity_type' => 'contact_message',
+      'field_name' => $field_name = strtolower($this->randomMachineName()),
+    ]);

A bit unsure about adding a dependency ton contact module in field module, even if it's just a test.

What about doing it with a test entity type in a kernel test, like the existing test coverage in \Drupal\Tests\field\Kernel\String\StringFormatterTest::testStringFormatter? We can either use a test entity type or any of the content entity types in web/core/modules/system/tests/modules/entity_test/src/Entity, it doesn't actually need to be a config entity type.

mohit_aghera’s picture

Status: Needs review » Needs work

@Berdir I think we probably need a config entity for the same.
I tried to reproduce for node and user by adding a field and following the steps, I was not able to reproduce it.
I'll try to refactor it and implement it with Kernel tests.

Berdir’s picture

Sure, node/user is fine, those have a canonical link template, you need something that doesn't. For example \Drupal\entity_test\Entity\EntityTestLabel.

mohit_aghera’s picture

Assigned: KittenDestroyer » Unassigned
Status: Needs work » Needs review
FileSize
4.68 KB
5.06 KB

@Berdir
I've refactored the test cases and now we are using kernel tests.
Currently, I've just added test cases for a testing entity by doing render.

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php
@@ -92,14 +92,15 @@ public static function defaultSettings() {
+    if ($entity_type->hasLinkTemplate('canonical')) {
+      $form['link_to_entity'] = [
+        '#type' => 'checkbox',
+        '#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
+        '#default_value' => $this->getSetting('link_to_entity'),
+      ];

Here in the above snippet, we are displaying link_to_entities checkbox for the entities which has a canonical path.
Do you think if we need to cover this under functional tests and check if the checkbox is visible to certain entities only?

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Good question. As far as I see, we don't have existing test coverage for that checkbox nor the summary output and generally rarely do for formatter settings UI I think. So maybe we can get this in without, but will have to be decided by the core maintainers, lets see what they think :)

I did run the the test locally and verified that it fails without the fix.

  • catch committed 2951df5 on 9.2.x
    Issue #3094366 by mohit_aghera, KittenDestroyer, Berdir, rensingh99:...

  • catch committed 776e19d on 9.1.x
    Issue #3094366 by mohit_aghera, KittenDestroyer, Berdir, rensingh99:...
catch’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Agreed we don't need to test the individual formatter settings form here, testing the entity display config seems fine.

Committed/pushed to 9.2.x and cherry-picked to 9.1.x, thanks!

Status: Fixed » Closed (fixed)

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