Skip to content
Snippets Groups Projects

Check if a field exists before setting it in the build array.

Merged Jakob P requested to merge issue/ctools-2966909:2966909-ctools-block-field into 8.x-3.x
1 file
+ 7
4
Compare changes
  • Side-by-side
  • Inline
@@ -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();
Loading