You are here

varbase_editor.install in Varbase Editor 8.7

Install, update and uninstall functions for the Varbase editor module.

File

varbase_editor.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Varbase editor module.
 */
use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\FileStorage;

/**
 * Implements hook_install().
 */
function varbase_editor_install() {
  $module_name = preg_replace('/_install$/', '', __FUNCTION__);
  $module_path = Drupal::service('module_handler')
    ->getModule($module_name)
    ->getPath();

  // Processer for install: in [$module_name].info.yml file.
  // ---------------------------------------------------------------------------.
  $module_info_file = $module_path . '/' . $module_name . '.info.yml';
  if (file_exists($module_info_file)) {
    $module_info_data = (array) Yaml::parse(file_get_contents($module_info_file));
    if (isset($module_info_data['install']) && is_array($module_info_data['install'])) {
      \Drupal::service('module_installer')
        ->install($module_info_data['install'], TRUE);
    }
  }

  // Install optional configs.
  $optional_install_path = $module_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
  if (is_dir($optional_install_path)) {
    $config_installer = \Drupal::service('config.installer');
    $config_installer
      ->installDefaultConfig('module', $module_name);

    // Install any optional config the module provides.
    $storage = new FileStorage($optional_install_path, StorageInterface::DEFAULT_COLLECTION);
    $config_installer
      ->installOptionalConfig($storage, '');

    // Have the .settings.yml configs into the active config.
    $settings_config_files = \Drupal::service('file_system')
      ->scanDirectory($optional_install_path, '/^.*\\.(settings.yml)$/i');
    if (isset($settings_config_files) && is_array($settings_config_files)) {
      foreach ($settings_config_files as $settings_config_file) {
        $settings_config_file_content = file_get_contents(DRUPAL_ROOT . '/' . $settings_config_file->uri);
        $settings_config_file_data = (array) Yaml::parse($settings_config_file_content);
        $config_factory = \Drupal::configFactory()
          ->getEditable($settings_config_file->name);
        $config_factory
          ->setData($settings_config_file_data)
          ->save(TRUE);
      }
    }
  }

  // ---------------------------------------------------------------------------
}

/**
 * WARNING: The "Full HTML" text format was mistakenly referred to by its.
 *
 * Machine name (restricted_html), this is confusing and wrong. Therefore,
 * this filter has been deprecated in favor of better naming to become
 * "HTML code" (code_html).
 *
 * Please make sure to revise your content if it was using the "Full HTML"
 * text format. You can enable it again if you like, or switch to use the
 * new "HTML code" text format.
 */
function varbase_editor_update_8001() {

  // Enable HTML code text format (code_html).
  $code_html_editor_config = \Drupal::service('config.factory')
    ->getEditable('editor.editor.code_html');
  $code_html_editor_config_file = \Drupal::root() . '/' . drupal_get_path('module', 'varbase_editor') . '/config/install/editor.editor.code_html.yml';
  $code_html_editor_config_content = file_get_contents($code_html_editor_config_file);
  $code_html_editor_config_content_data = (array) Yaml::parse($code_html_editor_config_content);
  $code_html_editor_config
    ->setData($code_html_editor_config_content_data)
    ->save();
  $code_html_format_config = \Drupal::service('config.factory')
    ->getEditable('filter.format.code_html');
  $code_html_format_config_file = \Drupal::root() . '/' . drupal_get_path('module', 'varbase_editor') . '/config/install/filter.format.code_html.yml';
  $code_html_format_config_content = file_get_contents($code_html_format_config_file);
  $code_html_format_config_content_data = (array) Yaml::parse($code_html_format_config_content);
  $code_html_format_config
    ->setData($code_html_format_config_content_data)
    ->save();
  if (file_exists(\Drupal::root() . '/' . drupal_get_path('module', 'varbase_editor') . '/config/install/filter.format.restricted_html.yml')) {

    // Disable Full HTML text format (restricted_html).
    $restricted_html_format_config = \Drupal::service('config.factory')
      ->getEditable('filter.format.restricted_html');
    $restricted_html_format_config_file = \Drupal::root() . '/' . drupal_get_path('module', 'varbase_editor') . '/config/install/filter.format.restricted_html.yml';
    $restricted_html_format_config_content = file_get_contents($restricted_html_format_config_file);
    $restricted_html_format_config_content_data = (array) Yaml::parse($restricted_html_format_config_content);
    $restricted_html_format_config
      ->setData($restricted_html_format_config_content_data)
      ->save();
    $restricted_html_editor_config = \Drupal::service('config.factory')
      ->getEditable('editor.editor.restricted_html');
    $restricted_html_editor_config_file = \Drupal::root() . '/' . drupal_get_path('module', 'varbase_editor') . '/config/install/editor.editor.restricted_html.yml';
    $restricted_html_editor_config_content = file_get_contents($restricted_html_editor_config_file);
    $restricted_html_editor_config_content_data = (array) Yaml::parse($restricted_html_editor_config_content);
    $restricted_html_editor_config
      ->setData($restricted_html_editor_config_content_data)
      ->save();
    $update_message = t('<b>WARNING:</b><br /><p>The "Full HTML" text format was mistakenly referred to by its machine name (restricted_html), this is confusing and wrong. Therefore, this filter has been deprecated in favor of better naming to become "HTML code" (code_html).</p><p>Please make sure to revise your content if it was using the "Full HTML" text format. You can enable it again if you like, or switch to use the new "HTML code" text format.</p>');
    \Drupal::logger('varbase_editor')
      ->notice($update_message);
    \Drupal::messenger()
      ->addWarning($update_message);
  }
}

/**
 * Enable pathologic module.
 *
 * Enable image resize filter module.
 * Enable ckeditor bidi module.
 * Enable pathologic module.
 */
function varbase_editor_update_8002() {

  // Enable pathologic module.
  if (!\Drupal::moduleHandler()
    ->moduleExists('pathologic')) {
    \Drupal::service('module_installer')
      ->install([
      'pathologic',
    ], FALSE);
  }

  // Enable image resize filter module.
  if (!\Drupal::moduleHandler()
    ->moduleExists('image_resize_filter')) {
    \Drupal::service('module_installer')
      ->install([
      'image_resize_filter',
    ], FALSE);
  }

  // Enable ckeditor bidi module.
  if (!\Drupal::moduleHandler()
    ->moduleExists('ckeditor_bidi')) {
    \Drupal::service('module_installer')
      ->install([
      'ckeditor_bidi',
    ], FALSE);
  }

  // Enable pathologic module.
  if (!\Drupal::moduleHandler()
    ->moduleExists('pathologic')) {
    \Drupal::service('module_installer')
      ->install([
      'pathologic',
    ], FALSE);
  }
}

/**
 * Enable CKEditor media embed module.
 */
function varbase_editor_update_8003() {
  if (!\Drupal::moduleHandler()
    ->moduleExists('ckeditor_media_embed')) {
    \Drupal::service('module_installer')
      ->install([
      'ckeditor_media_embed',
    ], FALSE);
  }
}

/**
 * Update "Rich editor" to use the CKEditor media embed button.
 */
function varbase_editor_update_8004() {

  // Update "Rich editor" editor config.
  $full_html_editor_config = \Drupal::service('config.factory')
    ->getEditable('editor.editor.full_html');
  $full_html_editor_config_file = \Drupal::root() . '/' . drupal_get_path('module', 'varbase_editor') . '/config/install/editor.editor.full_html.yml';
  $full_html_editor_config_content = file_get_contents($full_html_editor_config_file);
  $full_html_editor_config_content_data = (array) Yaml::parse($full_html_editor_config_content);
  $full_html_editor_config
    ->setData($full_html_editor_config_content_data)
    ->save();

  // Update "Rich editor" filter format config.
  $full_html_format_config = \Drupal::service('config.factory')
    ->getEditable('filter.format.full_html');
  $full_html_format_config_file = \Drupal::root() . '/' . drupal_get_path('module', 'varbase_editor') . '/config/install/filter.format.full_html.yml';
  $full_html_format_config_content = file_get_contents($full_html_format_config_file);
  $full_html_format_config_content_data = (array) Yaml::parse($full_html_format_config_content);
  $full_html_format_config
    ->setData($full_html_format_config_content_data)
    ->save();
}

/**
 * Issue #3037311: Update [Image Resize Filter] module.
 *
 * And update [Varbase Editor] to configure [Rich editor, Simple editor] to
 * use for local and remote images and not to use for [HTML coder].
 */
function varbase_editor_update_8005() {
  if (\Drupal::moduleHandler()
    ->moduleExists('image_resize_filter')) {

    // Update "Rich editor" editor config.
    // ------------------------------------------------------------------------.
    // # filter_image_resize:
    // #   id: filter_image_resize
    // #   provider: image_resize_filter
    // #   status: true
    // #   weight: -40
    // #   settings:
    // #     image_locations:
    // #       local: true
    // #       remote: true
    // #
    // # .
    $full_html_editor_config = \Drupal::service('config.factory')
      ->getEditable('filter.format.full_html');
    if (isset($full_html_editor_config)) {
      $full_html_editor_config_data = $full_html_editor_config
        ->get();
      $full_html_editor_config_data['filters']['filter_image_resize']['status'] = TRUE;
      $full_html_editor_config_data['filters']['filter_image_resize']['settings'] = [
        'image_locations' => [
          'local' => TRUE,
          'remote' => TRUE,
        ],
      ];
      $full_html_editor_config
        ->setData($full_html_editor_config_data)
        ->save(TRUE);
    }

    // Update "Simple editor" editor config.
    // ------------------------------------------------------------------------.
    // # filter_image_resize:
    // #   id: filter_image_resize
    // #   provider: image_resize_filter
    // #   status: true
    // #   weight: 0
    // #   settings:
    // #     image_locations:
    // #       local: true
    // #       remote: true
    // #
    // # .
    $basic_html_editor_config = \Drupal::service('config.factory')
      ->getEditable('filter.format.basic_html');
    if (isset($basic_html_editor_config)) {
      $basic_html_editor_config_data = $basic_html_editor_config
        ->get();
      $basic_html_editor_config_data['filters']['filter_image_resize']['status'] = TRUE;
      $basic_html_editor_config_data['filters']['filter_image_resize']['settings'] = [
        'image_locations' => [
          'local' => TRUE,
          'remote' => TRUE,
        ],
      ];
      $basic_html_editor_config
        ->setData($basic_html_editor_config_data)
        ->save(TRUE);
    }

    // Update "HTML code" editor config.
    // ------------------------------------------------------------------------.
    // # filter_image_resize:
    // #   id: filter_image_resize
    // #   provider: image_resize_filter
    // #   status: false
    // #   weight: -42
    // #   settings:
    // #     image_locations:
    // #       local: false
    // #       remote: false
    // #
    // # .
    $code_html_editor_config = \Drupal::service('config.factory')
      ->getEditable('filter.format.code_html');
    if (isset($code_html_editor_config)) {
      $code_html_editor_config_data = $code_html_editor_config
        ->get();
      $code_html_editor_config_data['filters']['filter_image_resize']['status'] = FALSE;
      $code_html_editor_config_data['filters']['filter_image_resize']['settings'] = [
        'image_locations' => [
          'local' => FALSE,
          'remote' => FALSE,
        ],
      ];
      $code_html_editor_config
        ->setData($code_html_editor_config_data)
        ->save(TRUE);
    }
  }
}

/**
 * Issue #3085555: Have the [Replaces global and entity tokens.
 *
 * With their values] filter only for HTML Editor (code filter) text format.
 */
function varbase_editor_update_8701() {
  if (!\Drupal::moduleHandler()
    ->moduleExists('token_filter')) {
    \Drupal::service('module_installer')
      ->install([
      'token_filter',
    ], FALSE);
  }

  // Update "HTML code" editor config.
  // ------------------------------------------------------------------------.
  // #  token_filter:
  // #    id: token_filter
  // #    provider: token_filter
  // #    status: true
  // #    weight: 0
  // #    settings:
  // #      replace_empty: '1'
  // #
  // # .
  $code_html_editor_config = \Drupal::service('config.factory')
    ->getEditable('filter.format.code_html');
  if (isset($code_html_editor_config)) {
    $code_html_editor_config_data = $code_html_editor_config
      ->get();
    if (!isset($code_html_editor_config_data['filters']['token_filter'])) {
      $code_html_editor_config_data['dependencies']['module'][] = 'token_filter';
      $code_html_editor_config_data['filters']['token_filter'] = [
        'id' => 'token_filter',
        'provider' => 'token_filter',
        'status' => TRUE,
        'weight' => 0,
        'settings' => [
          'replace_empty' => '1',
        ],
      ];
      $code_html_editor_config
        ->setData($code_html_editor_config_data)
        ->save(TRUE);
    }
  }
}

/**
 * Issue #3157438: Fix CKEditor Media Embed not working on any https env.
 */
function varbase_editor_update_8702() {
  if (\Drupal::moduleHandler()
    ->moduleExists('ckeditor_media_embed')) {
    $ckeditor_media_embed_settings = \Drupal::service('config.factory')
      ->getEditable('ckeditor_media_embed.settings');
    $embed_provider = $ckeditor_media_embed_settings
      ->get('embed_provider');
    if (isset($embed_provider) && !empty($embed_provider)) {
      $embed_provider = str_replace("http:", "", $embed_provider);
      $ckeditor_media_embed_settings
        ->set('embed_provider', $embed_provider)
        ->save(TRUE);
    }
  }
}

Functions

Namesort descending Description
varbase_editor_install Implements hook_install().
varbase_editor_update_8001 WARNING: The "Full HTML" text format was mistakenly referred to by its.
varbase_editor_update_8002 Enable pathologic module.
varbase_editor_update_8003 Enable CKEditor media embed module.
varbase_editor_update_8004 Update "Rich editor" to use the CKEditor media embed button.
varbase_editor_update_8005 Issue #3037311: Update [Image Resize Filter] module.
varbase_editor_update_8701 Issue #3085555: Have the [Replaces global and entity tokens.
varbase_editor_update_8702 Issue #3157438: Fix CKEditor Media Embed not working on any https env.