You are here

social_private_message.install in Open Social 8.5

The Social Private Message (de)installation file.

File

modules/social_features/social_private_message/social_private_message.install
View source
<?php

/**
 * @file
 * The Social Private Message (de)installation file.
 */
use Drupal\user\Entity\Role;

/**
 * Implements hook_uninstall().
 */
function social_private_message_install() {
  _social_private_message_set_permissions();

  // Disable the email notification provided by private_message.

  /** @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig $config */
  $config = \Drupal::getContainer()
    ->get('config.factory')
    ->getEditable('private_message.settings');
  $config
    ->set('enable_email_notifications', FALSE)
    ->save();
  $config
    ->set('send_by_default', FALSE)
    ->save();

  // Set module weight.
  module_set_weight('social_private_message', 1);
}

/**
 * Function to set permissions.
 */
function _social_private_message_set_permissions() {
  $roles = Role::loadMultiple();

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'administrator') {
      continue;
    }
    $permissions = _social_private_message_get_permissions($role
      ->id());
    user_role_grant_permissions($role
      ->id(), $permissions);
  }
}

/**
 * Build the permissions.
 *
 * @param string $role
 *   The role.
 *
 * @return array
 *   Returns an array containing the permissions.
 */
function _social_private_message_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'use private messaging system',
  ]);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['authenticated'], []);

  // Site manager.
  $permissions['sitemanager'] = array_merge($permissions['contentmanager'], []);
  if (isset($permissions[$role])) {
    return $permissions[$role];
  }
  return [];
}

/**
 * Set module weight.
 */
function social_private_message_update_8001() {
  module_set_weight('social_private_message', 1);
}

/**
 * Remove inbox blocks.
 */
function social_private_message_update_8002() {
  $plugin = 'social_private_message_inbox_block';
  $storage = \Drupal::entityTypeManager()
    ->getStorage('block');
  $blocks = $storage
    ->loadByProperties([
    'plugin' => $plugin,
  ]);

  /** @var \Drupal\block\Entity\Block $block */
  foreach ($blocks as $block) {
    $block
      ->delete();
  }
}

Functions