openquestions-1.0.x-dev/openquestions.install
openquestions.install
<?php
use Drupal\user\Entity\Role;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Core\Config\FileStorage;
function openquestions_install() {
$vocabularyStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary');
$termStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$vid = 'oq_application_disposition';
$vocabularyName = 'OQ application disposition';
$termNames = [
'Pending',
'Approved',
'Spam',
'Defunct',
'NotQualified',
'DuplicatedBy',
'NotAppropriate',
'Revoked',
];
$vocabulary = $vocabularyStorage->load($vid);
if (!$vocabulary) {
$vocabulary = $vocabularyStorage->create([
'vid' => $vid,
'name' => $vocabularyName,
'description' => $vocabularyName
])->save();
}
foreach ($termNames as $termName) {
$term = $termStorage->loadByProperties([
'vid' => $vid,
'name' => $termName,
]);
if (!$term) {
$term = $termStorage->create([
'vid' => $vid,
'name' => $termName,
])->save();
}
}
$viewNames = [
'oq_application_histories_for_oq_application',
'oq_applications',
'oq_items',
'oq_voters',
];
$dir = __DIR__ . '/config/optional';
$fileStorage = new FileStorage($dir);
$viewsStorage = \Drupal::entityTypeManager()->getStorage('view');
foreach ($viewNames as $viewName) {
if (!$viewsStorage->load($viewName)) {
try {
$data = $fileStorage->read('views.view.' . $viewName);
$view = $viewsStorage->create($data);
$view->save();
}
catch (\Exception $e) {
}
}
}
}
function openquestions_uninstall() {
$viewNames = [
'oq_application_histories_for_oq_application',
'oq_applications',
'oq_items',
'oq_voters',
];
$viewsStorage = \Drupal::entityTypeManager()->getStorage('view');
foreach ($viewNames as $viewName) {
$view = $viewsStorage->load($viewName);
if ($view) {
$view->delete();
}
}
$role = Role::load('oq_voter');
if ($role) {
$role->delete();
}
$vocabulary = Vocabulary::load('oq_application_disposition');
if ($vocabulary) {
$vocabulary->delete();
}
}
