You are here

function varbase_media_update_8702 in Varbase Media 9.0.x

Same name and namespace in other branches
  1. 8.7 varbase_media.install \varbase_media_update_8702()

Issue #3103229: Fix icons for [Embed buttons] for the CKEditor.

After the change of Embed API as they had droped icon_uuid and change of config schema.

1 call to varbase_media_update_8702()
varbase_media_update_8703 in ./varbase_media.install
Follow up fix Issue #3103229: Fix icons for [Embed buttons] for the CKEditor.

File

./varbase_media.install, line 236
Contains install and update for Varbase Media module.

Code

function varbase_media_update_8702() {
  varbase_media_update_8701();
  $module_path = Drupal::service('module_handler')
    ->getModule('varbase_media')
    ->getPath();

  // 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) {
    $embed_button_list = \Drupal::configFactory()
      ->listAll('embed.button.' . $embed_button_id);
    if (count($embed_button_list) > 0) {
      $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', EmbedButton::convertImageToEncodedData($target_file
          ->getFileUri()))
          ->save();
      }
      else {
        return t("Unable to copy @source to the public files directory.", [
          '@source' => $embed_button_info['source'],
        ]);
      }
    }
  }
}