You are here

public function SchedulerManager::viewsUpdate in Scheduler 2.x

Refreshes scheduler views from source.

If the view exists in the site's active storage it will be updated from the source yml file. If the view is now required but does not exist in active storage it will be loaded.

Called from scheduler_modules_installed() and scheduler_update_8202().

Parameters

array $only_these_types: List of entity types to restrict the update of views to these types only. Optional. If none then revert/load all applicable scheduler views.

Return value

array Labels of the views that were updated.

File

src/SchedulerManager.php, line 1116

Class

SchedulerManager
Defines a scheduler manager.

Namespace

Drupal\scheduler

Code

public function viewsUpdate(array $only_these_types = []) {
  $updated = [];
  $definition = $this->entityTypeManager
    ->getDefinition('view');
  $view_storage = $this->entityTypeManager
    ->getStorage('view');

  // Get the supported entity type ids for enabled modules where the provider
  // is Scheduler. Third-party plugins do not need to be processed here.
  $entity_types = $this
    ->getPluginEntityTypes('scheduler');
  if ($only_these_types) {
    $entity_types = array_intersect($entity_types, $only_these_types);
  }
  foreach ($entity_types as $entity_type) {
    $name = 'scheduler_scheduled_' . ($entity_type == 'node' ? 'content' : $entity_type);
    $full_name = $definition
      ->getConfigPrefix() . '.' . $name;

    // Read the view definition from the .yml file. First try the /optional
    // folder, then the main /config folder.
    $optional_folder = drupal_get_path('module', 'scheduler') . '/config/optional';
    $source_storage = new FileStorage($optional_folder);
    if (!($source = $source_storage
      ->read($full_name))) {
      $install_folder = drupal_get_path('module', 'scheduler') . '/config/install';
      $source_storage = new FileStorage($install_folder);
      if (!($source = $source_storage
        ->read($full_name))) {
        throw new \Exception(sprintf('Failed to read source file for %s from either %s or %s folders', $full_name, $install_folder, $optional_folder));
      }
    }

    // Try to read the view definition from active config storage.

    /** @var \Drupal\Core\Config\StorageInterface $config_storage */
    $config_storage = \Drupal::service('config.storage');
    if ($config_storage
      ->read($full_name)) {

      // The view does exist in active storage, so load it, then replace the
      // value with the source, but retain the _core and uuid values.
      $view = $view_storage
        ->load($name);
      $core = $view
        ->get('_core');
      $uuid = $view
        ->get('uuid');
      $view = $view_storage
        ->updateFromStorageRecord($view, $source);
      $view
        ->set('_core', $core);
      $view
        ->set('uuid', $uuid);
      $view
        ->save();
      $this->logger
        ->notice('%view view updated.', [
        '%view' => $source['label'],
      ]);
    }
    else {

      // The view does not exist in active storage so import it from source.
      $view = $view_storage
        ->createFromStorageRecord($source);
      $view
        ->save();
      $this->logger
        ->notice('%view view loaded from source.', [
        '%view' => $source['label'],
      ]);
    }
    $updated[] = $source['label'];
  }

  // The views are loaded OK but the publish-on and unpublish-on views field
  // handlers are not found. Clearing the views data cache solves the problem.
  Cache::invalidateTags([
    'views_data',
  ]);
  return $updated;
}