diff --git a/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php index 6bae2ca..0228c25 100644 --- a/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php +++ b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php @@ -244,10 +244,20 @@ class ContentEntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin break; default: - if (is_subclass_of($entity, '\Drupal\Core\Entity\FieldableEntityInterface', TRUE) && $entity->hasField($field)) { - $property = $entity->getFieldDefinition($field)->getFieldStorageDefinition()->getMainPropertyName(); - $value = $entity->$field->view(['label' => 'hidden']); - $row[] = $value ? \Drupal::service('renderer')->renderPlain($value) : $entity->$field->$property; + // Make sure the field exists on this entity. + if ($entity instanceof FieldableEntityInterface && $entity->hasField($field)) { + try { + // Get the main property as a fallback if the field can not be + // viewed. + $property = $entity->getFieldDefinition($field)->getFieldStorageDefinition()->getMainPropertyName(); + $value = $entity->$field->view(['label' => 'hidden']); + // Try to render the field, fall back the main property. + $row[] = $value ? \Drupal::service('renderer')->renderPlain($value) : $entity->$field->$property; + } catch (\Exception $e) { + // Catch any exception and display as an error. + drupal_set_message(t('Error while trying to display %field: @error', ['%field' => $field, '@error' => $e->getMessage()]), 'error'); + $row[] = ''; + } } else { $row[] = ''; @@ -302,7 +312,7 @@ class ContentEntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin $options = []; foreach ($entity_types as $id => $label) { $class = $this->entityManager->getDefinition($id)->getClass(); - if (is_subclass_of($class, '\Drupal\Core\Entity\ContentEntityInterface', TRUE)) { + if (is_subclass_of($class, '\Drupal\Core\Entity\FieldableEntityInterface')) { $options[$id] = $label; }; }