You are here

social_comment.install in Open Social 8.8

Install, update and uninstall functions for the social_comment module.

File

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

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

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

  // Set some default permissions.
  _social_comment_set_permissions();

  // Set the view mode to use when shown in activities.
  activity_creator_set_entity_view_mode('comment', 'activity');
}

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

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'administrator') {
      continue;
    }
    $permissions = _social_comment_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_comment_get_permissions($role) {

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

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'access comments',
    'post comments',
    'skip comment approval',
    'edit own comments',
    'delete own comments',
    'administer own comments',
  ]);

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

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

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

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

/**
 * Enable 'administer own comments' permission for authenticated users.
 */
function social_comment_update_8001(&$sandbox) {
  $roles = Role::loadMultiple();
  $permissions = [
    'administer own comments',
  ];

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

/**
 * Enable 'administer comments' permission for sitemanagers and contentmanagers.
 */
function social_comment_update_8002() {

  // These permissions were added to default installs in PR 959 but an update
  // hook was not added at that point so this must be rectified now.
  user_role_grant_permissions('contentmanager', [
    'administer comments',
  ]);
  user_role_grant_permissions('sitemanager', [
    'administer comments',
  ]);
}

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

/**
 * Ensure like_and_dislike widget is rendered correctly.
 */
function social_comment_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_comment', 'social_comment_update_8901');

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

Functions

Namesort descending Description
social_comment_install Implements hook_install().
social_comment_update_8001 Enable 'administer own comments' permission for authenticated users.
social_comment_update_8002 Enable 'administer comments' permission for sitemanagers and contentmanagers.
social_comment_update_8801 Set the view mode to use when shown in activities.
social_comment_update_8901 Ensure like_and_dislike widget is rendered correctly.
social_comment_update_dependencies Implements hook_update_dependencies().
_social_comment_get_permissions Build the permissions.
_social_comment_set_permissions Function to set permissions.