skimlinks-8.x-1.x-dev/skimlinks.install
skimlinks.install
<?php
/**
* @file
* Install, requirements, and uninstall functionsfor the Skimlinks
* module.
*/
use Drupal\Core\Url;
use Drupal\Core\Database\Database;
/**
* Implements hook_install().
*
* Prompt user to configure the module on install.
*/
function skimlinks_install() {
$config_link = \Drupal::l(t('Please configure the Skimlinks module.'), \Drupal\Core\Url::fromRoute('skimlinks.admin_settings_form'));
drupal_set_message($config_link);
$schema = Database::getConnection()->schema();
foreach (_skimlinks_8001_tables() as $name => $table) {
$schema->createTable($name, $table);
}
}
/**
* Implements hook_requirements().
*/
function skimlinks_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
$config = \Drupal::config('skimlinks.settings');
// Raise warning if SiteID has not been set yet.
if (empty($config->get('domainid'))) {
$requirements['skimlinks_domainid'] = [
'title' => t('Skimlinks module'),
'description' => t('Skimlinks module has not been configured yet. Please configure its settings from the <a href=":url">Skimlinks settings page</a>.', [':url' => Url::fromRoute('skimlinks.admin_settings_form')->toString()]),
'severity' => REQUIREMENT_WARNING,
'value' => t('Not configured'),
];
}
}
return $requirements;
}
/**
* Create Skimlinks tables.
*/
function skimlinks_update_8001() {
$schema = Database::getConnection()->schema();
if (!$schema->tableExists('skimlinks_domains')) {
foreach (_skimlinks_8001_tables() as $name => $table) {
$schema->createTable($name, $table);
}
}
}
/**
* Implements hook_uninstall().
*
* Remove script when module is uninstalled.
*/
function skimlinks_uninstall() {
// Remove any filter_skimlinks plugin data before uninstall to prevent
// PluginNotFoundException
$query = \Drupal::database()->query("SELECT * FROM {config} WHERE name LIKE 'filter.format%'");
$results = $query->fetchAll();
foreach ($results as $result) {
$data = unserialize($result->data);
// Unset filter
if (isset($data['filters']['filter_skimlinks'])) {
unset($data['filters']['filter_skimlinks']);
}
// Unset dependencies
if (isset($data['dependencies']['module'])) {
unset($data['dependencies']['module'][array_search('skimlinks', $data['dependencies']['module'])]);
}
if (empty($data['dependencies']['module'])) {
unset($data['dependencies']['module']);
}
$queryUpdate = \Drupal::database()->update('config')
->fields(array(
'data' => serialize($data),
))
->condition('name', $result->name)
->execute();
}
// Flush all caches to complete the process
drupal_flush_all_caches();
$schema = Database::getConnection()->schema();
foreach (_skimlinks_8001_tables() as $name => $table) {
$schema->dropTable($name);
}
}
function _skimlinks_8001_tables() {
$max_key_length = _skimlinks_max_domain_length();
return [
'skimlinks_blacklist' => [
'description' => 'Stores blacklisted domains.',
'fields' => [
'domain' => [
'type' => 'varchar',
'length' => $max_key_length,
'not null' => TRUE,
'description' => 'The domain name',
],
],
'primary key' => ['domain'],
],
'skimlinks_domains' => [
'description' => 'Stores known domains.',
'fields' => [
'domain' => [
'type' => 'varchar',
'length' => $max_key_length,
'not null' => TRUE,
'description' => 'The domain name',
],
'last_updated' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp for last update',
],
'valid' => [
'type' => 'int',
'default' => 0,
'size' => 'tiny',
'description' => 'The domain exists on the Skimlinks Merchant API',
],
],
'primary key' => ['domain'],
'indexes' => [
'domain_valid' => ['domain', 'valid'],
],
],
];
}
