You are here

social_topic.install in Open Social 8.9

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;
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 = [
    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',
    '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,
  ];
  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);
    }
  }
}

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_update_8801 Update view mode for new small teaser style.
social_topic_update_8901 Update like setting in small teaser style.
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_set_permissions Function to set permissions.