You are here

function varbase_core_optional_update_8701 in Varbase Core 8.7

Same name and namespace in other branches
  1. 8.8 varbase_core.install \varbase_core_optional_update_8701()
  2. 9.0.x varbase_core.install \varbase_core_optional_update_8701()

Add [Gallery] media type.

File

./varbase_core.install, line 338
Install, update and uninstall functions for the Varbase core module.

Code

function varbase_core_optional_update_8701() {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('varbase_core', 'varbase_core_optional_update_8701');
  $module_path = Drupal::service('module_handler')
    ->getModule('varbase_media')
    ->getPath();
  $optional_install_path = $module_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;

  // Load Media types icons for the media libraray table view page.
  $source = $module_path . '/images/icons';
  $destination = \Drupal::config('media.settings')
    ->get('icon_base_uri');
  file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  $files = file_scan_directory($source, '/.*\\.(svg|png|jpg|jpeg|gif)$/');
  foreach ($files as $file) {
    if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
      file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_ERROR);
    }
  }

  // Configure Entity Embed buttons.
  $embed_button_config_files = file_scan_directory($optional_install_path, '/^embed.button.*\\.(yml)$/i');
  if (isset($embed_button_config_files) && is_array($embed_button_config_files)) {
    foreach ($embed_button_config_files as $embed_button_config_file) {
      $embed_button_config_file_content = file_get_contents(DRUPAL_ROOT . '/' . $embed_button_config_file->uri);
      $embed_button_config_file_data = (array) Yaml::parse($embed_button_config_file_content);
      $embed_button_config_factory = \Drupal::configFactory()
        ->getEditable($embed_button_config_file->name);
      $embed_button_config_factory
        ->setData($embed_button_config_file_data)
        ->save(TRUE);
    }
  }

  // Add Icons for Entity Embed CKEditor Media Library and Gallery.
  $embed_buttons = [
    // CKEditor Embed Media Library icon for the gallery embed entity button.
    'media' => [
      'source' => DRUPAL_ROOT . '/' . $module_path . '/images/ckeditor/hidpi/embed-media.png',
      'destination' => 'public://embed-media.png',
    ],
    // CKEditor Embed Gallery icon for the gallery embed entity button.
    'gallery' => [
      'source' => DRUPAL_ROOT . '/' . $module_path . '/images/ckeditor/hidpi/embed-gallery.png',
      'destination' => 'public://embed-gallery.png',
    ],
  ];
  foreach ($embed_buttons as $embed_button_id => $embed_button_info) {
    $target_destination = NULL;
    if (version_compare(Drupal::VERSION, '8.7.0', '>=')) {
      try {
        $target_destination = Drupal::service('file_system')
          ->copy($embed_button_info['source'], $embed_button_info['destination']);
      } catch (FileException $e) {
        $target_destination = FALSE;
      }
    }
    else {
      $target_destination = call_user_func('file_unmanaged_copy', $embed_button_info['source'], $embed_button_info['destination']);
    }
    if ($target_destination) {
      $target_file = File::create([
        'uri' => $target_destination,
      ]);
      $target_file
        ->save();
      EmbedButton::load($embed_button_id)
        ->set('icon_uuid', $target_file
        ->uuid())
        ->save();
    }
    else {
      return t("Unable to copy @source to the public files directory.", [
        '@source' => $embed_button_info['source'],
      ]);
    }
  }

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}