Can't preview a new node

Created on 4 May 2016, almost 9 years ago
Updated 5 June 2023, almost 2 years ago

To reproduce:

  1. Add a disqus comment field to a content type, eg: article
  2. Go to the page to a new node, at eg: /node/add/article
  3. Enter any required fields, like title
  4. Click "Preview" to preview the node

The following error is generated:

Drupal\Core\Entity\EntityMalformedException: The "node" entity cannot have a URI as it does not have an ID in Drupal\Core\Entity\Entity->toUrl() (line 184 of core/lib/Drupal/Core/Entity/Entity.php). Drupal\disqus\Plugin\Field\FieldFormatter\DisqusFormatter->viewElements(Object, 'en') (Line: 85) Drupal\Core\Field\FormatterBase->view(Object, NULL) (Line: 264) Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array) (Line: 308) Drupal\Core\Entity\EntityViewBuilder->buildComponents(Array, Array, Array, 'default') (Line: 29) Drupal\node\NodeViewBuilder->buildComponents(Array, Array, Array, 'default') (Line: 251) Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 208) Drupal\Core\Entity\EntityViewBuilder->build(Array) call_user_func(Array, Array) (Line: 384) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200) Drupal\Core\Render\Renderer->render(Array, ) (Line: 231) Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 577) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 232) Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 122) Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 95) Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 116) Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 144) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 62) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 53) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 103) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 82) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 51) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 55) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 637) Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

The cause of this is Disqus trying to render comments when the entity has no ID (is not yet saved). I think the correct behavior would be to just render nothing in that case, or maybe some sort of placeholder "disqus comments would go here".

A workaround for now is to disable access to the field for unsaved entities:

function mysite_deploy_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if ($field_definition->getType() == 'disqus_comment') {
    $entity = $items->getEntity();
    if (empty($entity->id())) {
      return AccessResult::forbidden();
    }
  }

  return AccessResult::neutral();
}
πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada vasi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024