You are here

function photos_update_8502 in Album Photos 6.0.x

Create default view modes and views if needed.

File

./photos.install, line 263
Install, update, and uninstall functions for the Photos module.

Code

function photos_update_8502(&$sandbox) {
  $message = NULL;
  $all_created = FALSE;

  // Create view modes.
  if (!EntityViewMode::load('photos_image.cover')) {

    // Album cover.
    EntityViewMode::create([
      'id' => 'photos_image.cover',
      'targetEntityType' => 'photos_image',
      'label' => 'Album cover',
    ])
      ->setStatus(TRUE)
      ->save();
  }
  if (!EntityViewMode::load('photos_image.full')) {

    // Full.
    EntityViewMode::create([
      'id' => 'photos_image.full',
      'targetEntityType' => 'photos_image',
      'label' => 'Full content',
    ])
      ->save();
  }
  if (!EntityViewMode::load('photos_image.list')) {

    // List.
    EntityViewMode::create([
      'id' => 'photos_image.list',
      'targetEntityType' => 'photos_image',
      'label' => 'List',
    ])
      ->setStatus(TRUE)
      ->save();
  }
  if (!EntityViewMode::load('photos_image.pager')) {

    // Pager.
    EntityViewMode::create([
      'id' => 'photos_image.pager',
      'targetEntityType' => 'photos_image',
      'label' => 'Pager',
    ])
      ->save();
  }
  if (!EntityViewMode::load('photos_image.search_result')) {

    // Search result highlighting input.
    EntityViewMode::create([
      'id' => 'photos_image.search_result',
      'targetEntityType' => 'photos_image',
      'label' => 'Search result highlighting input',
    ])
      ->save();
  }
  if (!EntityViewMode::load('photos_image.search_result_image')) {

    // Search Result Image.
    EntityViewMode::create([
      'id' => 'photos_image.search_result_image',
      'targetEntityType' => 'photos_image',
      'label' => 'Search Result Image',
    ])
      ->save();
  }
  if (!EntityViewMode::load('photos_image.sort')) {

    // Sort.
    EntityViewMode::create([
      'id' => 'photos_image.sort',
      'targetEntityType' => 'photos_image',
      'label' => 'Sort',
    ])
      ->save();
  }

  // Create new views.
  if (\Drupal::moduleHandler()
    ->moduleExists('views')) {
    $all_created = TRUE;

    // Only create if the photos view doesn't exist and views is enabled.
    if (!View::load('photos')) {
      $config_path = drupal_get_path('module', 'photos') . '/config/optional/views.view.photos.yml';
      $data = Yaml::parseFile($config_path);
      \Drupal::configFactory()
        ->getEditable('views.view.photos')
        ->setData($data)
        ->save(TRUE);
    }
    else {
      $all_created = FALSE;
    }

    // Album view.
    if (!View::load('photos_album')) {
      $config_path = drupal_get_path('module', 'photos') . '/config/optional/views.view.photos_album.yml';
      $data = Yaml::parseFile($config_path);
      \Drupal::configFactory()
        ->getEditable('views.view.photos_album')
        ->setData($data)
        ->save(TRUE);
    }
    else {
      $all_created = FALSE;
    }

    // Album list view.
    if (!View::load('photos_album_list')) {
      $config_path = drupal_get_path('module', 'photos') . '/config/optional/views.view.photos_album_list.yml';
      $data = Yaml::parseFile($config_path);
      \Drupal::configFactory()
        ->getEditable('views.view.photos_album_list')
        ->setData($data)
        ->save(TRUE);
    }
    else {
      $all_created = FALSE;
    }
  }
  if ($all_created) {
    $message = 'New views and view modes have been created.';
  }
  else {
    $message = 'Some of the views already exist or could not be created and may
      need to be created manually (if needed).';
  }
  return $message;
}