You are here

function content_browser_install in Content Browser 8

Implements hook_install().

File

./content_browser.install, line 16
Defines install routines for Content Browser.

Code

function content_browser_install() {

  // Add an icon for Content Browser if we aren't installing from configuration.
  if (!\Drupal::isConfigSyncing()) {
    $data = file_get_contents(dirname(__FILE__) . '/content_browser_icon.png');

    /** @var \Drupal\file\FileInterface $file */
    $file = file_save_data($data, 'public://content_browser_icon.png', FileSystemInterface::EXISTS_REPLACE);
    if ($file) {

      // Set file uuid same as default config.
      $uuid = Yaml::decode(file_get_contents(dirname(__FILE__) . '/config/install/embed.button.content_browser.yml'))['icon_uuid'];
      $file
        ->set('uuid', $uuid);
      $file
        ->save();
      \Drupal::service('file.usage')
        ->add($file, 'embed', 'embed_button', 'content_browser');
    }
  }

  // Copy the teaser view mode for every Content Type so that our View makes
  // some level of sense initially.
  $bundles = array_keys(node_type_get_names());
  $teaser_ids = [];
  $existing_ids = [];
  foreach ($bundles as $bundle) {
    $teaser_ids[] = 'node.' . $bundle . '.teaser';
    $existing_ids[] = 'node.' . $bundle . '.content_browser';
  }

  // Get a list of existing Node teaser displays.
  $results = \Drupal::entityQuery('entity_view_display')
    ->condition('id', $teaser_ids)
    ->condition('status', TRUE)
    ->execute();

  // Also get a list of existing Node content browser displays, to prevent
  // installation errors if a bundle already has a display defined.
  $existing_results = \Drupal::entityQuery('entity_view_display')
    ->condition('id', $existing_ids)
    ->execute();

  // Load the selected teaser displays.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display');

  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay[] $displays */
  $displays = $storage
    ->loadMultiple($results);

  // Clone each display and save it.
  foreach ($displays as $display) {
    $copy = $display
      ->createCopy('content_browser');

    // Check if the display already exists.
    if (!in_array($copy
      ->id(), $existing_results)) {
      $copy
        ->save();
    }
  }
}