You are here

social_private_message.install in Open Social 10.2.x

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;
use Symfony\Component\Yaml\Yaml;

/**
 * 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 */
  \Drupal::getContainer()
    ->get('config.factory')
    ->getEditable('private_message.settings')
    ->set('enable_email_notifications', FALSE)
    ->set('send_by_default', FALSE)
    ->save();

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

  // Alter display configuration of the private message module.
  _social_private_message_set_private_message_default_form_settings();
  _social_private_message_set_private_message_default_view_settings();
  _social_private_message_set_private_message_inbox_view_settings();
  _social_private_message_set_private_message_thread_default_view_settings();
}

/**
 * Implements hook_update_dependencies().
 */
function social_private_message_update_dependencies() {

  // Run the activities view mode update after the final features removal ran.
  $dependencies['social_private_message'][8801] = [
    'social_core' => 8802,
  ];
  return $dependencies;
}

/**
 * Sets the widget for selecting Private Message thread members.
 */
function _social_private_message_set_private_message_default_form_settings() {

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $pm_form_display */
  $pm_form_display = \Drupal::service('entity_type.manager')
    ->getStorage('entity_form_display')
    ->load('private_message_thread.private_message_thread.default');
  $pm_form_members_field = $pm_form_display
    ->getComponent('members');
  $pm_form_members_field['type'] = 'social_private_message_thread_member_widget';
  $pm_form_display
    ->setComponent('members', $pm_form_members_field)
    ->save();
}

/**
 * Set the Open Social specific settings for the private message default view.
 */
function _social_private_message_set_private_message_default_view_settings() {

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $pm_view_display */
  $pm_view_display = \Drupal::service('entity_type.manager')
    ->getStorage('entity_view_display')
    ->load('private_message.private_message.default');
  $created_field = $pm_view_display
    ->getComponent('created');
  $created_field['type'] = 'timestamp_ago';
  $created_field['settings'] = [
    'granularity' => 1,
    'future_format' => '@interval hence',
    'past_format' => '@interval ago',
  ];
  $owner_field = $pm_view_display
    ->getComponent('owner');
  $owner_field['type'] = 'social_private_message_owner_formatter';
  $owner_field['settings'] = [
    'link' => FALSE,
  ];
  $pm_view_display
    ->setComponent('created', $created_field)
    ->setComponent('owner', $owner_field)
    ->save();
}

/**
 * Set the Open Social specific settings for the private message inbox view.
 */
function _social_private_message_set_private_message_inbox_view_settings() {

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $pm_view_display */
  $pm_view_display = \Drupal::service('entity_type.manager')
    ->getStorage('entity_view_display')
    ->load('private_message.private_message.inbox');
  $created_field = $pm_view_display
    ->getComponent('created');
  $created_field['type'] = 'timestamp_ago';
  $created_field['settings'] = [
    'granularity' => 1,
    'future_format' => '@interval hence',
    'past_format' => '@interval ago',
  ];
  $owner_field = $pm_view_display
    ->getComponent('owner');
  $owner_field['type'] = 'entity_reference_label';
  $owner_field['label'] = 'hidden';
  $owner_field['settings'] = [
    'link' => FALSE,
  ];
  $pm_view_display
    ->setComponent('created', $created_field)
    ->setComponent('owner', $owner_field)
    ->save();
}

/**
 * Set the OS specific settings for the private message thread default view.
 */
function _social_private_message_set_private_message_thread_default_view_settings() {

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $pm_view_display */
  $pm_view_display = \Drupal::service('entity_type.manager')
    ->getStorage('entity_view_display')
    ->load('private_message_thread.private_message_thread.default');
  $deleted_field = [
    'weight' => -100,
    'region' => 'content',
    'settings' => [],
    'third_party_settings' => [],
  ];
  $members_field = $pm_view_display
    ->getComponent('members');
  $members_field['type'] = 'social_private_message_thread_member_formatter';
  $members_field['label'] = 'hidden';
  $pm_field = $pm_view_display
    ->getComponent('private_messages');
  $pm_field['label'] = 'hidden';
  $pm_field['settings'] = [
    'message_count' => 30,
    'ajax_previous_load_count' => 5,
    'ajax_refresh_rate' => 120,
    'message_order' => 'asc',
  ];
  $pm_view_display
    ->setComponent('deleted_field', $deleted_field)
    ->setComponent('members', $members_field)
    ->setComponent('private_messages', $pm_field)
    ->save();
}

/**
 * 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',
    'create private messages thread',
    'reply to private messages thread',
    'delete private messages thread',
  ]);

  // 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();
  }
}

/**
 * Config import social_private_message default configs.
 *
 * The feature removal script did not succeed for social_private_message.
 * Import the configs in an update hook.
 */
function social_private_message_update_8801() {
  $config_files = [
    'core.entity_form_display.private_message_thread.private_message_thread.default',
    'core.entity_view_display.private_message.private_message.default',
    'core.entity_view_display.private_message_thread.private_message_thread.default',
  ];

  // Revert the configurations.
  _social_private_message_features_removal_config_revert_helper($config_files);
}

/**
 * Revert remaining social_private_message configs not included in 8801.
 */
function social_private_message_update_8802() {
  $config_files = [
    'core.entity_view_display.private_message.private_message.inbox',
    'core.entity_view_display.profile.profile.compact_private_message',
    'core.entity_view_mode.profile.compact_private_message',
    'message.template.create_private_message',
    'views.view.inbox',
  ];

  // Revert the configurations.
  _social_private_message_features_removal_config_revert_helper($config_files);
}

/**
 * Helper function to revert configurations.
 *
 * @param array $config_files
 *   Array of configuration file names.
 */
function _social_private_message_features_removal_config_revert_helper(array $config_files) {
  foreach ($config_files as $config_file) {
    $config = drupal_get_path('module', 'social_private_message') . '/config/features_removal/' . $config_file . '.yml';
    if (is_file($config)) {
      $settings = Yaml::parse(file_get_contents($config));
      if (is_array($settings)) {
        $update_config = \Drupal::configFactory()
          ->getEditable($config_file);
        $update_config
          ->setData($settings)
          ->save(TRUE);
      }
    }
  }
}

/**
 * Update permissions for private messages.
 */
function social_private_message_update_8803() {
  $roles_storage = Drupal::entityTypeManager()
    ->getStorage('user_role');
  $roles = $roles_storage
    ->loadMultiple();
  if (!empty($roles)) {

    /** @var \Drupal\user\RoleInterface $role */
    foreach ($roles as $role) {
      if ($role
        ->id() === 'administrator') {
        continue;
      }
      if ($role
        ->hasPermission('use private messaging system')) {
        user_role_grant_permissions($role
          ->id(), [
          'create private messages thread',
          'reply to private messages thread',
          'delete private messages thread',
        ]);
      }
    }
  }
}

Functions

Namesort descending Description
social_private_message_install Implements hook_uninstall().
social_private_message_update_8001 Set module weight.
social_private_message_update_8002 Remove inbox blocks.
social_private_message_update_8801 Config import social_private_message default configs.
social_private_message_update_8802 Revert remaining social_private_message configs not included in 8801.
social_private_message_update_8803 Update permissions for private messages.
social_private_message_update_dependencies Implements hook_update_dependencies().
_social_private_message_features_removal_config_revert_helper Helper function to revert configurations.
_social_private_message_get_permissions Build the permissions.
_social_private_message_set_permissions Function to set permissions.
_social_private_message_set_private_message_default_form_settings Sets the widget for selecting Private Message thread members.
_social_private_message_set_private_message_default_view_settings Set the Open Social specific settings for the private message default view.
_social_private_message_set_private_message_inbox_view_settings Set the Open Social specific settings for the private message inbox view.
_social_private_message_set_private_message_thread_default_view_settings Set the OS specific settings for the private message thread default view.