content_workflow_bynder-1.0.0/content_workflow_bynder.install
content_workflow_bynder.install
<?php
/**
* @file
* Install and uninstall script for Content Workflow module.
*/
use Drupal\Core\Database\Database;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\content_workflow_bynder\Entity\Mapping;
/**
* Implements hook_install().
*/
function content_workflow_bynder_install() {
if (\Drupal::entityTypeManager()->hasDefinition('taxonomy_term')) {
$entityFieldManager = \Drupal::service('entity_field.manager');
$definitions = $entityFieldManager->getFieldStorageDefinitions('taxonomy_term');
if (!isset($definitions['contentworkflowbynder_option_ids'])) {
FieldStorageConfig::create([
'field_name' => 'contentworkflowbynder_option_ids',
'entity_type' => 'taxonomy_term',
'type' => 'string',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'locked' => TRUE,
'persist_with_no_fields' => TRUE,
'settings' => [
'is_ascii' => FALSE,
'case_sensitive' => FALSE,
],
])->save();
}
}
# Migrate from legacy gathercontent config.
if (\Drupal::moduleHandler()->moduleExists('gathercontent')) {
$config_factory = \Drupal::configFactory();
$old_settings = $config_factory->get('gathercontent.settings');
$cwb_settings = $config_factory->getEditable('content_workflow_bynder.settings');
foreach ($old_settings->get() as $key => $value) {
if (str_starts_with($key, '_')) {
continue;
}
$new_name = str_replace('gathercontent_', '', $key);
$cwb_settings->set($new_name, $value);
}
$cwb_settings->save();
$old_import = $config_factory->get('gathercontent.import');
$cwb_import = $config_factory->getEditable('content_workflow_bynder.import');
foreach ($old_import->get() as $key => $value) {
if (str_starts_with($key, '_')) {
continue;
}
$cwb_import->set($key, $value);
}
$cwb_import->save();
$mapping_storage = \Drupal::entityTypeManager()
->getStorage('gathercontent_mapping');
/** @var \Drupal\gathercontent\Entity\Mapping[] $mappings */
$mappings = $mapping_storage->loadMultiple();
$migration_creator = \Drupal::service('content_workflow_bynder.migration_creator');
foreach ($mappings as $old_mapping) {
$values = [
'id' => $old_mapping->id(),
'status' => $old_mapping->status(),
'project_id' => $old_mapping->getGathercontentProjectId(),
'project' => $old_mapping->getGathercontentProject(),
'template_id' => $old_mapping->getGathercontentTemplateId(),
'template_name' => $old_mapping->getGathercontentTemplate(),
'entity_type' => $old_mapping->getMappedEntityType(),
'content_type' => $old_mapping->getContentType(),
'content_type_name' => $old_mapping->getContentTypeName(),
'updated_drupal' => $old_mapping->getUpdatedDrupal(),
'data' => $old_mapping->getData(),
'template' => $old_mapping->getTemplate(),
];
$cwb_mapping = Mapping::create($values);
// Remove the old migration definitions, so the migrated ones don't get deleted when uninstalling
// the old module.
$migration_storage = \Drupal::entityTypeManager()->getStorage('migration');
foreach ($old_mapping->getMigrations() as $old_migration_id) {
if ($old_migration = $migration_storage->load($old_migration_id)) {
$old_migration->delete();
$old_mapping->set('migration_definitions', []);
$old_mapping->save();
}
}
$migration_creator->setMapping($cwb_mapping)
->setMappingData(unserialize($cwb_mapping->getData()))
->createMigrationDefinition();
$cwb_mapping->save();
}
// Migrate tracked entities.
$query_old = \Drupal::database()
->select('gathercontent_entity_mapping', 'old')
->fields('old', ['entity_id', 'entity_type', 'gc_id', 'migration_id', 'langcode']);
$results_old = $query_old->execute()->fetchAll();
if (count($results_old) > 0) {
$query_cwb = \Drupal::database()->insert('content_workflow_bynder_entity_mapping')
->fields(['entity_id', 'entity_type', 'cwb_id', 'migration_id', 'langcode']);
foreach ($results_old as $entry) {
$query_cwb->values(array_values((array) $entry));
}
$query_cwb->execute();
}
}
}
/**
* Entity mapping schema.
*
* @return array
* Schema array.
*/
function _content_workflow_bynder_entity_mapping_spec() {
return [
'description' => 'Stores entity id with cwb_id for migrate rollback process.',
'fields' => [
'entity_id' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Primary Key: Entity ID.',
],
'entity_type' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => 'Entity type.',
],
'cwb_id' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => "Content Workflow (Bynder) import id.",
],
'migration_id' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Migration ID.',
],
'langcode' => [
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'description' => 'Language ISO2.',
],
],
'primary key' => ['entity_id', 'entity_type', 'langcode'],
'indexes' => [
'entity_type' => ['entity_type'],
'cwb_id' => ['cwb_id'],
'migration_id' => ['migration_id'],
],
];
}
/**
* Implements hook_schema().
*/
function content_workflow_bynder_schema() {
return [
'content_workflow_bynder_entity_mapping' => _content_workflow_bynder_entity_mapping_spec(),
];
}
