function LocaleConfigBatch::buildBatch

Same name and namespace in other branches
  1. 11.x core/modules/locale/src/LocaleConfigBatch.php \Drupal\locale\LocaleConfigBatch::buildBatch()

Builds a locale batch to refresh configuration.

Parameters

array $options: An array with options that can have the following elements:

  • 'finish_feedback': (optional) Whether or not to give feedback to the user when the batch is finished. Defaults to TRUE.

array $langcodes: (optional) Array of language codes. Defaults to all translatable languages.

array $components: (optional) Array of component lists indexed by type. If not present or it is an empty array, it will update all components.

bool $update_default_config_langcodes: Determines whether default configuration langcodes should be updated. This should only happen during site and extension install.

Return value

array The batch definition.

File

core/modules/locale/src/LocaleConfigBatch.php, line 49

Class

LocaleConfigBatch
Provides the locale config update batch services.

Namespace

Drupal\locale

Code

public function buildBatch(array $options, array $langcodes = [], array $components = [], bool $update_default_config_langcodes = FALSE) : ?array {
  $langcodes = $langcodes ?: array_keys($this->languageManager
    ->getLanguages());
  if ($langcodes && $names = $this->localeConfigManager
    ->getComponentNames($components)) {
    // If the component list is empty we need to ensure that all configuration
    // in the default collection is using the site's default langcode.
    $options += [
      'finish_feedback' => TRUE,
    ];
    $batch_builder = (new BatchBuilder())->setTitle($this->t('Updating configuration translations'))
      ->setInitMessage($this->t('Starting configuration update'))
      ->setErrorMessage($this->t('Error updating configuration translations'));
    if ($update_default_config_langcodes && $this->languageManager
      ->getDefaultLanguage()
      ->getId() !== 'en') {
      $batch_builder->addOperation(self::class . ':batchUpdateDefaultConfigLangcodes');
    }
    // Chunking the array of names into batches of 20 for better performance.
    $name_chunks = array_chunk($names, 20);
    foreach ($name_chunks as $chunk) {
      // During installation the caching of configuration objects is disabled
      // so it is very expensive to initialize the \Drupal::config() object
      // on each request. We batch a small number of configuration object
      // upgrades together to improve the overall performance of the process.
      $batch_builder->addOperation(self::class . ':batchUpdateConfigTranslations', [
        $chunk,
        $langcodes,
      ]);
    }
    if (!empty($options['finish_feedback'])) {
      $batch_builder->setFinishCallback(self::class . ':batchFinished');
    }
    return $batch_builder->toArray();
  }
  return NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.