You are here

public function EntityAliasTypeBase::batchDelete in Pathauto 8

Gets called to batch delete all aliases created by pathauto.

Parameters

array $context: Batch context.

Overrides AliasTypeBatchUpdateInterface::batchDelete

File

src/Plugin/pathauto/AliasType/EntityAliasTypeBase.php, line 203

Class

EntityAliasTypeBase
A pathauto alias type plugin for entities with canonical links.

Namespace

Drupal\pathauto\Plugin\pathauto\AliasType

Code

public function batchDelete(&$context) {
  if (!isset($context['sandbox']['current'])) {
    $context['sandbox']['count'] = 0;
    $context['sandbox']['current'] = 0;
  }
  $entity_type = $this->entityTypeManager
    ->getDefinition($this
    ->getEntityTypeId());
  $id_key = $entity_type
    ->getKey('id');
  $query = $this->database
    ->select($entity_type
    ->get('base_table'), 'base_table');
  $query
    ->innerJoin($this
    ->getTableInfo()['table'], 'pa', "CONCAT('" . $this
    ->getSourcePrefix() . "' , base_table.{$id_key}) = pa.{$this->getTableInfo()['fields']['path']}");
  $query
    ->addField('base_table', $id_key, 'id');
  $query
    ->addField('pa', $this
    ->getTableInfo()['fields']['id']);
  $query
    ->condition("pa.{$this->getTableInfo()['fields']['id']}}", $context['sandbox']['current'], '>');
  $query
    ->orderBy("pa.{$this->getTableInfo()['fields']['id']}}");
  $query
    ->addTag('pathauto_bulk_delete');
  $query
    ->addMetaData('entity', $this
    ->getEntityTypeId());

  // Get the total amount of items to process.
  if (!isset($context['sandbox']['total'])) {
    $context['sandbox']['total'] = $query
      ->countQuery()
      ->execute()
      ->fetchField();

    // If there are no entities to delete, then stop immediately.
    if (!$context['sandbox']['total']) {
      $context['finished'] = 1;
      return;
    }
  }
  $query
    ->range(0, 100);
  $pids_by_id = $query
    ->execute()
    ->fetchAllKeyed();
  PathautoState::bulkDelete($this
    ->getEntityTypeId(), $pids_by_id);
  $context['sandbox']['count'] += count($pids_by_id);
  $context['sandbox']['current'] = !empty($pids_by_id) ? max($pids_by_id) : 0;
  $context['results']['deletions'][] = $this
    ->getLabel();
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  }
}