ww_publish-8.x-1.x-dev/ww_publish.install
ww_publish.install
<?php
/**
* @file
* Installation hooks for WW Publish module.
*/
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\media\Entity\MediaType;
use Drupal\node\Entity\NodeType;
use Drupal\ww_publish\Entity\SnsMessageEntity;
use Drupal\ww_publish\Entity\SnsMessageEntityInterface;
/**
* Delete the old entity type and install it again with the new definition.
*/
function ww_publish_update_8001() {
$entity_update_manager = \Drupal::entityDefinitionUpdateManager();
// Uninstall the currently install config entity type if installed.
$entity_type = $entity_update_manager->getEntityType('ww_publish_sns_message');
if ($entity_type) {
$entity_update_manager->uninstallEntityType($entity_type);
}
// Install the new content entity type.
$entity_type = \Drupal::entityTypeManager()->getDefinition('ww_publish_sns_message');
$entity_update_manager->installEntityType($entity_type);
// Convert the current configuration files into the new content entity and
// then delete them.
$snsMessagesNames = \Drupal::configFactory()->listAll('ww_publish.ww_publish_sns_message.');
foreach ($snsMessagesNames as $snsMessagesName) {
$config = \Drupal::configFactory()->getEditable($snsMessagesName);
$values = $config->get();
// Unset properties that do not exist as fields.
unset($values['uuid'], $values['langcode'], $values['status'], $values['dependencies']);
// Delete the migrated configuration.
$config->delete();
$message = SnsMessageEntity::create($values);
$message->save();
}
}
/**
* Setting the default value to default text format.
*/
function ww_publish_update_8002() {
\Drupal::configFactory()->getEditable('ww_publish.settings')->set('default_text_format', 'full_html')->save();
}
/**
* Set the new id field third party setting on the image media type.
*/
function ww_publish_update_8003() {
if (\Drupal::moduleHandler()->moduleExists('media')) {
$image_type = MediaType::load('image');
if ($image_type) {
foreach (NodeType::loadMultiple() as $node_type) {
if ($article_id_field = $node_type->getThirdPartySetting('ww_publish', 'article_id_field')) {
$image_type->setThirdPartySetting('ww_publish', 'id_field', $article_id_field);
$image_type->save();
break;
}
}
}
}
}
/**
* Update the Topic ARN setting.
*/
function ww_publish_update_8004() {
$settings = \Drupal::configFactory()->getEditable('ww_publish.settings');
if ($topic_arn = $settings->get('topic_arn')) {
$settings->set('topic_arn', [$topic_arn]);
}
else {
$settings->set('topic_arn', []);
}
$settings->save();
}
/**
* Convert the executed field to the new status field.
*/
function ww_publish_update_8005() {
$status = BaseFieldDefinition::create('list_integer')
->setLabel(t('Status'))
->setDefaultValue(0)
->setSetting('unsigned', TRUE)
->setSetting('allowed_values', [
0 => t('New'),
1 => t('Imported'),
2 => t('Failed'),
])
->setInitialValue(SnsMessageEntityInterface::IMPORTED);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('status', 'ww_publish_sns_message', 'ww_publish', $status);
// It is not possible to use the executed field as initial value as the
// field types do not match, assume that most messages are already executed
// and manually update those that are not.
if (\Drupal::database()->schema()->tableExists('ww_publish_sns_message')) {
\Drupal::database()->update('ww_publish_sns_message')
->fields([
'status' => SnsMessageEntityInterface::NEW,
])
->condition('executed', 0)
->execute();
}
$executed = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('executed', 'ww_publish_sns_message');
if ($executed) {
\Drupal::entityDefinitionUpdateManager()->uninstallFieldStorageDefinition($executed);
}
}
