opigno_ilt.install in Opigno Instructor-led Trainings 8
Same filename and directory in other branches
Install, update and uninstall functions for the Opigno ILT module.
File
opigno_ilt.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the Opigno ILT module.
*/
use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\group\Entity\GroupRole;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_install().
*/
function opigno_ilt_install() {
if (!\Drupal::isConfigSyncing()) {
// Allow users to view ILT entities.
$role = Role::load(RoleInterface::AUTHENTICATED_ID);
$role
->grantPermission('view ilt entities');
$role
->save();
// Allow platform-level student managers to score ILT entities.
$role = Role::load('user_manager');
$role
->grantPermission('score ilt entities');
$role
->save();
// Allow group-level student managers to score ILT entities.
$role = GroupRole::load('learning_path-user_manager');
$role
->grantPermission('score ilt entities');
$role
->save();
}
}
/**
* Creates the ILT calendar event type.
*/
function opigno_ilt_update_8001() {
$config_path = drupal_get_path('module', 'opigno_ilt') . '/config/install';
$storage = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$data = $storage
->read('opigno_calendar_event.type.ilt_calendar_event');
$config_storage
->write('opigno_calendar_event.type.ilt_calendar_event', $data);
$data = $storage
->read('field.storage.opigno_calendar_event.field_ilt');
if (!FieldStorageConfig::loadByName($data['entity_type'], $data['field_name'])) {
FieldStorageConfig::create($data)
->save();
}
$data = $storage
->read('field.field.opigno_calendar_event.ilt_calendar_event.field_ilt');
if (!FieldConfig::loadByName($data['entity_type'], $data['bundle'], $data['field_name'])) {
FieldConfig::create($data)
->save();
}
$data = $storage
->read('field.field.opigno_calendar_event.ilt_calendar_event.field_calendar_event_members');
if (!FieldConfig::loadByName($data['entity_type'], $data['bundle'], $data['field_name'])) {
FieldConfig::create($data)
->save();
}
}
/**
* Setup permissions.
*/
function opigno_ilt_update_8002() {
// Allow users to view ILT entities.
$role = Role::load(RoleInterface::AUTHENTICATED_ID);
$role
->grantPermission('view ilt entities');
$role
->save();
// Allow platform-level student managers to score ILT entities.
$role = Role::load('user_manager');
$role
->grantPermission('score ilt entities');
$role
->save();
// Allow group-level student managers to score ILT entities.
$role = GroupRole::load('learning_path-user_manager');
$role
->grantPermission('score ilt entities');
$role
->save();
}
/**
* Add "trainer" field to "opigno_ilt".
*/
function opigno_ilt_update_8003() {
// Add new field.
$definition = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Trainer'))
->setDescription(t('The trainer of the ILT entity.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default');
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('trainer', 'opigno_ilt', 'opigno_ilt', $definition);
}
/**
* Creates create cronjobs.
*/
function opigno_ilt_update_8004() {
// Enable new module opigno_cron.
if (!\Drupal::moduleHandler()
->moduleExists('opigno_cron')) {
\Drupal::service('module_installer')
->install([
'opigno_cron',
]);
}
// Add new cron configuretions.
$config_path = drupal_get_path('module', 'opigno_ilt') . '/config/install';
$storage = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$data = $storage
->read('ultimate_cron.job.opigno_ilt_cron');
$config_storage
->write('ultimate_cron.job.opigno_ilt_cron', $data);
}
Functions
Name | Description |
---|---|
opigno_ilt_install | Implements hook_install(). |
opigno_ilt_update_8001 | Creates the ILT calendar event type. |
opigno_ilt_update_8002 | Setup permissions. |
opigno_ilt_update_8003 | Add "trainer" field to "opigno_ilt". |
opigno_ilt_update_8004 | Creates create cronjobs. |