social_topic.install in Open Social 10.3.x
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.3 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.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\field\Entity\FieldConfig;
use Drupal\field\FieldConfigInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\user\Entity\Role;
use Symfony\Component\Yaml\Yaml;
/**
* 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 = [
[
'label' => t('Blog'),
'icon' => 'icon-blog',
],
[
'label' => t('Discussion'),
'icon' => 'icon-discussion',
],
[
'label' => t('News'),
'icon' => 'icon-news',
],
];
foreach ($terms as $term) {
$term = Term::create([
'vid' => 'topic_types',
'name' => $term['label'],
'field_topic_type_icon' => $term['icon'],
]);
$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'], [
'translate topic node',
'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',
'administer social_topic 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();
}
}
/**
* Implements hook_update_dependencies().
*/
function social_topic_update_dependencies() {
// New config changes should run after the features removal/revert.
$dependencies['social_topic'][8801] = [
'social_core' => 8805,
];
// New image style change should run after the new image style is imported.
$dependencies['social_topic'][8909] = [
'social_core' => 8908,
];
return $dependencies;
}
/**
* 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);
}
}
}
/**
* Set social_topic settings.
*/
function social_topic_update_8003() {
// Give SM the administer social_topic settings permission.
user_role_grant_permissions('sitemanager', [
'administer social_topic settings',
]);
// Set default topic type settings.
$config = \Drupal::configFactory()
->getEditable('social_topic.settings');
$config
->set('social_topic_type_select_changer', 8);
$config
->save();
}
/**
* Update view mode for new small teaser style.
*/
function social_topic_update_8801() {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');
// Execute configuration update definitions with logging of success.
$updateHelper
->executeUpdate('social_topic', 'social_topic_update_8801');
// Output logged messages to related channel of update execution.
return $updateHelper
->logger()
->output();
}
/**
* Update like setting in small teaser style.
*/
function social_topic_update_8901() {
$config_file = drupal_get_path('module', 'social_topic') . '/config/static/core.entity_view_display.node.topic.small_teaser_8901.yml';
if (is_file($config_file)) {
$settings = Yaml::parse(file_get_contents($config_file));
if (is_array($settings)) {
$config = \Drupal::configFactory()
->getEditable('core.entity_view_display.node.topic.small_teaser');
$config
->setData($settings)
->save(TRUE);
}
}
}
/**
* Empty, moved to social_topic_update_8909().
*/
function social_topic_update_8902() {
// Code has been moved to social_page_update_8909().
}
/**
* Add image file extensions to be added as attachments.
*/
function social_topic_update_8903() {
$config_file = drupal_get_path('module', 'social_topic') . '/config/static/field.field.node.topic.field_files_8903.yml';
if (is_file($config_file)) {
$settings = Yaml::parse(file_get_contents($config_file));
if (is_array($settings)) {
$config = \Drupal::configFactory()
->getEditable('field.field.node.topic.field_files');
$config
->setData($settings)
->save(TRUE);
}
}
}
/**
* Updated fieldset labels and added new fieldset called Settings.
*/
function social_topic_update_8904() {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');
// Execute configuration update definitions with logging of success.
$updateHelper
->executeUpdate('social_topic', 'social_topic_update_8904');
// Output logged messages to related channel of update execution.
return $updateHelper
->logger()
->output();
}
/**
* Added fields to their new respective fieldsets.
*/
function social_topic_update_8905() {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');
// Execute configuration update definitions with logging of success.
$updateHelper
->executeUpdate('social_topic', 'social_topic_update_8905');
// Output logged messages to related channel of update execution.
return $updateHelper
->logger()
->output();
}
/**
* Remove group_topic_description fieldgroup from topic content type.
*/
function social_topic_update_8906() {
// There can be possibility that someone might have added fields in
// fieldgroup we are removing. So, we want to skip removal if there are any
// children present in fieldgroup.
$group = field_group_load_field_group('group_topic_description', 'node', 'topic', 'form', 'default');
if ($group && empty($group->children)) {
field_group_delete_field_group($group);
}
}
/**
* Create new field & field storage configuration for new topic type icon field.
*/
function social_topic_update_8907(&$sandbox) {
$config_file = drupal_get_path('module', 'social_topic') . '/config/static/social_topic_update_8907.yml';
if (is_file($config_file)) {
$sandbox['configs'] = Yaml::parse(file_get_contents($config_file));
if (!isset($sandbox['total'])) {
// Count the amount we need to add to cover batching..
$sandbox['total'] = count($sandbox['configs']);
$sandbox['current'] = 0;
}
$names = array_keys($sandbox['configs']);
$name = $names[$sandbox['current']++];
$data = $sandbox['configs'][$name];
$parts = explode('.', $name);
switch ($parts[0] . '.' . $parts[1]) {
case 'field.storage':
$entity_type = \Drupal::service('config.manager')
->getEntityTypeIdByName($name);
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type);
$entity = $storage
->createFromStorageRecord($data);
$entity
->save();
break;
case 'field.field':
$field_config = FieldConfig::loadByName($parts[2], $parts[3], $parts[4]);
if ($field_config instanceof FieldConfigInterface) {
$field_config
->setDescription($data);
}
else {
$field_config = FieldConfig::create($data);
}
$field_config
->save();
break;
default:
// Fallback similar to before.
\Drupal::configFactory()
->getEditable($name)
->setData($data)
->save(TRUE);
}
$sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
}
}
/**
* Add icons to default topic types.
*/
function social_topic_update_8908() {
// Default topic types.
$topic_types = [
[
'label' => t('Blog')
->render(),
'icon' => 'icon-blog',
],
[
'label' => t('Discussion')
->render(),
'icon' => 'icon-discussion',
],
[
'label' => t('News')
->render(),
'icon' => 'icon-news',
],
];
foreach ($topic_types as $type) {
$term = _social_topic_get_term_id_by_name($type['label']);
if ($term && $term
->hasField('field_topic_type_icon') && $term->field_topic_type_icon
->isEmpty()) {
$term->field_topic_type_icon->value = $type['icon'];
$term
->save();
}
}
}
/**
* Find topic type term by name.
*/
function _social_topic_get_term_id_by_name($term_name) {
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'name' => $term_name,
'vid' => 'topic_types',
]);
$term = reset($terms);
return !empty($term) ? $term : FALSE;
}
/**
* Show thumbnail on File fields when attachment is an image.
*/
function social_topic_update_8909() {
$config_file = drupal_get_path('module', 'social_topic') . '/config/static/core.entity_view_display.node.topic.default_8909.yml';
if (is_file($config_file)) {
$settings = Yaml::parse(file_get_contents($config_file));
if (is_array($settings)) {
$config = \Drupal::configFactory()
->getEditable('core.entity_view_display.node.topic.default');
$config
->setData($settings)
->save(TRUE);
}
}
}
/**
* Update topic form labels.
*/
function social_topic_update_10201() {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');
// Execute configuration update definitions with logging of success.
$updateHelper
->executeUpdate('social_topic', 'social_topic_update_10201');
// Output logged messages to related channel of update execution.
return $updateHelper
->logger()
->output();
}
/**
* Add translation compatibility for Topic fields and views.
*/
function social_topic_update_10301() {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');
// Execute configuration update definitions with logging of success.
$updateHelper
->executeUpdate('social_topic', 'social_topic_update_10301');
// Output logged messages to related channel of update execution.
return $updateHelper
->logger()
->output();
}
/**
* Allow CM+ translate "Topic" node type.
*/
function social_topic_update_10302() {
foreach ([
'sitemanager',
'contentmanager',
] as $role_id) {
user_role_grant_permissions($role_id, [
'translate topic node',
]);
}
}
/**
* Update the configs in the database in accordance with the newest schema.
*/
function social_topic_update_10303() {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');
// Execute configuration update definitions with logging of success.
$updateHelper
->executeUpdate('social_topic', 'social_topic_update_10303');
// Output logged messages to related channel of update execution.
return $updateHelper
->logger()
->output();
}
Functions
Name![]() |
Description |
---|---|
social_topic_install | Implements hook_install(). |
social_topic_update_10201 | Update topic form labels. |
social_topic_update_10301 | Add translation compatibility for Topic fields and views. |
social_topic_update_10302 | Allow CM+ translate "Topic" node type. |
social_topic_update_10303 | Update the configs in the database in accordance with the newest schema. |
social_topic_update_8001 | Install download_count module. |
social_topic_update_8002 | Set my topics permissions. |
social_topic_update_8003 | Set social_topic settings. |
social_topic_update_8801 | Update view mode for new small teaser style. |
social_topic_update_8901 | Update like setting in small teaser style. |
social_topic_update_8902 | Empty, moved to social_topic_update_8909(). |
social_topic_update_8903 | Add image file extensions to be added as attachments. |
social_topic_update_8904 | Updated fieldset labels and added new fieldset called Settings. |
social_topic_update_8905 | Added fields to their new respective fieldsets. |
social_topic_update_8906 | Remove group_topic_description fieldgroup from topic content type. |
social_topic_update_8907 | Create new field & field storage configuration for new topic type icon field. |
social_topic_update_8908 | Add icons to default topic types. |
social_topic_update_8909 | Show thumbnail on File fields when attachment is an image. |
social_topic_update_dependencies | Implements hook_update_dependencies(). |
_social_topic_create_menu_links | Function to create some menu items. |
_social_topic_get_permissions | Return the permissions per role. |
_social_topic_get_term_id_by_name | Find topic type term by name. |
_social_topic_set_permissions | Function to set permissions. |