You are here

opigno_class.install in Opigno class 8

Same filename and directory in other branches
  1. 3.x opigno_class.install

Install, update and uninstall functions for the Opigno Class module.

File

opigno_class.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Opigno Class module.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Create new field in opigno_class group type.
 */
function opigno_class_update_8001() {
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/install';
  $storage = new FileStorage($config_path);
  $data = $storage
    ->read('field.storage.group.field_class_media_image');
  if (!FieldStorageConfig::loadByName($data['entity_type'], $data['field_name'])) {
    FieldStorageConfig::create($data)
      ->save();
  }
  $data = $storage
    ->read('field.field.group.opigno_class.field_class_media_image');
  if (!FieldConfig::loadByName($data['entity_type'], $data['bundle'], $data['field_name'])) {
    FieldConfig::create($data)
      ->save();
  }
}

/**
 * Update class roles.
 */
function opigno_class_update_8002() {

  // Remove old configs.
  $configs = [
    'group.role.opigno_class-admin',
    'group.role.opigno_class-student',
    'group.role.opigno_class-coach',
  ];
  foreach ($configs as $config) {
    try {
      \Drupal::configFactory()
        ->getEditable($config)
        ->delete();
    } catch (Exception $e) {
      \Drupal::logger('opigno_class')
        ->error($e
        ->getMessage());
    }
  }
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/install';
  $source = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('group.role.opigno_class-class_manager', $source
    ->read('group.role.opigno_class-class_manager'));
}

/**
 * Update view with list of classes and admin menu.
 */
function opigno_class_update_8003() {
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/optional';
  $source = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('views.view.opigno_classes', $source
    ->read('views.view.opigno_classes'));
}

/**
 * Update view with list of classes and admin menu.
 */
function opigno_class_update_8005() {
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/optional';
  $source = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('views.view.opigno_classes', $source
    ->read('views.view.opigno_classes'));
}

/**
 * Update permissions for users with role 'User manager'.
 */
function opigno_class_update_8006() {
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/install';
  $source = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('user.role.user_manager', $source
    ->read('user.role.user_manager'));
  $config_storage
    ->write('userprotect.rule.protection_of_administrators', $source
    ->read('userprotect.rule.protection_of_administrators'));
}

/**
 * Update permissions for users with role 'User manager'.
 */
function opigno_class_update_8007() {
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/rewrite';
  $source = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('user.role.authenticated', $source
    ->read('user.role.authenticated'));
}

/**
 * Enable Userprotect and Role Delegation modules.
 */
function opigno_class_update_8008() {
  \Drupal::service('module_installer')
    ->install([
    'userprotect',
    'role_delegation',
  ]);
}

/**
 * Update Class group structure.
 */
function opigno_class_update_8009() {
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/install';
  $source = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('group.type.opigno_class', $source
    ->read('group.type.opigno_class'));
  $bundles = [
    'opigno_class',
  ];
  $fields['field_class_media_image'] = [
    'entity_type' => 'group',
  ];
  foreach ($bundles as $bundle) {
    foreach ($fields as $field_name => $config) {
      $field = FieldConfig::loadByName($config['entity_type'], $bundle, $field_name);
      if (!empty($field)) {
        $field
          ->delete();
      }
    }
  }
  foreach ($fields as $field_name => $config) {
    $field_storage = FieldStorageConfig::loadByName($config['entity_type'], $field_name);
    if (!empty($field_storage)) {
      $field_storage
        ->delete();
    }
  }
}

/**
 * Update permissions for users with role 'Content manager'.
 */
function opigno_class_update_8010() {
  $config_path = drupal_get_path('module', 'opigno_class') . '/config/install';
  $source = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $config_storage
    ->write('user.role.content_manager', $source
    ->read('user.role.content_manager'));
}

Functions

Namesort descending Description
opigno_class_update_8001 Create new field in opigno_class group type.
opigno_class_update_8002 Update class roles.
opigno_class_update_8003 Update view with list of classes and admin menu.
opigno_class_update_8005 Update view with list of classes and admin menu.
opigno_class_update_8006 Update permissions for users with role 'User manager'.
opigno_class_update_8007 Update permissions for users with role 'User manager'.
opigno_class_update_8008 Enable Userprotect and Role Delegation modules.
opigno_class_update_8009 Update Class group structure.
opigno_class_update_8010 Update permissions for users with role 'Content manager'.