You are here

function minisite_update_8002 in Mini site 8

Re-saves all Minisite instances to populate the database with asset links.

File

./minisite.install, line 213
Contains install and update functions for Minisite.

Code

function minisite_update_8002(&$sandbox) {

  // Number of Minisite instances to process in a single batch.
  // trying to keep this as low as possible as some minisites may have
  // a lot of assets that will take time and resources to process.
  $batch_size = getenv('MINISITE_UPDATE_BATCH_SIZE') ?: 1;
  if (!isset($sandbox['info'])) {
    module_load_include('module', 'minisite');
    $info = minisite_get_info_all(TRUE);
    if (empty($info)) {
      $sandbox['#finished'] = 1;
      return t('There are no Minisite fields with content in any available entity types.');
    }
    $sandbox['info'] = $info;
    $sandbox['max'] = count($info);
    $sandbox['progress'] = 0;
    $sandbox['updated'] = 0;
  }
  $current_batch_info = array_slice($sandbox['info'], $sandbox['progress'], $batch_size);
  $stage_file_proxy_is_enabled = \Drupal::service('module_handler')
    ->moduleExists('stage_file_proxy');
  $messages = [];
  foreach ($current_batch_info as $info) {
    $sandbox['progress']++;
    list($entity_type, $field_name, $entity_id) = explode('__', $info);
    $messages[] = t('Processing Minisite for field @field_name attached to @entity_type with ID @entity_id.', [
      '@entity_type' => $entity_type,
      '@field_name' => $field_name,
      '@entity_id' => $entity_id,
    ]);
    $host_entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id);
    $field_item_list = $host_entity
      ->get($field_name);

    // Before proceeding with minisite instantiation, we need to check that
    // the archive file is available in this environment and do our best effort
    // to fetch files from origin location (works only on the environments with
    // stage_file_proxy enabled).

    /** @var \Drupal\file\Entity\File $archive_file */
    $archive_file = $field_item_list->entity;
    $archive_file_uri = $archive_file
      ->getFileUri();
    $archive_file_absolute_url = $archive_file
      ->createFileUrl(FALSE);
    if (!is_readable($archive_file_uri)) {
      if (!$stage_file_proxy_is_enabled) {
        $messages[] = '  ' . t('SKIPPED: Archive file is missing in this environment and stage_file_proxy module is not enabled.');
        continue;
      }
      _minisite_install_stage_file_proxy_fetch($archive_file_absolute_url);
      if (!is_readable($archive_file_uri)) {
        $messages[] = '  ' . t('SKIPPED: Unable to fetch archive file @uri.', [
          '@uri' => $archive_file_uri,
        ]);
        continue;
      }
      $messages[] = '  ' . t('Fetched archive file @uri.', [
        '@uri' => $archive_file_uri,
      ]);
    }
    $minisite = Minisite::createInstance($field_item_list);
    if ($minisite) {
      $minisite
        ->save();
      $sandbox['updated']++;
      $messages[] = '  ' . t('SUCCESS: Updated Ministe.');
    }
    else {
      $messages[] = '  ' . t('SKIPPED: Unable to process Ministe.');
    }
  }
  $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  return t('Processed @processed of @total and updated @updated Minisite instances: @messages', [
    '@total' => $sandbox['max'],
    '@processed' => $sandbox['progress'],
    '@updated' => $sandbox['updated'],
    '@messages' => PHP_EOL . implode(PHP_EOL, $messages),
  ]);
}