social_topic.install in Open Social 8.3
Same filename and directory in other branches
- 8.9 modules/social_features/social_topic/social_topic.install
- 8 modules/social_features/social_topic/social_topic.install
- 8.2 modules/social_features/social_topic/social_topic.install
- 8.4 modules/social_features/social_topic/social_topic.install
- 8.5 modules/social_features/social_topic/social_topic.install
- 8.6 modules/social_features/social_topic/social_topic.install
- 8.7 modules/social_features/social_topic/social_topic.install
- 8.8 modules/social_features/social_topic/social_topic.install
- 10.3.x modules/social_features/social_topic/social_topic.install
- 10.0.x modules/social_features/social_topic/social_topic.install
- 10.1.x modules/social_features/social_topic/social_topic.install
- 10.2.x modules/social_features/social_topic/social_topic.install
Install, update and uninstall functions for the social_topic module.
File
modules/social_features/social_topic/social_topic.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the social_topic module.
*/
use Drupal\taxonomy\Entity\Term;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\user\Entity\Role;
/**
* Implements hook_install().
*
* Perform actions related to the installation of social_topic.
*/
function social_topic_install() {
// Set some default permissions.
_social_topic_set_permissions();
// Add some links.
_social_topic_create_menu_links();
// Create some default taxonomy terms.
$terms = [
t('Blog'),
t('Discussion'),
t('News'),
];
foreach ($terms as $term_name) {
$term = Term::create([
'vid' => 'topic_types',
'name' => $term_name,
]);
$term
->save();
}
}
/**
* Function to set permissions.
*/
function _social_topic_set_permissions() {
$roles = Role::loadMultiple();
/** @var \Drupal\user\Entity\Role $role */
foreach ($roles as $role) {
if ($role
->id() === 'administrator') {
continue;
}
$permissions = _social_topic_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_topic_get_permissions($role) {
// Anonymous.
$permissions['anonymous'] = [
'view node.topic.field_content_visibility:public content',
];
// Authenticated.
$permissions['authenticated'] = array_merge($permissions['anonymous'], [
'create topic content',
'delete own topic content',
'edit own topic content',
'override topic published option',
'view node.topic.field_content_visibility:community content',
'view topics on my profile',
'view topics on other profiles',
]);
// Content manager.
$permissions['contentmanager'] = array_merge($permissions['authenticated'], [
'delete any topic content',
'edit any topic content',
'revert topic revisions',
'delete topic revisions',
'view topic revisions',
'override topic revision log entry',
'override topic authored by option',
'override topic authored on option',
'override topic promote to front page option',
'override topic revision option',
'override topic sticky option',
]);
// Site manager.
$permissions['sitemanager'] = array_merge($permissions['contentmanager'], [
'administer visibility settings',
]);
if (isset($permissions[$role])) {
return $permissions[$role];
}
return [];
}
/**
* Function to create some menu items.
*/
function _social_topic_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' => t('All topics'),
'link' => [
'uri' => 'internal:/all-topics',
],
'menu_name' => 'main',
'expanded' => FALSE,
'weight' => 40,
'parent' => $parent,
])
->save();
}
}
/**
* Install download_count module.
*/
function social_topic_update_8001() {
}
/**
* Set my topics permissions.
*/
function social_topic_update_8002() {
// Make it so that everyone who has the 'access user profiles' permission,
// now also get these two new permissions.
$permissions = [
'view topics on my profile',
'view topics on other profiles',
];
/** @var \Drupal\user\Entity\Role $role */
foreach (Role::loadMultiple() as $role) {
if ($role
->hasPermission('access user profiles')) {
user_role_grant_permissions($role
->id(), $permissions);
}
}
}
Functions
Name | Description |
---|---|
social_topic_install | Implements hook_install(). |
social_topic_update_8001 | Install download_count module. |
social_topic_update_8002 | Set my topics permissions. |
_social_topic_create_menu_links | Function to create some menu items. |
_social_topic_get_permissions | Return the permissions per role. |
_social_topic_set_permissions | Function to set permissions. |