From 29e09813530cb4ac15c25e09038cd3d3c8507156 Mon Sep 17 00:00:00 2001
From: Jakob Perry <japerry007@gmail.com>
Date: Sat, 6 Apr 2024 19:43:26 -0700
Subject: [PATCH] Check if a field exists before setting it in the build array.

---
 modules/ctools_block/src/Plugin/Block/EntityField.php | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/modules/ctools_block/src/Plugin/Block/EntityField.php b/modules/ctools_block/src/Plugin/Block/EntityField.php
index 23e8da42..3d768219 100644
--- a/modules/ctools_block/src/Plugin/Block/EntityField.php
+++ b/modules/ctools_block/src/Plugin/Block/EntityField.php
@@ -137,10 +137,13 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont
     /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
     $entity = $this->getContextValue('entity');
     $build = [];
-    /** @var \Drupal\Core\Field\FieldItemListInterface $field */
-    $field = $entity->{$this->fieldName};
-    $display_settings = $this->getConfiguration()['formatter'];
-    $build['field'] = $field->view($display_settings);
+
+    if ($entity->hasField($this->fieldName)) {
+      /** @var \Drupal\Core\Field\FieldItemListInterface $field */
+      $field = $entity->{$this->fieldName};
+      $display_settings = $this->getConfiguration()['formatter'];
+      $build['field'] = $field->view($display_settings);
+    }
 
     // Set the cache data appropriately.
     $build['#cache']['contexts'] = $this->getCacheContexts();
-- 
GitLab