The usage count, retrieved from the entity_usage table, is taking into account the usages where the parent node does not exist anymore as well as the usages where the current entity has been unlinked from the parent node. However, on the usage page, these entries are being skipped. This is causing confusion due to the mismatch of count and the entries shown in the usage page.

Such usages are not skipped from the usage page but are marked as deleted/unlinked usages.

Comments

rishvi created an issue. See original summary.

rishvi’s picture

StatusFileSize
new1.41 KB
jibran’s picture

Status: Active » Needs work

Thank you, for reporting the issue and creating the patch. Here are some suggestions.

  1. +++ b/src/Controller/ListUsageController.php
    @@ -203,10 +203,15 @@ class ListUsageController extends ControllerBase {
    -        // If the label is empty it means this usage shouldn't be shown
    -        // on the UI, just skip this row.
    ...
    +          // If it is skipped on the UI, it could cause confusion to the user.
    +          // Instead, keep the entry on the UI without a link.
    

    Let's keep the above block, rephrase it and remove the latter one:

    If the label is empty then show the placeholder.

  2. +++ b/src/Controller/ListUsageController.php
    @@ -203,10 +203,15 @@ class ListUsageController extends ControllerBase {
    +          //  If the label is empty,  the source entity either does not exist anymore
    +          // or the current entity is not linked to the entity it claims as the parent.
    

    Extra space after slashes and double space after , also comments can't be longer then 80 chars.

  3. +++ b/src/Controller/ListUsageController.php
    @@ -203,10 +203,15 @@ class ListUsageController extends ControllerBase {
    +          // or the current entity is not linked to the entity it claims as the parent.
    

    Again the comment is > 80 chars.

  4. +++ b/src/Controller/ListUsageController.php
    @@ -203,10 +203,15 @@ class ListUsageController extends ControllerBase {
    +          $parent_id = $source_entity->get('parent_id')->value;
    

    parent_id only exist for paragraph entity types so we need to check the entity type first. if ($source_entity->getEntityTypeId() == 'paragraph') {. We also need to check sset($this->get('parent_id')->value)

  5. +++ b/src/Controller/ListUsageController.php
    @@ -203,10 +203,15 @@ class ListUsageController extends ControllerBase {
    +          $link ='Deleted  / Unlinked Node #'.$parent_id;
    

    A space missing after = sign and spaces are missing on both sides of . as well.

  6. +++ b/src/Controller/ListUsageController.php
    @@ -203,10 +203,15 @@ class ListUsageController extends ControllerBase {
    +          //  If the label is empty,  the source entity either does not exist anymore
    

    This is also not true. We have

            $source_entity = $type_storage->load($source_id);
            if (!$source_entity) {
              // If for some reason this record is broken, just skip it.
              continue;
            }

    before this.

  7. +++ b/src/Controller/ListUsageController.php
    @@ -203,10 +203,15 @@ class ListUsageController extends ControllerBase {
             $link = $this->getSourceEntityLink($source_entity);
    

    What if I change the logic in this function. There is

          if ($orphan) {
            return FALSE;
          }

    which we can update.

rishvi’s picture

StatusFileSize
new922 bytes

Thanks for your feedback Jibran. I included your feedback changes in the new patch.

rishvi’s picture

StatusFileSize
new790 bytes
jibran’s picture

Let's address the failing test.

marcoscano’s picture

Thanks for your contribution!

I'd like to better understand your use case, when you mention in the issue summary:

The usage count is taking into account (...)

which count are you referring to? A value displayed in views? A value returned from an API call, such as \Drupal\entity_usage\EntityUsage::listSources() ?

Getting this page right is tricky, and there are several scenarios where we might want to skip rows. Non-deleted paragraphs, for instance, are the main example, but there are others as well. The code for this controller unfortunately is becoming more and more hacky-ish over time, has demonstrated to be the root of many issues and is in fact probably the main reason that triggered the whole module refactor idea in #3060802: Refactor module architecture in a simpler, opinionated and more performant approach. Would the new approach make more or less sense for your particular use case?

Thanks!

rishvi’s picture

Hi marcoscano,

Thanks for your reply.

We are displaying a list of entities and their usage count in a custom view. The count is retrieved from the entity_usage table which does not match the number of entries displayed on the entity's usage page.
The proposed resolution in the new architecture looks great. These two things on there will require us to make changes to our view but will surely improve the current experience of usage page by showing only default revision. Although, we would like to have the ability to extract usage count.

  • We no longer care to store information about the fieldname, relationship method (plugin id), or count in DB. Now all a DB row tells us is that: "a (source, top-level) entity of type: A, with id: B, on its revision C and language D points to a (target, bottom-level) entity of type E with id: F". This "reference" from source and target may be direct or through any number of intermediate non-top / non-bottom entities.
  • We can now build the usage page on the UI using direct paged queries. The page will show usages grouped by source entity type (so we can easily join the entity table on the default revision) and only show records that point to the default revision

Thanks!

jibran’s picture

Issue summary: View changes

I have updated IS as per #8.

@rishvi and @marcoscano I think we can roll this patch on top of #3060802: Refactor module architecture in a simpler, opinionated and more performant approach.

jibran’s picture

Title: Usage count does not match the number of entries on the UI » Usage count does not match the number of entities

Better title.

acbramley’s picture

This patch no longer applies to the latest beta, I tracked it down to the changes in #3056026: Remove orphan handling of paragraphs when parent exists

After reading that issue, I think we can close this one as outdated or won't fix as the paragraph orphan issue has been tidied up considerably.

marcoscano’s picture

Status: Needs work » Closed (outdated)

I agree, the paragraphs workaround was probably the source of most mismatches people could find. Closing this for now, please feel free to re-open if you experience this again with the latest release.

Thanks everyone!