diff --git a/core/modules/comment/src/Plugin/views/field/EntityLink.php b/core/modules/comment/src/Plugin/views/field/EntityLink.php index 9760057103..e37c402e44 100644 --- a/core/modules/comment/src/Plugin/views/field/EntityLink.php +++ b/core/modules/comment/src/Plugin/views/field/EntityLink.php @@ -74,6 +74,9 @@ public function preRender(&$values) { */ public function render(ResultRow $values) { $entity = $this->getEntity($values); + if (!$entity) { + return ''; + } // Only render the links, if they are defined. return !empty($this->build[$entity->id()]['links']['comment__comment']) ? \Drupal::service('renderer')->render($this->build[$entity->id()]['links']['comment__comment']) : ''; diff --git a/core/modules/comment/src/Plugin/views/field/LinkApprove.php b/core/modules/comment/src/Plugin/views/field/LinkApprove.php index 683c3cbb4c..86687f52f0 100644 --- a/core/modules/comment/src/Plugin/views/field/LinkApprove.php +++ b/core/modules/comment/src/Plugin/views/field/LinkApprove.php @@ -19,7 +19,11 @@ class LinkApprove extends LinkBase { * {@inheritdoc} */ protected function getUrlInfo(ResultRow $row) { - return Url::fromRoute('comment.approve', ['comment' => $this->getEntity($row)->id()]); + $entity = $this->getEntity($row); + if (!$entity) { + return NULL; + } + return Url::fromRoute('comment.approve', ['comment' => $entity->id()]); } /** diff --git a/core/modules/comment/src/Plugin/views/field/LinkReply.php b/core/modules/comment/src/Plugin/views/field/LinkReply.php index 3ddbd08152..816f341a6c 100644 --- a/core/modules/comment/src/Plugin/views/field/LinkReply.php +++ b/core/modules/comment/src/Plugin/views/field/LinkReply.php @@ -21,6 +21,9 @@ class LinkReply extends LinkBase { protected function getUrlInfo(ResultRow $row) { /** @var \Drupal\comment\CommentInterface $comment */ $comment = $this->getEntity($row); + if (!$comment) { + return NULL; + } return Url::fromRoute('comment.reply', [ 'entity_type' => $comment->getCommentedEntityTypeId(), 'entity' => $comment->getCommentedEntityId(), diff --git a/core/modules/contact/src/Plugin/views/field/ContactLink.php b/core/modules/contact/src/Plugin/views/field/ContactLink.php index a98be137dd..af72aa3313 100644 --- a/core/modules/contact/src/Plugin/views/field/ContactLink.php +++ b/core/modules/contact/src/Plugin/views/field/ContactLink.php @@ -30,7 +30,11 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { * {@inheritdoc} */ protected function getUrlInfo(ResultRow $row) { - return Url::fromRoute('entity.user.contact_form', ['user' => $this->getEntity($row)->id()]); + $entity = $this->getEntity($row); + if (!$entity) { + return NULL; + } + return Url::fromRoute('entity.user.contact_form', ['user' => $entity->id()]); } /** @@ -38,6 +42,9 @@ protected function getUrlInfo(ResultRow $row) { */ protected function renderLink(ResultRow $row) { $entity = $this->getEntity($row); + if (!$entity) { + return ''; + } $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = $this->getUrlInfo($row); diff --git a/core/modules/media_library/media_library.module b/core/modules/media_library/media_library.module index 7c29b9d0a5..1872d841bd 100644 --- a/core/modules/media_library/media_library.module +++ b/core/modules/media_library/media_library.module @@ -122,7 +122,10 @@ function media_library_form_views_form_media_library_page_alter(array &$form, Fo $view = $form['output'][0]['#view']; foreach (Element::getVisibleChildren($form['media_bulk_form']) as $key) { if (isset($view->result[$key])) { - $media = $view->field['media_bulk_form']->getEntity($view->result[$key]); + if (!$media = $view->field['media_bulk_form']->getEntity($view->result[$key])) { + $form['media_bulk_form'][$key]['#title'] = ''; + continue; + } $form['media_bulk_form'][$key]['#title'] = t('Select @label', [ '@label' => $media->label(), ]); diff --git a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php index 562c6a2275..76e5a7f246 100644 --- a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php +++ b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php @@ -60,7 +60,10 @@ public function viewsForm(array &$form, FormStateInterface $form_state) { // Render checkboxes for all rows. $form[$this->options['id']]['#tree'] = TRUE; foreach ($this->view->result as $row_index => $row) { - $entity = $this->getEntity($row); + if (!$entity = $this->getEntity($row)) { + $form[$this->options['id']][$row_index] = []; + continue; + } $form[$this->options['id']][$row_index] = [ '#type' => 'checkbox', '#title' => $this->t('Select @label', [ diff --git a/core/modules/node/src/Plugin/views/field/RevisionLink.php b/core/modules/node/src/Plugin/views/field/RevisionLink.php index 949ad6e07c..ce6b235ba7 100644 --- a/core/modules/node/src/Plugin/views/field/RevisionLink.php +++ b/core/modules/node/src/Plugin/views/field/RevisionLink.php @@ -21,6 +21,9 @@ class RevisionLink extends LinkBase { protected function getUrlInfo(ResultRow $row) { /** @var \Drupal\node\NodeInterface $node */ $node = $this->getEntity($row); + if (!$node) { + return NULL; + } // Current revision uses the node view path. return !$node->isDefaultRevision() ? Url::fromRoute('entity.node.revision', ['node' => $node->id(), 'node_revision' => $node->getRevisionId()]) : @@ -33,7 +36,7 @@ protected function getUrlInfo(ResultRow $row) { protected function renderLink(ResultRow $row) { /** @var \Drupal\node\NodeInterface $node */ $node = $this->getEntity($row); - if (!$node->getRevisionid()) { + if (!$node || !$node->getRevisionid()) { return ''; } $text = parent::renderLink($row); diff --git a/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php b/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php index 2e1f683b52..8f92a2ee1b 100644 --- a/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php +++ b/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php @@ -20,6 +20,9 @@ class RevisionLinkDelete extends RevisionLink { protected function getUrlInfo(ResultRow $row) { /** @var \Drupal\node\NodeInterface $node */ $node = $this->getEntity($row); + if (!$node) { + return NULL; + } return Url::fromRoute('node.revision_delete_confirm', ['node' => $node->id(), 'node_revision' => $node->getRevisionId()]); } diff --git a/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php b/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php index 33b20b8885..7c6c10f8b3 100644 --- a/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php +++ b/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php @@ -20,6 +20,9 @@ class RevisionLinkRevert extends RevisionLink { protected function getUrlInfo(ResultRow $row) { /** @var \Drupal\node\NodeInterface $node */ $node = $this->getEntity($row); + if (!$node) { + return NULL; + } return Url::fromRoute('node.revision_revert_confirm', ['node' => $node->id(), 'node_revision' => $node->getRevisionId()]); } diff --git a/core/modules/user/src/Plugin/views/field/Permissions.php b/core/modules/user/src/Plugin/views/field/Permissions.php index ac8c3184c4..f2342f1c32 100644 --- a/core/modules/user/src/Plugin/views/field/Permissions.php +++ b/core/modules/user/src/Plugin/views/field/Permissions.php @@ -81,7 +81,11 @@ public function preRender(&$values) { $rids = []; foreach ($values as $result) { - $user_rids = $this->getEntity($result)->getRoles(); + $user = $this->getEntity($result); + if (!$user) { + continue; + } + $user_rids = $user->getRoles(); $uid = $this->getValue($result); foreach ($user_rids as $rid) { diff --git a/core/modules/views/src/Plugin/views/field/BulkForm.php b/core/modules/views/src/Plugin/views/field/BulkForm.php index 33b1afb015..5901a39122 100644 --- a/core/modules/views/src/Plugin/views/field/BulkForm.php +++ b/core/modules/views/src/Plugin/views/field/BulkForm.php @@ -308,7 +308,11 @@ public function viewsForm(&$form, FormStateInterface $form_state) { // Render checkboxes for all rows. $form[$this->options['id']]['#tree'] = TRUE; foreach ($this->view->result as $row_index => $row) { - $entity = $this->getEntityTranslation($this->getEntity($row), $row); + if (!$entity = $this->getEntity($row)) { + $form[$this->options['id']][$row_index] = []; + continue; + } + $entity = $this->getEntityTranslation($entity, $row); $form[$this->options['id']][$row_index] = [ '#type' => 'checkbox', diff --git a/core/modules/views/src/Plugin/views/field/EntityLink.php b/core/modules/views/src/Plugin/views/field/EntityLink.php index 75b8da3949..69b3d6f0b6 100644 --- a/core/modules/views/src/Plugin/views/field/EntityLink.php +++ b/core/modules/views/src/Plugin/views/field/EntityLink.php @@ -26,6 +26,9 @@ public function render(ResultRow $row) { */ protected function renderLink(ResultRow $row) { if ($this->options['output_url_as_text']) { + if (!$urlInfo = $this->getUrlInfo($row)) { + return ''; + } return $this->getUrlInfo($row)->toString(); } return parent::renderLink($row); @@ -36,7 +39,11 @@ protected function renderLink(ResultRow $row) { */ protected function getUrlInfo(ResultRow $row) { $template = $this->getEntityLinkTemplate(); - return $this->getEntity($row)->toUrl($template)->setAbsolute($this->options['absolute']); + $entity = $this->getEntity($row); + if (!$entity) { + return NULL; + } + return $entity->toUrl($template)->setAbsolute($this->options['absolute']); } /** diff --git a/core/modules/views/src/Plugin/views/field/EntityOperations.php b/core/modules/views/src/Plugin/views/field/EntityOperations.php index 32a30ad1ae..5d5c9abc58 100644 --- a/core/modules/views/src/Plugin/views/field/EntityOperations.php +++ b/core/modules/views/src/Plugin/views/field/EntityOperations.php @@ -139,7 +139,10 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { * {@inheritdoc} */ public function render(ResultRow $values) { - $entity = $this->getEntityTranslation($this->getEntity($values), $values); + if (!$entity = $this->getEntity($values)) { + return []; + } + $entity = $this->getEntityTranslation($entity, $values); $operations = $this->entityTypeManager->getListBuilder($entity->getEntityTypeId())->getOperations($entity); if ($this->options['destination']) { foreach ($operations as &$operation) { diff --git a/core/modules/views/src/Plugin/views/field/LinkBase.php b/core/modules/views/src/Plugin/views/field/LinkBase.php index 31dca4c7c3..ddbe654c8d 100644 --- a/core/modules/views/src/Plugin/views/field/LinkBase.php +++ b/core/modules/views/src/Plugin/views/field/LinkBase.php @@ -123,6 +123,9 @@ public function query() { */ public function render(ResultRow $row) { $access = $this->checkUrlAccess($row); + if ($access === NULL) { + return ''; + } $build = ['#markup' => $access->isAllowed() ? $this->renderLink($row) : '']; BubbleableMetadata::createFromObject($access)->applyTo($build); return $build; @@ -134,11 +137,14 @@ public function render(ResultRow $row) { * @param \Drupal\views\ResultRow $row * A view result row. * - * @return \Drupal\Core\Access\AccessResultInterface + * @return \Drupal\Core\Access\AccessResultInterface|null * The access result. */ protected function checkUrlAccess(ResultRow $row) { $url = $this->getUrlInfo($row); + if (!$url) { + return NULL; + } return $this->accessManager->checkNamedRoute($url->getRouteName(), $url->getRouteParameters(), $this->currentUser(), TRUE); } @@ -148,7 +154,7 @@ protected function checkUrlAccess(ResultRow $row) { * @param \Drupal\views\ResultRow $row * A view result row. * - * @return \Drupal\Core\Url + * @return \Drupal\Core\Url|null * The URI elements of the link. */ abstract protected function getUrlInfo(ResultRow $row); @@ -164,7 +170,9 @@ protected function checkUrlAccess(ResultRow $row) { */ protected function renderLink(ResultRow $row) { $this->options['alter']['make_link'] = TRUE; - $this->options['alter']['url'] = $this->getUrlInfo($row); + if ($urlInfo = $this->getUrlInfo($row)) { + $this->options['alter']['url'] = $this->getUrlInfo($row); + } $text = !empty($this->options['text']) ? $this->sanitizeValue($this->options['text']) : $this->getDefaultLabel(); $this->addLangcode($row); return $text; diff --git a/core/modules/views/src/Plugin/views/field/RenderedEntity.php b/core/modules/views/src/Plugin/views/field/RenderedEntity.php index f70b03f5d9..4539866605 100644 --- a/core/modules/views/src/Plugin/views/field/RenderedEntity.php +++ b/core/modules/views/src/Plugin/views/field/RenderedEntity.php @@ -146,15 +146,16 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { * {@inheritdoc} */ public function render(ResultRow $values) { - $entity = $this->getEntityTranslation($this->getEntity($values), $values); + if (!$entity = $this->getEntity($values)) { + return []; + } + $entity = $this->getEntityTranslation($entity, $values); $build = []; - if (isset($entity)) { - $access = $entity->access('view', NULL, TRUE); - $build['#access'] = $access; - if ($access->isAllowed()) { - $view_builder = $this->entityTypeManager->getViewBuilder($this->getEntityTypeId()); - $build += $view_builder->view($entity, $this->options['view_mode']); - } + $access = $entity->access('view', NULL, TRUE); + $build['#access'] = $access; + if ($access->isAllowed()) { + $view_builder = $this->entityTypeManager->getViewBuilder($this->getEntityTypeId()); + $build += $view_builder->view($entity, $this->options['view_mode']); } return $build; }