social_profile.install in Open Social 8.5
Same filename and directory in other branches
- 8.9 modules/social_features/social_profile/social_profile.install
- 8 modules/social_features/social_profile/social_profile.install
- 8.2 modules/social_features/social_profile/social_profile.install
- 8.3 modules/social_features/social_profile/social_profile.install
- 8.4 modules/social_features/social_profile/social_profile.install
- 8.6 modules/social_features/social_profile/social_profile.install
- 8.7 modules/social_features/social_profile/social_profile.install
- 8.8 modules/social_features/social_profile/social_profile.install
- 10.3.x modules/social_features/social_profile/social_profile.install
- 10.0.x modules/social_features/social_profile/social_profile.install
- 10.1.x modules/social_features/social_profile/social_profile.install
- 10.2.x modules/social_features/social_profile/social_profile.install
Install, update and uninstall functions for the social_profile module.
File
modules/social_features/social_profile/social_profile.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the social_profile module.
*/
use Drupal\file\Entity\File;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\user\Entity\Role;
use Drupal\block\Entity\Block;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
use Drupal\user\Entity\User;
/**
* Implements hook_install().
*
* Perform actions related to the installation of social_profile.
*/
function social_profile_install() {
// Set some default permissions.
_social_profile_set_permissions();
// Add some links.
_social_profile_create_menu_links();
// Add default profile image.
_social_profile_add_default_profile_image();
// Set default config.
_social_profile_set_default_config();
// Create a profile for user 1.
Profile::create([
'type' => ProfileType::load('profile')
->id(),
'uid' => 1,
])
->save();
}
/**
* Function to set default profile image if not set already.
*/
function _social_profile_add_default_profile_image() {
// Add default image.
$config_factory = \Drupal::configFactory();
$field_image_config = $config_factory
->getEditable('field.field.profile.profile.field_profile_image');
$default_image = $field_image_config
->get('settings.default_image');
$uri = file_unmanaged_copy(drupal_get_path('module', 'social_profile') . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'default-profile-picture.png', 'public://default-profile-picture.png', FILE_EXISTS_REPLACE);
$media = File::create([
'uri' => $uri,
]);
$media->uuid = '4eb1d927-28f4-402a-8c87-017e637f434a';
$media->status = 1;
$media
->save();
$default_image['uuid'] = $media
->uuid();
$default_image['alt'] = 'Default profile image';
$default_image['title'] = 'Default profile image';
$default_image['height'] = 200;
$default_image['width'] = 200;
$field_image_config
->set('settings.default_image', $default_image)
->save(TRUE);
drupal_flush_all_caches();
}
/**
* Function to set permissions.
*/
function _social_profile_set_permissions() {
$roles = Role::loadMultiple();
/** @var \Drupal\user\Entity\Role $role */
foreach ($roles as $role) {
if ($role
->id() === 'administrator') {
continue;
}
$permissions = _social_profile_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_profile_get_permissions($role) {
// Anonymous.
$permissions['anonymous'] = [];
// Authenticated.
$permissions['authenticated'] = array_merge($permissions['anonymous'], [
'add own profile profile',
'update own profile profile',
'view any profile profile',
'view own profile profile',
'view profile',
]);
// Content manager.
$permissions['contentmanager'] = array_merge($permissions['authenticated'], [
'add any profile profile',
'update any profile profile',
'edit profile tags',
]);
// Site manager.
$permissions['sitemanager'] = array_merge($permissions['contentmanager'], [
'delete terms in profile_tag',
'edit terms in profile_tag',
'administer profile settings',
'view profile email',
]);
if (isset($permissions[$role])) {
return $permissions[$role];
}
return [];
}
/**
* Function to create some menu items.
*/
function _social_profile_create_menu_links() {
$menu_links = MenuLinkContent::loadMultiple();
$parent = NULL;
/** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */
foreach ($menu_links as $menu_link) {
if ($menu_link
->label() === 'Explore' && $menu_link
->isExpanded()) {
$parent = 'menu_link_content:' . $menu_link
->uuid();
}
}
if (!is_null($parent)) {
MenuLinkContent::create([
'title' => 'All members',
'link' => [
'uri' => 'internal:/all-members',
],
'menu_name' => 'main',
'expanded' => FALSE,
'weight' => 50,
'parent' => $parent,
])
->save();
}
}
/**
* Set default config.
*/
function _social_profile_set_default_config() {
$config = \Drupal::configFactory()
->getEditable('social_profile.settings');
$config
->set('social_profile_show_email', 1);
$config
->save();
}
/**
* Implements hook_uninstall().
*/
function social_profile_uninstall() {
\Drupal::service('config.factory')
->getEditable('social_profile.settings')
->delete();
}
/**
* Set default profile image.
*/
function social_profile_update_8002(&$sandbox) {
// Only run when there is not a file added to the file managed table.
// Confirmed that it's not there on our current platforms. (But is locally).
$query = \Drupal::database()
->select('file_managed', 'fm');
$query
->addField('fm', 'uuid');
$query
->condition('fm.filename', 'default-profile-picture.png');
$query
->range(0, 1);
$result = $query
->execute()
->fetchField();
// $result will be FALSE if there is no UUID available.
if (!$result) {
_social_profile_add_default_profile_image();
}
}
/**
* Add social_profile.settings config. Update permissions for SM role.
*/
function social_profile_update_8003(&$sandbox) {
_social_profile_set_default_config();
// Add permission to view profile email and administer profile settings for
// sitemanager role.
$roles = Role::loadMultiple();
$permissions = [
'administer profile settings',
'view profile email',
];
/** @var \Drupal\user\Entity\Role $role */
foreach ($roles as $role) {
if ($role
->id() === 'sitemanager') {
user_role_grant_permissions($role
->id(), $permissions);
}
}
}
/**
* Add edit own and edit any profile permissions after update to Profile beta1.
*/
function social_profile_update_8004(&$sandbox) {
$roles = Role::loadMultiple();
$lu_permissions = [
'update own profile profile',
];
$cm_permissions = [
'update any profile profile',
];
/** @var \Drupal\user\Entity\Role $role */
foreach ($roles as $role) {
if ($role
->id() === 'authenticated') {
user_role_grant_permissions($role
->id(), $lu_permissions);
}
if ($role
->id() === 'contentmanager' || $role
->id() === 'sitemanager') {
user_role_grant_permissions($role
->id(), $cm_permissions);
}
}
}
/**
* Update the context_mapping for profile_hero_block blocks.
*/
function social_profile_update_8005() {
$blocks = Block::loadMultiple();
/** @var \Drupal\block\BlockInterface $block */
foreach ($blocks as $block) {
if ($block
->getPluginId() == 'profile_hero_block') {
$settings = $block
->get('settings');
if (!isset($settings['context_mapping']['user'])) {
$settings['context_mapping']['user'] = '@social_user.user_route_context:user';
$block
->set('settings', $settings);
$block
->save();
}
}
}
}
/**
* Create a profile for users that have none.
*/
function social_profile_update_8006(&$sandbox) {
if (!isset($sandbox['progress'])) {
// This must be the first run. Initialize the sandbox.
$sandbox['progress'] = 0;
$sandbox['profiles_created'] = 0;
// We use array_values to get the entity_ids.
// Not interested in the revisions.
$sandbox['uids'] = array_values(\Drupal::entityQuery('user')
->execute());
$sandbox['user_count'] = count($sandbox['uids']);
\Drupal::logger('social_profile')
->info('Checking profile status for @count users', [
'@count' => $sandbox['user_count'],
]);
}
// Try to do 5 each cycle. Never do more than are available.
for ($target = $sandbox['progress'] + 5; $sandbox['progress'] < $target && $sandbox['progress'] < $sandbox['user_count']; $sandbox['progress']++) {
$uid = $sandbox['uids'][$sandbox['progress']];
/** @var \Drupal\user\Entity\User $account */
$account = User::load($uid);
// For anonymous we do nothing.
if ($account
->isAnonymous()) {
continue;
}
// Check if the user has a profile already.
$profile = \Drupal::entityTypeManager()
->getStorage('profile')
->loadByUser($account, ProfileType::load('profile')
->id(), TRUE);
if (!$profile) {
try {
Profile::create([
'type' => ProfileType::load('profile')
->id(),
'uid' => $account
->id(),
])
->save();
$sandbox['profiles_created']++;
} catch (Exception $e) {
\Drupal::logger('social_profile')
->error('Could not create profile for @user_id', [
'@user_id' => $uid,
]);
}
}
}
$sandbox['#finished'] = empty($sandbox['user_count']) ? 1 : $sandbox['progress'] / $sandbox['user_count'];
if ($sandbox['#finished'] === 1) {
\Drupal::logger('social_profile')
->info('Created profiles for @count users', [
'@count' => $sandbox['profiles'],
]);
}
}
/**
* Grant the 'view profile' permission.
*/
function social_profile_update_8007(&$sandbox) {
$roles = [
'authenticated',
'contentmanager',
'sitemanager',
];
$permissions = [
'view profile',
];
foreach ($roles as $role) {
user_role_grant_permissions($role, $permissions);
}
}
Functions
Name | Description |
---|---|
social_profile_install | Implements hook_install(). |
social_profile_uninstall | Implements hook_uninstall(). |
social_profile_update_8002 | Set default profile image. |
social_profile_update_8003 | Add social_profile.settings config. Update permissions for SM role. |
social_profile_update_8004 | Add edit own and edit any profile permissions after update to Profile beta1. |
social_profile_update_8005 | Update the context_mapping for profile_hero_block blocks. |
social_profile_update_8006 | Create a profile for users that have none. |
social_profile_update_8007 | Grant the 'view profile' permission. |
_social_profile_add_default_profile_image | Function to set default profile image if not set already. |
_social_profile_create_menu_links | Function to create some menu items. |
_social_profile_get_permissions | Return the permissions per role. |
_social_profile_set_default_config | Set default config. |
_social_profile_set_permissions | Function to set permissions. |