You are here

social_topic.install in Open Social 8.7

Install, update and uninstall functions for the social_topic module.

File

modules/social_features/social_topic/social_topic.install
View 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();
  }

  // Set default topic type settings.
  $config = \Drupal::configFactory()
    ->getEditable('social_topic.settings');
  $config
    ->set('social_topic_type_select_changer', 8);
  $config
    ->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',
    '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();
  }
}

/**
 * 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();
}

Functions

Namesort descending 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_update_8003 Set social_topic settings.
_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.