You are here

public function EntityAliasTypeBase::batchUpdate in Pathauto 8

Gets called to batch update all entries.

Parameters

string $action: One of:

  • 'create' to generate a URL alias for paths having none.
  • 'update' to recreate the URL alias for paths already having one, useful if the pattern changed.
  • 'all' to do both actions above at the same time.

array $context: Batch context.

Overrides AliasTypeBatchUpdateInterface::batchUpdate

File

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

Class

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

Namespace

Drupal\pathauto\Plugin\pathauto\AliasType

Code

public function batchUpdate($action, &$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
    ->leftJoin($this
    ->getTableInfo()['table'], 'pa', "CONCAT('" . $this
    ->getSourcePrefix() . "' , base_table.{$id_key}) = pa.{$this->getTableInfo()['fields']['path']}");
  $query
    ->addField('base_table', $id_key, 'id');
  switch ($action) {
    case 'create':
      $query
        ->isNull("pa.{$this->getTableInfo()['fields']['path']}");
      break;
    case 'update':
      $query
        ->isNotNull("pa.{$this->getTableInfo()['fields']['path']}");
      break;
    case 'all':

      // Nothing to do. We want all paths.
      break;
    default:

      // Unknown action. Abort!
      return;
  }
  $query
    ->condition('base_table.' . $id_key, $context['sandbox']['current'], '>');
  $query
    ->orderBy('base_table.' . $id_key);
  $query
    ->addTag('pathauto_bulk_update');
  $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 update, then stop immediately.
    if (!$context['sandbox']['total']) {
      $context['finished'] = 1;
      return;
    }
  }
  $query
    ->range(0, 25);
  $ids = $query
    ->execute()
    ->fetchCol();
  $updates = $this
    ->bulkUpdate($ids);
  $context['sandbox']['count'] += count($ids);
  $context['sandbox']['current'] = !empty($ids) ? max($ids) : 0;
  $context['results']['updates'] += $updates;
  $context['message'] = $this
    ->t('Updated alias for %label @id.', [
    '%label' => $entity_type
      ->getLabel(),
    '@id' => end($ids),
  ]);
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  }
}