<?php

/**
 * @file
 * Install, update and uninstall functions for the social_group_secret module.
 */

use Drupal\Core\Config\FileStorage;

/**
 * Implements hook_install().
 */
function social_group_secret_install() {
  // Grant the default permissions for this feature.
  user_role_grant_permissions(
    'sitemanager',
    [
      'create secret_group group',
    ]
  );

  // Set module weight higher as the weight of the "Social Group" module because
  // needs have the Views group type field which added via the "Social Group"
  // module.
  module_set_weight('social_group_secret', 4);

  // Secret groups change the permissions for content in groups so we need to
  // make sure access is rebuilt.
  node_access_rebuild();
}

/**
 * Implements hook_update_dependencies().
 */
function social_group_secret_update_dependencies() {
  // New config changes should run after the features removal/revert.
  $dependencies['social_group_secret'][8801] = [
    'social_core' => 8805,
  ];

  return $dependencies;
}

/**
 * Update view mode for new small teaser style.
 */
function social_group_secret_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_group_secret', 'social_group_secret_update_8801');

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

/**
 * Update form display for "Secret Group" according new 10.x UX design.
 */
function social_group_secret_update_8901() {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

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

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

/**
 * Make group creators as a group managers by default.
 */
function social_group_secret_update_8902() {
  $config_factory = \Drupal::configFactory();
  $group_type = 'secret_group';

  $config = $config_factory->getEditable('group.type.' . $group_type);
  $config->set('creator_membership', TRUE);

  $creator_roles = $config->get('creator_roles');
  $group_role_id = $group_type . '-group_manager';
  if (!in_array($group_role_id, $creator_roles)) {
    $creator_roles[] = $group_role_id;
    $config->set('creator_roles', $creator_roles);
  }

  $config->save(TRUE);
}

/**
 * Install display view modes for group contents for Secret group type.
 */
function social_group_secret_update_10301() {
  // Install new configs.
  $config_files = [
    'core.entity_view_display.group_content.secret_group-group_node-event.activity' => 'core.entity_view_display.group_content.secret_group-group_node-event.activity_10301',
    'core.entity_view_display.group_content.secret_group-group_node-topic.activity' => 'core.entity_view_display.group_content.secret_group-group_node-topic.activity_10301',
  ];

  $config_storage = \Drupal::service('config.storage');
  $config_path = \Drupal::service('extension.list.module')->getPath('social_group_secret') . '/config/static/';
  $source = new FileStorage($config_path);

  foreach ($config_files as $origin_name => $update_name) {
    $config_storage->write($origin_name, $source->read($update_name));
  }
}
