Add a "Centarro Toolbox" menu item beneath "Configuration" in the Commerce menu with a single child item called "Orphaned entities". We have multiple entity types (product variations, order items, etc.) that are meant to be tightly coupled to a parent but through site error can become orphaned.

An "orphaned" entity is one that identifies a parent entity (i.e. $product_variation->product_id) that does not reference the child entity in return. The "Orphaned entities" page should be a form with a fieldset for each entity type, "Product variations", "Order items", etc. The description of the fieldset should be a count of how many such orphaned entities exist in the site:

"Your site has 0 orphaned product variations."
"Your site has 12 orphaned order items."

Each fieldset should then include a submit button that reads,

"Delete orphaned product variations"
"Delete orphaned order items"

It should be disabled if there are 0 orphaned entities. When submitted, those entities should be deleted and a status message shown:

"12 orphaned order items deleted."

That same message should be logged to the watchdog.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rszrama created an issue. See original summary.

khiminrm’s picture

khiminrm’s picture

Status: Active » Needs review
khiminrm’s picture

Added support of commerce_shipment entities. Working on adding conditions to check if modules providing entity types are enabled - maybe will add it in CommerceOrphanedEntitiesService and will return array of available entity types for the form.

jsacksick’s picture

Status: Needs review » Needs work

Let's skip the additional service for now and add a helper method in the form class itself.

Additionally, there's probably a way to build the query automatically with variables that works for all our entity types.

See SqlContentEntityStorage::buildQuery().

For example, you can get the base table from the storage by doing:

  $entity_storage = $this->entityTypeManager()->getStorage($entity_type_id)->getBaseTable().

Also, as a side note, it'd be interesting to see if we can load entities by group of 50 at once (for better performance), instead of loading them one by one.