workflow_participants-8.x-2.x-dev/workflow_participants.install
workflow_participants.install
<?php
/**
* @file
* Update functions for the workflow_participants module.
*/
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
/**
* Enable the My workflow view.
*/
function workflow_participants_update_8201() {
/** @var \Drupal\Core\Config\ConfigInstaller $service */
$service = \Drupal::service('config.installer');
$install_path = \Drupal::service('extension.list.module')->getPath('workflow_participants') . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
$storage = new FileStorage($install_path, StorageInterface::DEFAULT_COLLECTION);
$service->installOptionalConfig($storage, ['module' => 'views']);
}
/**
* Remove unused entries in workflow_participants and accompanying tables.
*/
function workflow_participants_update_8202() {
$database = \Drupal::database();
$sub_query = $database->select('workflow_participants', 'wfp');
$sub_query->fields('wfp', ['moderated_entity__target_id']);
$sub_query->groupBy('wfp.moderated_entity__target_id');
$sub_query->having("COUNT(wfp.moderated_entity__target_id) > 1", []);
$query = $database->select('workflow_participants', 'wp');
$query->fields('wp', ['moderated_entity__target_id', 'id']);
$query->innerJoin($sub_query, 'sub', 'wp.moderated_entity__target_id = sub.moderated_entity__target_id');
$query->orderBy('wp.moderated_entity__target_id', 'ASC');
$query->orderBy('wp.id', 'ASC');
$results = $query->execute()->fetchAll();
$visited = [];
$delete_ids = [];
foreach ($results as $result) {
if (!isset($visited[$result->moderated_entity__target_id])) {
$visited[$result->moderated_entity__target_id] = TRUE;
continue;
}
$delete_ids[] = $result->id;
}
$tables = [
'workflow_participants' => 'id',
'workflow_participants__editors' => 'entity_id',
'workflow_participants__reviewers' => 'entity_id',
];
foreach ($tables as $table => $field) {
$delete = $database->delete($table);
if (!empty($delete_ids)) {
$delete->condition($field, $delete_ids, 'IN');
$delete->execute();
}
}
}
/**
* Add database unique key constraint to moderated entity field.
*
* Constraint is necessary since multiple entries are being added
* to the workflow_participants table for the same moderated entity.
*/
function workflow_participants_update_8203() {
// Update storage schema to reflect new schema.
$schema = \Drupal::keyValue('entity.storage_schema.sql')->get('workflow_participants.field_schema_data.moderated_entity');
$schema['workflow_participants']['fields']['moderated_entity__target_id']['not null'] = TRUE;
$schema['workflow_participants']['fields']['moderated_entity__target_type']['not null'] = TRUE;
$schema['workflow_participants']['unique keys']['workflow_participants_field__moderated_entity'][] = "moderated_entity__target_id";
$schema['workflow_participants']['unique keys']['workflow_participants_field__moderated_entity'][] = "moderated_entity__target_type";
\Drupal::keyValue('entity.storage_schema.sql')->set('workflow_participants.field_schema_data.moderated_entity', $schema);
$schema = \Drupal::database()->schema();
$fields = ['moderated_entity__target_id', 'moderated_entity__target_type'];
$schema->addUniqueKey('workflow_participants', 'workflow_participants_field__moderated_entity', $fields);
}
