You are here

sharemessage.install in Share Message 8

Same filename and directory in other branches
  1. 7 sharemessage.install

Update function for the Share Message module.

File

sharemessage.install
View source
<?php

/**
 * @file
 * Update function for the Share Message module.
 */
use Drupal\Core\Session\AccountInterface;

/**
 * Update override default settings and set the AddThis plugin.
 */
function sharemessage_update_8001() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory
    ->listAll('sharemessage.sharemessage') as $name) {
    $config = $config_factory
      ->getEditable($name);
    $override_default_settings = $config
      ->get('override_default_settings');
    $config
      ->clear('override_default_settings');
    $config
      ->set('settings.override_default_settings', $override_default_settings);
    $config
      ->set('plugin', 'addthis');
    $config
      ->save();
  }
  $config_factory
    ->rename('sharemessage.settings', 'sharemessage.addthis');
}

/**
 * Migrate 'message_enforcement' settings to new configuration.
 */
function sharemessage_update_8002() {
  $config_factory = \Drupal::configFactory();
  $addthis_config = $config_factory
    ->getEditable('sharemessage.addthis');
  $sharemessage_config = $config_factory
    ->getEditable('sharemessage.settings');
  $message_enforcement = $addthis_config
    ->get('message_enforcement');
  $addthis_config
    ->clear('message_enforcement');
  $sharemessage_config
    ->set('message_enforcement', $message_enforcement);
  $addthis_config
    ->save();
  $sharemessage_config
    ->save();
}

/**
 * Implements hook_install().
 */
function sharemessage_install() {
  user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
    'view sharemessages',
  ]);
  user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, [
    'view sharemessages',
  ]);
}

/**
 * Implements hook_requirements().
 */
function sharemessage_requirements($phase) {
  $requirements = [];
  if ($phase == 'runtime') {
    $library_discovery = \Drupal::service('library.discovery');
    $library = $library_discovery
      ->getLibraryByName('sharemessage', 'socialshareprivacy');
    $has_social_share = FALSE;
    if (isset($library['library path'])) {
      $has_social_share = is_file(ltrim($library['library path'], '/') . '/jquery.socialshareprivacy.min.js');
    }
    $storage = \Drupal::entityTypeManager()
      ->getStorage('sharemessage');
    $in_use = (int) $storage
      ->getQuery()
      ->condition('plugin', 'socialshareprivacy')
      ->count()
      ->execute();
    $requirements['socialshareprivacy_library'] = [
      'title' => t('Share message'),
      'value' => t('Installed correctly'),
      'description' => t('Share message module has been installed correctly.'),
    ];
    if (!$has_social_share) {
      if ($in_use) {
        $requirements['socialshareprivacy_library']['value'] = t('Dependencies not found');
        $requirements['socialshareprivacy_library']['severity'] = REQUIREMENT_ERROR;
        $requirements['socialshareprivacy_library']['description'] = t('The <a href=":url">Social Share Privacy library</a> should be installed in <strong>/libraries/socialshareprivacy</strong>. The directory must contain jquery.socialshareprivacy.min.js and stylesheets/jquery.socialshareprivacy.min.css', [
          ':url' => 'http://panzi.github.io/SocialSharePrivacy/',
        ]);
      }
      else {
        $requirements['socialshareprivacy_library']['value'] = t('Social Share Privacy plugin');
        $requirements['socialshareprivacy_library']['severity'] = REQUIREMENT_INFO;
        $requirements['socialshareprivacy_library']['description'] = t('Share message adds the Social Share Privacy plugin, please visit <a href=":url">this page</a> for download instructions and place the library in <strong>/libraries/socialshareprivacy</strong>. The directory must contain jquery.socialshareprivacy.min.js and stylesheets/jquery.socialshareprivacy.min.css.', [
          ':url' => 'http://panzi.github.io/SocialSharePrivacy/',
        ]);
      }
    }
  }
  return $requirements;
}

/**
 * @} End of "addtogroup updates-8.0.0-beta".
 */

Functions

Namesort descending Description
sharemessage_install Implements hook_install().
sharemessage_requirements Implements hook_requirements().
sharemessage_update_8001 Update override default settings and set the AddThis plugin.
sharemessage_update_8002 Migrate 'message_enforcement' settings to new configuration.