getEntityTypes doesn't need to check entities AND processItems has wrong starting point

Created on 29 December 2023, 6 months ago
Updated 21 January 2024, 5 months ago

Problem/Motivation

1. getEntityTypes opens every entity on the site to verify that it has translations. This is not necessary and on large sites crashes due to memory/time limits.
2. processItems has the wrong starting value and as such only runs for 50% of entities when larger than batch size.

Steps to reproduce

1A. Have a large site DB with lots of content of any type
1B. Attempt loading the /admin/config/regional/delete-entity-translations and it will hang/crash

2A. Attempt running a delete process with more than ~100 entities and only 50% will delete.

Proposed resolution

1. Remove the code loading entities on page load to verify translations. Verification isn't necessary -- the batch process will determine whether or not there are actually any entities or not and still works great without them.

  /**
   * Get list of entity types with selected language | translation.
   *
   * @param string $langcode
   *   The langcode of selected language.
   *
   * @return array
   *   Array of entity types with selected language | translation.
   */
  public function getEntityTypes(string $langcode): array {
    $entity_types = [];
    $definitions = $this->entityTypeManager->getDefinitions();

    foreach ($definitions as $id => $definition) {
      $entity_types[$id] = $definition->getLabel();
    }

    return $entity_types;
  }

2. The real fix is in the $ids assignment where the range starts at 0 rather than at the progress variable but this code assigns a batch size constant to be used as well. I also removed the unneeded `processed` variable.

define("PROCESS_COMMIT_BATCH_SIZE", 50);

  /**
   * Batch Callback to generate csv.
   *
   * @param string $langcode
   *   The langcode of selected language.
   * @param string $entity_type
   *   The entity type need to process.
   * @param array $context
   *   The batch context.
   */
  public function processItems(string $langcode, string $entity_type, array &$context): void {
    $query = $this->entityTypeManager->getStorage($entity_type)->getQuery();

    if (empty($context['sandbox'])) {
      $count = $query
        ->accessCheck(FALSE)
        ->condition('langcode', $langcode)
        ->count()
        ->execute();

      $context['sandbox']['progress'] = 0;
      $context['sandbox']['max'] = $count;
    }

    $ids = $query
      ->accessCheck(FALSE)
      ->condition('langcode', $langcode)
      ->range(0, PROCESS_COMMIT_BATCH_SIZE)
      ->execute();


    ...

Remaining tasks

Add to code base.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇺🇸United States lakesta

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024