cloudwords-8.x-1.x-dev/cloudwords.install
cloudwords.install
<?php
/**
* @file
* Schema definitions for cloudwords.module.
*/
use Drupal\Core\Field\BaseFieldDefinition;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_install().
*
* Set the supported languages.
*/
function cloudwords_install() {
drupal_static_reset('language_list');
require_once dirname(__FILE__) . '/cloudwords.languages.inc';
$langcodes = array_intersect(array_keys(\Drupal::languageManager()->getlanguages()), _cloudwords_map_cloudwords_drupal());
\Drupal::configFactory()->getEditable('cloudwords.settings')->set('cloudwords_lang_codes', $langcodes)->save();
}
/**
* Implements hook_requirements().
*/
function cloudwords_requirements($phase) {
$requirements = [];
if ($phase == 'install') {
$t = 't';
// Check for the cURL library.
if (extension_loaded('curl')) {
$info = curl_version();
$requirements['cloudwords_curl'] = [
'value' => $info['version'],
'severity' => REQUIREMENT_OK,
];
}
else {
$requirements['cloudwords_curl'] = [
'severity' => REQUIREMENT_ERROR,
'description' => $t('The cURL library for PHP is missing. Check the <a href="@url">PHP cURL documentation</a> for information on how to correct this.', ['@url' => 'http://www.php.net/manual/en/book.curl.php']),
];
}
$requirements['cloudwords_curl']['title'] = $t('cURL library support');
}
return $requirements;
}
/**
* Implements hook_uninstall().
*/
function cloudwords_uninstall() {
//@TODO fix uninstall process
/*
\Drupal::config('cloudwords.settings')->clear('cloudwords_lang_codes')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_client_mode')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_auth_token')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_upload_file_size')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_base_api_url_production')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_base_url_stage')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_last_cron_run')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_node_view_access_bypass')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_auto_import_translation_enabled')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_auto_import_translation_frequency')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_auto_import_translation_max_process_items')->save();
\Drupal::config('cloudwords.settings')->clear('cloudwords_preview_bundle_enabled')->save();
*/
}
/**
* Implements hook_schema().
*/
function cloudwords_schema() {
$schema = [];
$schema['cloudwords_project_language'] = [
'description' => 'Manages information about Cloudwords projects.',
'fields' => [
'pid' => [
'description' => 'The project id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'language' => [
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
'description' => 'The language to translate to.',
],
'status' => [
'description' => 'The import status of a language for a project.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 1,
],
],
// 'primary key' => array('module', 'objectid', 'language'),
'primary key' => ['pid', 'language'],
];
$schema['cloudwords_content'] = [
'description' => 'Manages information about Cloudwords projects.',
'fields' => [
'pid' => [
'description' => 'The project id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'ctid' => [
'description' => 'The queue item.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'status' => [
'description' => 'The status of the imported item.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['pid', 'ctid'],
];
$schema['cloudwords_project_user'] = [
'description' => "Stores the mapping for a user's projects.",
'fields' => [
'uid' => [
'description' => 'The user id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'pid' => [
'description' => 'The project id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
],
'primary key' => ['uid', 'pid'],
];
return $schema;
}
/**
* Add 'status' field to 'cloudwords_translatable' entities.
*/
function cloudwords_update_8001() {
$storage_definition = BaseFieldDefinition::create('integer')
->setLabel(t('status'))
->setDescription(t('The status of a queue item'))
->setDefaultValue(0)
->setSettings([
'max_length' => 2,
]);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('status', 'cloudwords_translatable', 'cloudwords', $storage_definition);
}
/**
* Add 'translation_module' field to 'cloudwords_translatable' entities to differentiate between type of translation
*/
function cloudwords_update_8002(){
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Translation module'))
->setDescription(t('The translation module used to translate items'))
->setSettings([
'max_length' => 25,
]);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('translation_module', 'cloudwords_translatable', 'cloudwords', $storage_definition);
$updated_count = db_update('cloudwords_translatable')
->fields([
'translation_module' => CLOUDWORDS_CONTENT_TRANSLATION_TYPE,
])
->execute();
}
/**
* Adds config object id property to support config ids
*/
function cloudwords_update_8003(){
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Config Object ID'))
->setDescription(t(''))
->setSettings([
'max_length' => 128,
'text_processing' => 0,
]);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('config_objectid', 'cloudwords_translatable', 'cloudwords', $storage_definition);
}
/**
* Updates cloudwords_translatables views with updated view install config
*/
function cloudwords_update_8004(){
$config_path = drupal_get_path('module', 'cloudwords') . '/config/install/views.view.cloudwords_translatable.yml';
$data = Yaml::parse($config_path);
if(is_array($data)){
\Drupal::configFactory()->getEditable('views.view.cloudwords_translatable')->setData($data)->save(TRUE);
}
}
/**
* Updates cloudwords_translatable entity type name property to be 255 which is the same length of node entity titles
*/
function cloudwords_update_8005() {
$database = \Drupal::database();
// Retrieve existing field data in cloudwords_translatable table.
$tables = [
'cloudwords_translatable',
];
$existingDdata = [];
foreach ($tables as $table) {
// Retrieve any available data and temporarily store in array $existingDdata.
$existingDdata[$table] = $database->select($table)
->fields($table)
->execute()
->fetchAll(PDO::FETCH_ASSOC);
// Delete any table data.
$database->truncate($table)->execute();
}
try {
$entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
$field_definition = $entity_definition_update_manager->getFieldStorageDefinition('name', 'cloudwords_translatable');
// $field_definition->setSetting('max_length', 255);
// $entity_definition_update_manager->updateFieldStorageDefinition($field_definition);
$entity_definition_update_manager->applyUpdates();
\Drupal::logger('Cloudwords: ')->notice('Successfully applied updates');
}
catch (\Drupal\Core\Entity\EntityStorageException $e) {
\Drupal::logger('Cloudwords: ')->notice($e->getMessage());
}
// Restore the data.
foreach ($tables as $table) {
if (!empty($existingDdata[$table])) {
$insert_query = $database
->insert($table)
->fields(array_keys(end($existingDdata[$table])));
foreach ($existingDdata[$table] as $row) {
$insert_query->values(array_values($row));
}
$insert_query->execute();
}
}
}