You are here

social_topic.install in Open Social 8.2

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

/**
 * 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',
  ]);

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

Functions

Namesort descending Description
social_topic_install Implements hook_install().
social_topic_update_8001 Install download_count module.
_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.