o365-2.0.3/modules/o365_groups/o365_groups.install
modules/o365_groups/o365_groups.install
<?php
/**
* @file
* Update and install hooks for the o365_groups module.
*/
use Drupal\Core\Database\Database;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\group\Entity\Group;
/**
* Create our custom time format.
*/
function o365_groups_update_9001(&$sandbox) {
$dateFormat = DateFormat::create([
'id' => 'o365_groups_date',
'label' => 'Microsoft 365 - Group files date',
'locked' => FALSE,
'pattern' => 'j F',
]);
$dateFormat->save();
}
/**
* Install our custom table for saving the group with a team.
*/
function o365_groups_update_9002() {
$spec = o365_groups_schema();
$schema = Database::getConnection()->schema();
$schema->createTable('o365_groups_teams', $spec['o365_groups_teams']);
}
/**
* Migrate the already set data to our custom table.
*/
function o365_groups_update_9003() {
$groups = Group::loadMultiple();
foreach ($groups as $group) {
$uuidField = $group->get('field_o365_groups_teams_id')->getValue();
if (!empty($uuidField)) {
Database::getConnection()->insert('o365_groups_teams')
->fields([
'gid' => $group->id(),
'uuid' => $uuidField[0]['value'],
])
->execute();
}
}
}
/**
* Remove our custom base field.
*/
function o365_groups_update_9004() {
$update_manager = Drupal::service('entity.definition_update_manager');
$definition = $update_manager->getFieldStorageDefinition('field_o365_groups_teams_id', 'group');
$update_manager->uninstallFieldStorageDefinition($definition);
return t('Entity: field_o365_groups_teams_id was uninstalled');
}
/**
* Install our base field, that should still exist until the next release.
*/
function o365_groups_update_9005() {
$groupField = BaseFieldDefinition::create('string')
->setLabel(t('Connected Microsoft 365 group'))
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setDisplayOptions('form', [
'type' => 'select',
'weight' => 30,
])
->setDisplayConfigurable('form', TRUE)
->setRequired(FALSE)
->setTranslatable(TRUE)
->setStorageRequired(TRUE)
->setDisplayConfigurable('view', TRUE)
->setComputed(FALSE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('field_o365_groups_teams_id', 'group', 'o365_groups', $groupField);
}
/**
* Remove groups field_o365_groups_teams_id field.
*/
function o365_groups_update_9006() {
$update_manager = Drupal::service('entity.definition_update_manager');
$definition = $update_manager->getFieldStorageDefinition('field_o365_groups_teams_id', 'group');
$update_manager->uninstallFieldStorageDefinition($definition);
return t('Entity: field_o365_groups_teams_id was uninstalled');
}
/**
* Implements hook_schema().
*/
function o365_groups_schema() {
$schema['o365_groups_teams'] = [
'description' => 'Stores the connection between groups and Microsoft 365 teams',
'fields' => [
'gid' => [
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The group ID',
'not null' => TRUE,
],
'uuid' => [
'type' => 'varchar',
'description' => 'The Teams UUID from Microsoft 365',
'length' => '255',
'not null' => TRUE,
'default' => '',
],
],
'primary key' => ['gid', 'uuid'],
'indexes' => [
'uuid' => ['uuid'],
],
];
return $schema;
}
