aggrid-8.x-1.x-dev/aggrid.install
aggrid.install
<?php
/**
* @file
* Installation file for aggrid module.
*/
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
// aggrid update hooks.
include_once __DIR__ . '/includes/aggrid.install.update.inc';
/**
* Implements hook_requirements().
*/
function aggrid_requirements($phase) {
$requirements = [];
if (($phase == 'runtime' || $phase == 'update')) {
// Verify the library is installed.
$aggrid_library_community = \Drupal::service('library.discovery')->getLibraryByName('aggrid', 'ag-grid-community');
$install_path_community = $aggrid_library_community['js'][0]['data'];
$aggrid_library_enterprise = \Drupal::service('library.discovery')->getLibraryByName('aggrid', 'ag-grid-enterprise');
$install_path_enterprise = $aggrid_library_enterprise['js'][0]['data'];
$aggrid_available = aggrid_library_local_check();
if ($aggrid_available) {$requirements['aggrid'] = [
'title' => t('aggrid'),
'severity' => REQUIREMENT_OK,
'value' => '',
'description' => t('ag-Grid library for aggrid module is successfully installed at %installpathcommunity and %installpathenterprise.', [
'%installpathcommunity' => $install_path_community,
'%installpathenterprise' => $install_path_enterprise,
]),
];
}
else {
$requirements['aggrid'] = [
'title' => t('aggrid'),
'severity' => REQUIREMENT_WARNING,
'value' => t('Not installed'),
'description' => t('ag-Grid Library for aggrid module has not been installed. Please run "drush aggrid:download" from the command line. Otherwise you must download each raw file from <a href=":url" target="_blank">https://github.com/ag-grid/ag-grid/raw/master/packages/</a> for community and enterprise min file inside (folder)/dist and install as %installpathcommunity and %installpathenterprise. **Make sure you clear the cache after installing**', [
':url' => 'https://github.com/ag-grid/ag-grid/raw/master/packages/',
'%installpathcommunity' => $install_path_community,
'%installpathenterprise' => $install_path_enterprise,
]),
];
}
}
return $requirements;
}
/**
* Adds the 'prevwarninglist' column to the schema of all 'aggrid' field storage
* configurations.
*/
function aggrid_update_9001() {
$message = 'Adding new "prevwarninglist" column to the aggrid field type schema as a native JSON field, using the module\'s defined schema mapping, and preserving existing columns.';
\Drupal::logger('aggrid')->notice($message);
$database = \Drupal::database();
$schema = $database->schema();
// Load all field storage configurations that are of the 'aggrid' field type.
$field_storages = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties(['type' => 'aggrid']);
// Check if any aggrid field storages exist.
if (empty($field_storages)) {
return 'No "aggrid" field storage configurations found. No update needed for prevwarninglist column.';
}
foreach ($field_storages as $field_storage) {
$field_name = $field_storage->getName();
$table_name = "node__" . $field_name;
$revision_table_name = "node_revision__" . $field_name;
// Define the new 'prevwarninglist' column schema.
$column_schema = [
'description' => 'JSON data for ag-Grid Previous Data Warnings',
'type' => 'text',
'not null' => FALSE,
'pgsql_type' => 'json',
'mysql_type' => 'json',
];
// --- STEP 1: Add the column to the actual database table ---
// Check if the column already exists to prevent errors on re-runs.
if (!$schema->fieldExists($table_name, $field_name . '_prevwarninglist')) {
try {
$schema->addField($table_name, $field_name . '_prevwarninglist', $column_schema);
\Drupal::logger('aggrid')->notice('Added prevwarninglist column to table @table.', ['@table' => $table_name]);
} catch (\Exception $e) {
\Drupal::logger('aggrid')->error('Failed to add prevwarninglist column to table @table: @error', [
'@table' => $table_name,
'@error' => $e->getMessage(),
]);
// If adding the column fails, we cannot proceed with updating its FieldStorageConfig.
continue;
}
} else {
\Drupal::logger('aggrid')->notice('Column prevwarninglist already exists in table @table. Skipping database column addition.', ['@table' => $table_name]);
}
// --- STEP 2: Add the column to the revision database table ---
// Check if the column already exists to prevent errors on re-runs.
if (!$schema->fieldExists($revision_table_name, $field_name . '_prevwarninglist')) {
try {
$schema->addField($revision_table_name, $field_name . '_prevwarninglist', $column_schema);
\Drupal::logger('aggrid')->notice('Added prevwarninglist column to table @table.', ['@table' => $revision_table_name]);
} catch (\Exception $e) {
\Drupal::logger('aggrid')->error('Failed to add prevwarninglist column to table @table: @error', [
'@table' => $revision_table_name,
'@error' => $e->getMessage(),
]);
// If adding the column fails, we cannot proceed with updating its FieldStorageConfig.
continue;
}
} else {
\Drupal::logger('aggrid')->notice('Column prevwarninglist already exists in table @table. Skipping database column addition.', ['@table' => $revision_table_name]);
}
// Save the field storage configuration. This will ensure Drupal's Field API
// recognizes the updated schema for this field.
$field_storage->save();
}
return $message;
}
