Problem/Motivation

The EntityAggregator currently offers various entities that actually aren't supported.

Examples: After creating a sensor for Block entities, the message shows:

Drupal\Core\Entity\Query\QueryException: Aggregation over configuration entities is not supported

And when you try to edit it: LogicException: Getting the base fields is not supported for entity type Block. in Drupal\Core\Entity\EntityManager->buildBaseFieldDefinitions() (line 412 of core/lib/Drupal/Core/Entity/EntityManager.php).

Fixing the second error can be fixed by removing this code from the entityAggregator:

    $entity_type = $this->entityManager->getDefinition($this->getEntityTypeId());
    $form['verbose_fields']['available_fields'] = [
      '#markup' => t('Available Fields for entity %type: <b>%fields</b>', [
          '%type' => $entity_type->getLabel(),
          '%fields' => implode(', ', array_keys($this->entityManager->getBaseFieldDefinitions($this->getEntityTypeId())))
        ]
      ),
    ];

The getBaseFieldDefinitions method doesn't work with ConfigEntities.

Proposed resolution

Rename sensor to ContentEntityAggregatorSensorPlugin, remove the non-content entities from the dropdown selector.

Remaining tasks

Review issue, discuss, fix, provide patch, review patch and commit it.

User interface changes

None

API changes

None

Data model changes

None

Comments

berdir’s picture

Actually, I think we should do exactly that. Hide non-content entities, possibly rename the sensor ContentEntityAggregatorSensor.

LKS90’s picture

Issue summary: View changes
LKS90’s picture

Status: Active » Needs review
StatusFileSize
new15.38 KB

Here is the patch, I renamed the EntityAggregator to ContentEntityAggregator, so a folowup with a ConfigEntityAggregator should be easy. Maybe add a base EntityAggregator class from which both extend or something like that.

Validation is still missing:

  1. When switching between entities or adding a nonexisting field, there is fatal error. Fields have to be validated before saving.
  2. Creating a sensor with an already existing (machine) name leads to an ugly exception.
berdir’s picture

1. Validating this will be very hard. There are per-bundle fields and so on. Instead, make it not fatal if a field doesn't exist. as mentioned before, use hasField() on the entity before accessing it.

2. That's a separate bug, the exists callback is supposed to prevent that.

LKS90’s picture

StatusFileSize
new16.95 KB
new2.92 KB

Here is a version without the fatal error. The second bug is new, I distinctly remember it working.

LKS90’s picture

Title: Make EntityAggregatorSensor compatible with config entities » Modify EntityAggregatorSensor to only aggregate content entities.
berdir’s picture

Status: Needs review » Needs work
  1. +++ b/config/install/monitoring.sensor_config.user_active.yml
    @@ -2,7 +2,7 @@ id: user_active
     label: 'Active users'
     description: ''
     category: 'User activity'
    -plugin_id: entity_aggregator
    +plugin_id: content_entity_aggregator
     value_label: 'Users'
     value_type: 'number'
     status: FALSE
    

    Not sure about this, might make a bit more sense, but it will also break existing installations completely.

    I'd keep the current name for now.

  2. +++ b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php
    @@ -291,12 +297,13 @@ class EntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase im
           '#maxlength' => 255,
    -      '#options' => $this->entityManager->getEntityTypeLabels(),
    +      '#options' => isset($options['Content']) ? $options['Content'] : [],
           '#title' => t('Entity Type'),
    

    That problem again ;)

    Doesn't quite work like that, Content is a translatable string, so you would have to at least put t() around it. And I prefer to loop over entity types and check the entity class against the interface mentioned above. Contrib might add additional groups.

LKS90’s picture

StatusFileSize
new7.02 KB

Just a quick upload to maybe get some feedback about getting the list of compatible entities.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new7.49 KB

Fixed the problem I was having, is_subclass_of can use only string names for classes.

Status: Needs review » Needs work

The last submitted patch, 9: modify-2534498-9.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new8.12 KB
new0 bytes

Fixed test-fails! My entity_type dropdown didn't have keys, fixed that by setting them to the entity_id. Also removed a @todo about adding db aggregator tests.

miro_dietiker’s picture

Status: Needs review » Needs work

Nice interdiff (0 bytes) ;-P

  • +++ b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php
    @@ -242,9 +244,14 @@ class EntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase im
    +              $row[] = t('invalid field');
    

    Aha, and which one? Placeholder?

  • +++ b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php
    @@ -255,7 +262,7 @@ class EntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase im
    +      $header = $this->sensorConfig->getSetting('verbose_fields', ['ID, Label']);
    

    WTF?

    'ID', 'Label'... right?
    I think you want test coverage. ;-)
    Ah and the sensors all use 'id' and 'label' (small, ...).

  • +++ b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php
    @@ -291,12 +298,20 @@ class EntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase im
    +    $entity_types = $this->entityManager->getEntityTypeLabels();
    ...
    +    foreach (array_keys($entity_types) as $delta => $type) {
    ...
    +        $options[array_keys($entity_types)[$delta]] = array_values($entity_types)[$delta];
    

    Extracting keys and values again and again... :-S

  • +++ b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php
    @@ -416,8 +431,8 @@ class EntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase im
    +        '%fields' => implode(', ', array_keys($this->entityManager->getBaseFieldDefinitions($this->getEntityTypeId())))
             ]
           ),
    

    Then you want to merge the closing bracket lines too and reduce indentation.

  • And finally, dreaming of nice test coverage...

    LKS90’s picture

    Status: Needs work » Needs review
    StatusFileSize
    new1.43 KB
    new6.82 KB
    new12.47 KB

    I'll provide the old interdiff as well with this comment :).

    Aha, and which one? Placeholder?

    The header had the field name, so I though it would be redundant to placeholder the field in there.
    Changed it to: field <em>test_wrong_field</em> is invalid.

    Fixed the default values for the header.
    Simplified the array_keys/values stuff, it was just way more complicated than necessary.
    Collapsed the the function closing bracket to the array closing bracket.

    Added test coverage for the EntityAggregator.

    Status: Needs review » Needs work

    The last submitted patch, 13: modify-2534498-13.patch, failed testing.

    LKS90’s picture

    Status: Needs work » Needs review
    StatusFileSize
    new12.47 KB
    new762 bytes

    Added a default case for the conditions so if there are no conditions, an empty array is returned and array_values doesn't cause an exception anymore.

    miro_dietiker’s picture

    The "invalid field" output should be completely dropped like the field is empty. Otherwise it is too much visual noise.

    LKS90’s picture

    StatusFileSize
    new12.37 KB
    new1.52 KB

    Removed the output when the field is invalid.

    LKS90’s picture

    StatusFileSize
    new1.41 KB
    new12.44 KB

    Here is the change from FieldableEntityInterface to ContentEntityInterface for the available entity_types. Also added the check for FieldableEntityInterface to the default case so the File entity type (just one example I currently know of) won't break things.

    The last submitted patch, 17: modify-2534498-17.patch, failed testing.

    Status: Needs review » Needs work

    The last submitted patch, 18: modify-2534498-18.patch, failed testing.

    Status: Needs work » Needs review

    Berdir queued 18: modify-2534498-18.patch for re-testing.

    The last submitted patch, 8: modify-2534498-8.patch, failed testing.

    berdir’s picture

    StatusFileSize
    new12.96 KB
    new2.36 KB

    Didn't get that last change. The file entity thing is a core bug, I've opened #2539622: File uri field type has default formatter uri_link, throws exception when trying to render.

    I've added a try/catch around it and then I'm just displaying that error. We need to do that more often to avoid completely broken pages.

    berdir’s picture

    Status: Needs review » Fixed

    And committed!

    • Berdir committed 7d3c8c9 on 8.x-1.x authored by LKS90
      Issue #2534498 by LKS90, Berdir, miro_dietiker: Modify...

    Status: Fixed » Closed (fixed)

    Automatically closed - issue fixed for 2 weeks with no activity.