You are here

social_like.install in Open Social 10.1.x

Install, update and uninstall functions for the social_like module.

File

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

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

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

  // Set some default permissions.
  _social_like_set_permissions();
  _social_like_set_defaults();
}

/**
 * Re-set permissions for viewing the like widget.
 */
function social_like_update_8001() {

  // Set some default permissions.
  _social_like_set_permissions();
}

/**
 * Re-set permissions for viewing the like widget.
 */
function social_like_update_8002() {

  // Revoke permission for AN like widget.
  user_role_revoke_permissions('anonymous', [
    'view like widget',
  ]);
}

/**
 * Disable bootstrap modal.
 */
function social_like_update_8003() {

  // Bootstrap modal breaks jquery.dialog, so turn it off.
  $settings = \Drupal::configFactory()
    ->getEditable('socialbase.settings');
  $settings
    ->set('modal_enabled', 0)
    ->save();
}

/**
 * Install views_infinite_scroll module.
 */
function social_like_update_8004() {
  $modules = [
    'views_infinite_scroll',
  ];
  \Drupal::service('module_installer')
    ->install($modules);
}

/**
 * Enable deleting likes from a database when a user is deleted.
 */
function social_like_update_8005() {
  \Drupal::configFactory()
    ->getEditable('votingapi.settings')
    ->set('delete_everywhere', TRUE)
    ->save();
}

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

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

/**
 * Set default config.
 */
function _social_like_set_defaults() {

  // Add like and dislike settings.

  /** @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig $config */
  $container = \Drupal::getContainer()
    ->get('config.factory');
  $config = $container
    ->getEditable('like_and_dislike.settings');
  $config
    ->set('hide_vote_widget', TRUE)
    ->save();
  $config
    ->set('enabled_types', [
    'comment' => [
      'comment',
      'post_comment',
    ],
    'node' => [
      'topic',
      'event',
    ],
    'post' => [
      'post',
    ],
  ])
    ->save();

  // Add voting api settings.
  $config = $container
    ->getEditable('like_and_dislike.settings');
  $config
    ->set('delete_everywhere', FALSE)
    ->save();
}

/**
 * Build the permissions.
 */
function _social_like_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [
    'view like widget',
  ];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'add or remove like votes on post',
    'add or remove like votes on comment of comment',
    'add or remove like votes on post_comment of comment',
    'add or remove like votes on topic of node',
    'add or remove like votes on event of node',
  ]);

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

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

Functions

Namesort descending Description
social_like_install Implements hook_install().
social_like_update_8001 Re-set permissions for viewing the like widget.
social_like_update_8002 Re-set permissions for viewing the like widget.
social_like_update_8003 Disable bootstrap modal.
social_like_update_8004 Install views_infinite_scroll module.
social_like_update_8005 Enable deleting likes from a database when a user is deleted.
_social_like_get_permissions Build the permissions.
_social_like_set_defaults Set default config.
_social_like_set_permissions Function to set permissions.