social_lms_integrator-1.0.0-beta4/social_lms_integrator.install
social_lms_integrator.install
<?php
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Implements hook_uninstall().
*/
function social_lms_integrator_uninstall() {
// Remove social_lms_integrator_languages taxonomy vocabulary.
if ($vocabulary = Vocabulary::load('social_lms_integrator_languages')) {
$vocabulary->delete();
}
// Remove social_lms_integrator_templates taxonomy vocabulary.
if ($vocabulary = Vocabulary::load('social_lms_integrator_templates')) {
$vocabulary->delete();
}
}
/**
* Remove 'unwanted entity language_version'.
*/
function social_lms_integrator_update_8001() {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $manager->getEntityType('language_version');
if ($entity_type instanceof EntityTypeInterface) {
$manager->uninstallEntityType($entity_type);
}
}
function _social_lms_integrator_update_or_install_config( String $prefix, String $update_id, String $module) {
$updated = [];
$created = [];
/** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
$config_manager = \Drupal::service('config.manager');
$files = glob(drupal_get_path('module', $module) . '/config/update_' . $update_id. '/' . $prefix . '*.yml') ;
foreach ($files as $file) {
$raw = file_get_contents($file);
$value = \Drupal\Component\Serialization\Yaml::decode($raw);
if(!is_array($value)) {
throw new \RuntimeException(sprintf('Invalid YAML file %s'), $file);
}
$type = $config_manager->getEntityTypeIdByName(basename($file));
$entity_manager = $config_manager->getEntityTypeManager();
$definition = $entity_manager->getDefinition($type);
$id_key = $definition->getKey('id');
$id = $value[$id_key];
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $entity_storage */
$entity_storage = $entity_manager->getStorage($type);
$entity = $entity_storage->load($id);
if ($entity) {
$entity = $entity_storage->updateFromStorageRecord($entity, $value);
$entity->save();
$updated[] = $id;
}
else {
$entity = $entity_storage->createFromStorageRecord($value);
$entity->save();
$created[] = $id;
}
}
return [
'udpated' => $updated,
'created' => $created,
];
}
