You are here

social_page.install in Open Social 10.3.x

Install, update and uninstall functions for the social_page module.

File

modules/social_features/social_page/social_page.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the social_page module.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\user\Entity\Role;
use Symfony\Component\Yaml\Yaml;

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_page.
 */
function social_page_install() {

  // Set some default permissions.
  _social_page_set_permissions();
}

/**
 * Function to set permissions.
 */
function _social_page_set_permissions() {
  $roles = Role::loadMultiple();

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'administrator') {
      continue;
    }
    $permissions = _social_page_get_permissions($role
      ->id());
    user_role_grant_permissions($role
      ->id(), $permissions);
  }
}

/**
 * Build the permissions.
 */
function _social_page_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [
    'view node.page.field_content_visibility:public content',
  ];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'view node.page.field_content_visibility:community content',
  ]);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['authenticated'], [
    'create page content',
    'delete any page content',
    'delete own page content',
    'edit any page content',
    'edit own page content',
    'translate page node',
    'view page revisions',
    'delete page revisions',
    'revert page revisions',
    'override page revision log entry',
    'override page authored by option',
    'override page published option',
    'override page authored on option',
    'override page promote to front page option',
    'override page revision option',
    'override page sticky option',
  ]);

  // Site manager.
  $permissions['sitemanager'] = array_merge($permissions['contentmanager'], [
    'administer visibility settings',
  ]);
  if (isset($permissions[$role])) {
    return $permissions[$role];
  }
  return [];
}

/**
 * Implements hook_update_dependencies().
 */
function social_page_update_dependencies() {

  // New config changes should run after the update helper module is enabled.
  $dependencies['social_page'][8801] = [
    'social_core' => 8805,
  ];

  // New image style change should run after the new image style is imported.
  $dependencies['social_page'][8907] = [
    'social_core' => 8908,
  ];
  return $dependencies;
}

/**
 * Ensure the hero image field label is not visible.
 */
function social_page_update_8001() {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('social_page', 'social_page_update_8001');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Hide image label in teasers.
 */
function social_page_update_8002() {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('social_page', 'social_page_update_8002');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Empty, moved to social_page_update_8907().
 */
function social_page_update_8903() {

  // Code has been moved to social_page_update_8907().
}

/**
 * Add image file extensions to be added as attachments.
 */
function social_page_update_8904() {
  $config_file = drupal_get_path('module', 'social_page') . '/config/static/field.field.node.page.field_files_8904.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.page.field_files');
      $config
        ->setData($settings)
        ->save(TRUE);
    }
  }
}

/**
 * Updated fieldsets and field arrangement in entity form view of basic page.
 */
function social_page_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_page', 'social_page_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_page_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_page_description', 'node', 'page', 'form', 'default');
  if ($group && empty($group->children)) {
    field_group_delete_field_group($group);
  }
}

/**
 * Show thumbnail on File fields when attachment is an image.
 */
function social_page_update_8907() {
  $config_file = drupal_get_path('module', 'social_page') . '/config/static/core.entity_view_display.node.page.default_8907.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.page.default');
      $config
        ->setData($settings)
        ->save(TRUE);
    }
  }
}

/**
 * Add small_teaser view mode for page.
 */
function social_page_update_8908() {
  $config_path = drupal_get_path('module', 'social_page') . '/config/static';
  $source = new FileStorage($config_path);

  /** @var \Drupal\Core\Config\StorageInterface $config_storage */
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('core.entity_view_display.node.page.small_teaser', $source
    ->read('social_page_static_update_8908'));
}

/**
 * Updated basic page form labels.
 */
function social_page_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_page', 'social_page_update_10201');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Allow CM+ translate "Basic Page" node type.
 */
function social_page_update_10301() {
  foreach ([
    'sitemanager',
    'contentmanager',
  ] as $role_id) {
    user_role_grant_permissions($role_id, [
      'translate page node',
    ]);
  }
}

/**
 * Remove layout_builder settings from small_teaser view mode for page.
 */
function social_page_update_10302() {
  $config = \Drupal::configFactory()
    ->getEditable('core.entity_view_display.node.page.small_teaser');
  $config
    ->set('third_party_settings', [])
    ->save();
}

Functions

Namesort descending Description
social_page_install Implements hook_install().
social_page_update_10201 Updated basic page form labels.
social_page_update_10301 Allow CM+ translate "Basic Page" node type.
social_page_update_10302 Remove layout_builder settings from small_teaser view mode for page.
social_page_update_8001 Ensure the hero image field label is not visible.
social_page_update_8002 Hide image label in teasers.
social_page_update_8903 Empty, moved to social_page_update_8907().
social_page_update_8904 Add image file extensions to be added as attachments.
social_page_update_8905 Updated fieldsets and field arrangement in entity form view of basic page.
social_page_update_8906 Remove group_topic_description fieldgroup from topic content type.
social_page_update_8907 Show thumbnail on File fields when attachment is an image.
social_page_update_8908 Add small_teaser view mode for page.
social_page_update_dependencies Implements hook_update_dependencies().
_social_page_get_permissions Build the permissions.
_social_page_set_permissions Function to set permissions.