social_post.install in Open Social 10.1.x
Same filename and directory in other branches
- 8.9 modules/social_features/social_post/social_post.install
- 8 modules/social_features/social_post/social_post.install
- 8.2 modules/social_features/social_post/social_post.install
- 8.3 modules/social_features/social_post/social_post.install
- 8.4 modules/social_features/social_post/social_post.install
- 8.5 modules/social_features/social_post/social_post.install
- 8.6 modules/social_features/social_post/social_post.install
- 8.7 modules/social_features/social_post/social_post.install
- 8.8 modules/social_features/social_post/social_post.install
- 10.3.x modules/social_features/social_post/social_post.install
- 10.0.x modules/social_features/social_post/social_post.install
- 10.2.x modules/social_features/social_post/social_post.install
Install, update and uninstall functions for the social_post module.
File
modules/social_features/social_post/social_post.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the social_post module.
*/
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
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'];
}
/**
* Create "Featured" view mode/display for post.
*/
function social_post_update_8804() {
// Create a new post featured entity view mode.
if (!EntityViewMode::load('post.featured')) {
EntityViewMode::create([
'targetEntityType' => 'post',
'id' => 'post.featured',
'status' => TRUE,
'label' => t('Featured'),
])
->save();
}
// Create view display for post bundle of Post entity.
if (!EntityViewDisplay::load('post.post.featured')) {
$display = EntityViewDisplay::load('post.post.default')
->toArray();
unset($display['content']['field_post_comments'], $display['hidden']['like_and_dislike']);
$display['content']['like_and_dislike'] = [
'weight' => 3,
'region' => 'content',
];
$display = array_merge($display, [
'uuid' => NULL,
'_core' => NULL,
'targetEntityType' => 'post',
'mode' => 'featured',
]);
EntityViewDisplay::create($display)
->save();
}
}
/**
* 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
Name | 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_8804 | Create "Featured" view mode/display for post. |
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. |