You are here

class ResaveBatch in Automatic Entity Label 8.3

Processes entities in chunks to re-save their labels.

@package Drupal\auto_entitylabel\Batch

Hierarchy

Expanded class hierarchy of ResaveBatch

File

src/Batch/ResaveBatch.php, line 15

Namespace

Drupal\auto_entitylabel\Batch
View source
class ResaveBatch {

  /**
   * {@inheritdoc }
   */
  public static function batchOperation(array $chunk, array $bundle, array &$context) {
    foreach ($chunk as $id) {
      $entity = '';
      switch ($bundle[0]) {
        case 'node':
          $entity = Node::load($id);
          break;
        case 'taxonomy_term':
          $entity = Term::load($id);
          break;
        case 'media':
          $entity = Media::load($id);
          break;
        case 'comment':
          $entity = Comment::load($id);
          break;
      }
      $entity
        ->save();
      $context['results'][] = $id;
    }
  }

  /**
   * {@inheritdoc }
   */
  public static function batchFinished($success, array $results, array $operations) {
    $messenger = \Drupal::messenger();
    if ($success) {
      $messenger
        ->addMessage(t('Resaved @count labels.', [
        '@count' => count($results),
      ]));
    }
    else {

      // An error occurred.
      // $operations contains the operations that remained unprocessed.
      $error_operation = reset($operations);
      $message = t('An error occurred while processing %error_operation with arguments: @arguments', [
        '%error_operation' => $error_operation[0],
        '@arguments' => print_r($error_operation[1], TRUE),
      ]);
      $messenger
        ->addError($message);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResaveBatch::batchFinished public static function {@inheritdoc }
ResaveBatch::batchOperation public static function {@inheritdoc }