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
Comment | File | Size | Author |
---|---|---|---|
#15 | 2226171-15.patch | 2.88 KB | LinL |
#10 | 2226171-do-not-test.patch | 1.76 KB | LinL |
Comments
Comment #1
LinL CreditAttribution: LinL commentedOn 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.
Comment #2
valthebaldThe difference between article and other standard content types is existence of image field. I'm looking at how it is possible to avoid this
Comment #3
valthebaldComment #4
valthebaldComment #5
valthebaldComment #6
LinL CreditAttribution: LinL commentedThanks @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.
Comment #7
valthebald@LinL: I think so too, tagging
Comment #8
valthebaldHere are two patches, one is just adding the test (which fails), the second contains the new test and the fix
Comment #10
LinL CreditAttribution: LinL commentedSame again with a small change to comments.
Comment #11
danylevskyiPatch worked for me. The code seems valid. RTBC.
Comment #12
valthebaldComment #13
larowlanThese parameters need type hints
Comment #14
webchickComment #15
LinL CreditAttribution: LinL commentedThanks @larowlan. Type hints added.
Also rerolled for changes in #2215961: Entity::urlInfo() should return \Drupal\Core\Url
Comment #16
mallezieComment #17
valthebaldLast patch is the same as #11, with added type hinting, so RTBC
Comment #18
webchickAwesome, thanks!
Committed and pushed to 8.x.