social_user.install in Open Social 8.5
Same filename and directory in other branches
- 8.9 modules/social_features/social_user/social_user.install
- 8 modules/social_features/social_user/social_user.install
- 8.2 modules/social_features/social_user/social_user.install
- 8.3 modules/social_features/social_user/social_user.install
- 8.4 modules/social_features/social_user/social_user.install
- 8.6 modules/social_features/social_user/social_user.install
- 8.7 modules/social_features/social_user/social_user.install
- 8.8 modules/social_features/social_user/social_user.install
- 10.3.x modules/social_features/social_user/social_user.install
- 10.0.x modules/social_features/social_user/social_user.install
- 10.1.x modules/social_features/social_user/social_user.install
- 10.2.x modules/social_features/social_user/social_user.install
Install, update and uninstall functions for the social_user module.
File
modules/social_features/social_user/social_user.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the social_user module.
*/
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Implements hook_install().
*
* Perform actions related to the installation of social_user.
*/
function social_user_install() {
// Set some default permissions.
_social_user_set_permissions();
// Set default value for displaying navigation icons.
/** @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig $config */
$config = \Drupal::getContainer()
->get('config.factory')
->getEditable('social_user.navigation.settings');
$config
->set('display_social_private_message_icon', 1)
->save();
$config
->set('display_my_groups_icon', 1)
->save();
// Create the default configuration for the social_user default landing page.
$config = \Drupal::getContainer()
->get('config.factory')
->getEditable('social_user.settings');
$config
->set('social_user_profile_landingpage', 'entity.user.canonical')
->save();
}
/**
* Fix 2 wrongly saved view displays.
*/
function social_user_update_8001(&$sandbox) {
/** @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig $conf_default */
$conf_default = \Drupal::getContainer()
->get('config.factory')
->getEditable('core.entity_view_display.user.user.default');
/** @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig $conf_full */
$conf_full = \Drupal::getContainer()
->get('config.factory')
->getEditable('core.entity_view_display.user.user.full');
/*
* For both these displays, the content is empty.
* In a previous version 3 subfields were exported into the root level of
* 'content'.
* This removes those fields that have no purpose and makes sure
* the update hook for Drupal 8.3.1 works properly.
*/
$conf_default
->set('content', [])
->save();
$conf_full
->set('content', [])
->save();
}
/**
* Set default value for displaying navigation icons.
*/
function social_user_update_8002() {
/** @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig $config */
$config = \Drupal::getContainer()
->get('config.factory')
->getEditable('social_user_navigation.settings');
$config
->set('display_social_private_message_icon', 1)
->save();
$config
->set('display_my_groups_icon', 1)
->save();
}
/**
* Add permissions to view and block users for site manager role.
*/
function social_user_update_8003() {
$permissions = _social_user_get_permissions('sitemanager');
user_role_grant_permissions('sitemanager', $permissions);
}
/**
* Migrate navigation settings into the correct format.
*/
function social_user_update_8004() {
$old_config = \Drupal::getContainer()
->get('config.factory')
->getEditable('social_user_navigation.settings');
$new_config = \Drupal::getContainer()
->get('config.factory')
->getEditable('social_user.navigation.settings');
// Store the values.
$new_config
->setData($old_config
->getRawData());
// Remove the old config altogether.
$old_config
->delete();
}
/**
* Add permissions to administer the navigation settings.
*/
function social_user_update_8005() {
$rid = 'sitemanager';
// Revoke permissions for the role.
$role = Role::load($rid);
if ($role) {
$permissions = [
'administer navigation settings',
];
user_role_grant_permissions($rid, $permissions);
}
}
/**
* Create the default configuration for the social_user help texts.
*/
function social_user_update_8006() {
$config = [
"langcode" => "en",
"signup_help" => "A welcome message with further instructions will be sent to your email address after you successfully enter your email address and a username.",
"login_help" => "",
];
\Drupal::configFactory()
->getEditable('social_user.settings')
->setData($config)
->save();
}
/**
* Install views_bulk_operations module.
*/
function social_user_update_8007() {
$modules = [
'views_bulk_operations',
];
\Drupal::service('module_installer')
->install($modules);
// Rebuild the router for social_event_managers.route_subscriber.
\Drupal::service('router.builder')
->rebuild();
}
/**
* Create the default configuration for the social_user default landing page.
*/
function social_user_update_8008() {
// Create the default configuration for the social_user default landing page.
$config = \Drupal::getContainer()
->get('config.factory')
->getEditable('social_user.settings');
$config
->set('social_user_profile_landingpage', 'entity.user.canonical')
->save();
// Add permissions for sitemanager to administer social user settings.
$role = Role::load('sitemanager');
if ($role instanceof RoleInterface) {
user_role_grant_permissions($role
->id(), [
'administer social_user settings',
]);
}
}
/**
* Function to set permissions.
*/
function _social_user_set_permissions() {
$roles = Role::loadMultiple();
/** @var \Drupal\user\Entity\Role $role */
foreach ($roles as $role) {
if ($role
->id() === 'administrator') {
continue;
}
$permissions = _social_user_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_user_get_permissions($role) {
// Anonymous.
$permissions['anonymous'] = [];
// Authenticated.
$permissions['authenticated'] = array_merge($permissions['anonymous'], [
'access user profiles',
'cancel account',
'change own username',
'select account cancellation method',
]);
// Content manager.
$permissions['contentmanager'] = array_merge($permissions['authenticated'], []);
// Site manager.
$permissions['sitemanager'] = array_merge($permissions['contentmanager'], [
'view users',
'block users',
'administer navigation settings',
'administer social_user settings',
]);
if (isset($permissions[$role])) {
return $permissions[$role];
}
return [];
}
Functions
Name | Description |
---|---|
social_user_install | Implements hook_install(). |
social_user_update_8001 | Fix 2 wrongly saved view displays. |
social_user_update_8002 | Set default value for displaying navigation icons. |
social_user_update_8003 | Add permissions to view and block users for site manager role. |
social_user_update_8004 | Migrate navigation settings into the correct format. |
social_user_update_8005 | Add permissions to administer the navigation settings. |
social_user_update_8006 | Create the default configuration for the social_user help texts. |
social_user_update_8007 | Install views_bulk_operations module. |
social_user_update_8008 | Create the default configuration for the social_user default landing page. |
_social_user_get_permissions | Return the permissions per role. |
_social_user_set_permissions | Function to set permissions. |