opigno_moxtra.install in Opigno Moxtra 3.x
Same filename and directory in other branches
Install, update and uninstall functions for the Opigno Moxtra module.
File
opigno_moxtra.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the Opigno Moxtra module.
*/
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Config\FileStorage;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Implements hook_install().
*/
function opigno_moxtra_install($is_syncing) {
// Creates the collaborative workspace field in a training groups.
if ($is_syncing) {
return;
}
$config_path = drupal_get_path('module', 'opigno_moxtra') . '/config/install';
$storage = new FileStorage($config_path);
$data = $storage
->read('field.storage.group.field_workspace');
if (!FieldStorageConfig::loadByName($data['entity_type'], $data['field_name'])) {
FieldStorageConfig::create($data)
->save();
}
$data = $storage
->read('field.field.group.learning_path.field_workspace');
if (!FieldConfig::loadByName($data['entity_type'], $data['bundle'], $data['field_name'])) {
FieldConfig::create($data)
->save();
}
_opigno_moxtra_set_meeting_permissions();
}
/**
* Set meeting permissions.
*/
function _opigno_moxtra_set_meeting_permissions() {
// Allow content managers to start meeting.
$role = Role::load('content_manager');
$role
->grantPermission('start meeting');
$role
->save();
// Allow live meeting managers to start meeting.
$role = Role::load('collaborative_features');
$role
->grantPermission('start meeting');
$role
->grantPermission('score meeting');
$role
->save();
// Allow users to view ILT entities.
$role = Role::load(RoleInterface::AUTHENTICATED_ID);
$role
->grantPermission('view meeting entities');
$role
->save();
// Allow platform-level user managers to score LM entities.
$role = Role::load('user_manager');
$role
->grantPermission('score meeting');
$role
->save();
}
/**
* Implements hook_uninstall().
*/
function opigno_moxtra_uninstall() {
// Remove module configs.
$configs = [
'core.entity_form_display.opigno_calendar_event.meeting_calendar_event.default',
'core.entity_form_display.opigno_calendar_event.meeting_calendar_event.embedded_widget',
'core.entity_view_display.opigno_calendar_event.meeting_calendar_event.default',
'core.entity_view_display.opigno_calendar_event.meeting_calendar_event.embedded_display',
'field.field.group.learning_path.field_workspace',
'field.field.opigno_calendar_event.meeting_calendar_event.field_calendar_event_members',
'field.field.opigno_calendar_event.meeting_calendar_event.field_meeting',
'field.storage.group.field_workspace',
'field.storage.opigno_calendar_event.field_meeting',
'group.content_type.group_content_type_78b202c274a0f',
'opigno_calendar_event.type.meeting_calendar_event',
'opigno_moxtra.settings',
'user.role.collaborative_features',
];
foreach ($configs as $config) {
try {
\Drupal::configFactory()
->getEditable($config)
->delete();
} catch (Exception $e) {
\Drupal::logger('opigno_moxtra')
->error($e
->getMessage());
}
}
}
/**
* Changed view configs.
*/
function opigno_moxtra_update_9001() {
$config_path = drupal_get_path('module', 'opigno_moxtra') . '/config/install';
$storage = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$data = $storage
->read('core.entity_view_display.opigno_calendar_event.meeting_calendar_event.default');
$config_storage
->write('core.entity_view_display.opigno_calendar_event.meeting_calendar_event.default', $data);
}
/**
* Add permissions to administer ILT and LM.
*/
function opigno_moxtra_update_9002() {
_opigno_moxtra_update_user_role('user_manager', [
'administer ilt entities',
'administer meeting entities',
]);
}
/**
* Add permissions to collaborative_features role.
*/
function opigno_moxtra_update_9003() {
_opigno_moxtra_update_user_role('collaborative_features', [
'score meeting',
]);
_opigno_moxtra_update_user_role('user_manager', [
'score meeting',
]);
}
/**
* Add permissions to role.
*/
function _opigno_moxtra_update_user_role($role, $permissions) {
try {
$role = \Drupal::entityTypeManager()
->getStorage('user_role')
->load($role);
} catch (PluginNotFoundException|InvalidPluginDefinitionException $e) {
watchdog_exception('opigno_moxtra_exception', $e);
return;
}
if (!$role instanceof RoleInterface) {
return;
}
foreach ($permissions as $permission) {
$role
->grantPermission($permission);
}
try {
$role
->save();
} catch (EntityStorageException $e) {
watchdog_exception('opigno_moxtra_exception', $e);
}
}
Functions
Name | Description |
---|---|
opigno_moxtra_install | Implements hook_install(). |
opigno_moxtra_uninstall | Implements hook_uninstall(). |
opigno_moxtra_update_9001 | Changed view configs. |
opigno_moxtra_update_9002 | Add permissions to administer ILT and LM. |
opigno_moxtra_update_9003 | Add permissions to collaborative_features role. |
_opigno_moxtra_set_meeting_permissions | Set meeting permissions. |
_opigno_moxtra_update_user_role | Add permissions to role. |