Updated: Comment #10

Problem/Motivation

Steps to reproduce:

1. New install of D8

2. Go to "Add Content"

3. Choose "Article"

3. Enter a title, hit "Preview" button

4. Gives error page with this error:

Drupal\Core\Entity\EntityMalformedException: The "node" entity type has not been saved, and cannot have a URI. in Drupal\Core\Entity\Entity->urlInfo() (line 148 of core/lib/Drupal/Core/Entity/Entity.php).

This only happens with Article. Basic page and created content types work as expected.

Proposed resolution

The difference between article and other content types defined by standard profile is the field_image field. The problem occurs in \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter::viewElements():

    $image_link_setting = $this->getSetting('image_link');
    // Check if the formatter involves a link.
    if ($image_link_setting == 'content') {
      $uri = $items->getEntity()->urlInfo();
      // @todo Remove when theme_image_formatter() has support for route name.
      $uri['path'] = $items->getEntity()->getSystemPath();
    }

call to Drupal\Core\Entity\Entity::urlnfo() fails, since it contains this check in the very beginning:

    if ($this->isNew()) {
      throw new EntityMalformedException(sprintf('The "%s" entity type has not been saved, and cannot have a URI.', $this->getEntityTypeId()));
    }

Proposed solution is to check if $entity->isNew() before trying to get it's uri

Remaining tasks

Test has been added

User interface changes

No change in UI

API changes

No change in API

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

LinL’s picture

On further testing it seems to be the image field that is causing the problem.

If we delete the image field from the Article content type, then Preview works just fine.

Also if we add an image to our own content type, Preview works fine, so it appears to be the initial set up of the image field in the Article content type that causes the problem.

valthebald’s picture

Assigned: Unassigned » valthebald

The difference between article and other standard content types is existence of image field. I'm looking at how it is possible to avoid this

valthebald’s picture

Issue summary: View changes
valthebald’s picture

Status: Active » Needs review
Issue tags: +d.o szeged sprints
FileSize
1.11 KB
valthebald’s picture

Issue summary: View changes
LinL’s picture

Component: entity system » image system

Thanks @valthebald. Looks good. I've manually tested with Article, Basic page and my own content type and all works as expected.

I guess we probably do need to add a test?

Also I changed the component to image system.

valthebald’s picture

Issue tags: +Need tests

@LinL: I think so too, tagging

valthebald’s picture

Here are two patches, one is just adding the test (which fails), the second contains the new test and the fix

Status: Needs review » Needs work

The last submitted patch, 8: 2226171-8.patch, failed testing.

LinL’s picture

Assigned: valthebald » Unassigned
Status: Needs work » Needs review
Issue tags: -Need tests
FileSize
1.76 KB
2.87 KB

Same again with a small change to comments.

danylevskyi’s picture

Status: Needs review » Reviewed & tested by the community

Patch worked for me. The code seems valid. RTBC.

valthebald’s picture

Issue summary: View changes
larowlan’s picture

* @param $image
+   *   A file object representing the image to upload.
+   * @param $field_name
+   *   Name of the image field the image should be attached to.
+   * @param $type
+   *   The type of node to create.
+   */

These parameters need type hints

webchick’s picture

Status: Reviewed & tested by the community » Needs work
LinL’s picture

Status: Needs work » Needs review
FileSize
2.88 KB

Thanks @larowlan. Type hints added.

Also rerolled for changes in #2215961: Entity::urlInfo() should return \Drupal\Core\Url

mallezie’s picture

Issue tags: -d.o szeged sprints +drupal.org szeged sprint
valthebald’s picture

Status: Needs review » Reviewed & tested by the community

Last patch is the same as #11, with added type hinting, so RTBC

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Awesome, thanks!

Committed and pushed to 8.x.

  • Commit 660165c on 8.x by webchick:
    Issue #2226171 by LinL, valthebald: Preview in Create Article gives...

Status: Fixed » Closed (fixed)

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