You are here

function lightning_media_update_8006 in Lightning Media 8.4

Same name and namespace in other branches
  1. 8 lightning_media.install \lightning_media_update_8006()
  2. 8.2 lightning_media.install \lightning_media_update_8006()
  3. 8.3 lightning_media.install \lightning_media_update_8006()

Sets the icon for the media browser's embed button.

File

./lightning_media.install, line 95
Contains install and update routines for Lightning Media.

Code

function lightning_media_update_8006() {
  $source = __DIR__ . '/images/star.png';
  $destination = 'public://star.png';

  // In Drupal 8.7.0 and later, we need to use FileSystemInterface::copy() to
  // avoid deprecated code. But in previous versions of Drupal, we need to use
  // file_unmanaged_copy(), which we need to invoke in an obscure way so that
  // PHPStan doesn't get mad at us for calling a deprecated function.
  if (version_compare(Drupal::VERSION, '8.7.0', '>=')) {
    try {
      $destination = Drupal::service('file_system')
        ->copy($source, $destination);
    } catch (FileException $e) {
      $destination = FALSE;
    }
  }
  else {
    $destination = call_user_func('file_unmanaged_copy', $source, $destination);
  }
  if ($destination) {
    $file = File::create([
      'uri' => $destination,
    ]);
    $file
      ->save();
    EmbedButton::load('media_browser')
      ->set('icon_uuid', $file
      ->uuid())
      ->save();
  }
  else {
    return t("Unable to copy @source to the public files directory.", [
      '@source' => $source,
    ]);
  }
}