You are here

social_post.install in Open Social 8.8

Install, update and uninstall functions for the social_post module.

File

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

/**
 * @file
 * Install, update and uninstall functions for the social_post module.
 */
use Drupal\Core\Site\Settings;
use Drupal\user\Entity\Role;

/**
 * PROCEED WITH CARE HERE!
 *
 * The below issue causes updates from the social_post module to be recognizes
 * as post updates from the social profile.
 *
 * Therefore don't create update hooks here!!!!
 *
 * https://www.drupal.org/node/2880361
 */

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_post.
 */
function social_post_install() {

  // Set some default permissions.
  _social_post_set_permissions();

  // Set the view mode for posts in activities.
  activity_creator_set_entity_view_mode('post', 'activity');
}

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

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

/**
 * Return the permissions per role.
 *
 * @param string $role
 *   The role to get the permissions for.
 *
 * @return array
 *   A list of permissions.
 */
function _social_post_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [
    'view public posts',
    'view published post entities',
  ];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'add post entities',
    'add post post entities',
    'delete own post entities',
    'edit own post entities',
    'view community posts',
    'view own unpublished post entities',
  ]);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['authenticated'], [
    'delete any post entities',
    'edit any post entities',
    'view unpublished post entities',
    'administer post entities',
  ]);

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

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

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

/**
 * Set the view mode to use when shown in activities.
 */
function social_post_update_8801() {
  activity_creator_set_entity_view_mode('post', 'activity');
}

/**
 * Give access to creating posts of specific types.
 */
function social_post_update_8802(&$sandbox) {
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = \Drupal::entityQuery('user_role')
      ->condition('id', 'administrator', '<>')
      ->count()
      ->execute();
    $sandbox['processed'] = 0;
    $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
    $sandbox['permissions'] = array_keys(\Drupal::service('social_post.permission_generator')
      ->permissions());
  }
  $role_ids = \Drupal::entityQuery('user_role')
    ->condition('id', 'administrator', '<>')
    ->range($sandbox['processed'], $sandbox['limit'])
    ->execute();
  $storage = \Drupal::entityTypeManager()
    ->getStorage('user_role');
  foreach ($role_ids as $role_id) {

    /** @var \Drupal\user\RoleInterface $role */
    $role = $storage
      ->load($role_id);
    if ($role
      ->hasPermission('add post entities')) {
      user_role_grant_permissions($role_id, $sandbox['permissions']);
    }
  }
  $sandbox['processed'] += count($role_ids);
  $sandbox['#finished'] = $sandbox['processed'] / $sandbox['total'];
}

/**
 * Update likes in post activity and comment view modes.
 */
function social_post_update_8901() {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('social_post', 'social_post_update_8901');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

Functions

Namesort descending Description
social_post_install Implements hook_install().
social_post_update_8801 Set the view mode to use when shown in activities.
social_post_update_8802 Give access to creating posts of specific types.
social_post_update_8901 Update likes in post activity and comment view modes.
social_post_update_dependencies Implements hook_update_dependencies().
_social_post_get_permissions Return the permissions per role.
_social_post_set_permissions Function to set permissions.