contact_emails.install in Contact Emails 8
Contact emails database table.
File
contact_emails.installView source
<?php
/**
* @file
* Contact emails database table.
*/
use Drupal\contact_emails\Entity\ContactEmail;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Modify how the recipient type is stored.
*/
function contact_emails_update_8001(&$sandbox) {
$connection = \Drupal::database();
$schema = $connection
->schema();
if ($connection
->schema()
->tableExists('contact_message_email_settings')) {
// Add the new recipient type field.
$recipient_type = [
'type' => 'varchar',
'length' => 20,
'not null' => TRUE,
'default' => 'manual',
'description' => 'How to determine the submitter of the form.',
];
if (!$schema
->fieldExists('contact_message_email_settings', 'recipient_type')) {
$schema
->addField('contact_message_email_settings', 'recipient_type', $recipient_type);
}
// Add the new recipient field for when the recipient type is field.
$recipient_field = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The field to send to if recipient type is field.',
];
if (!$schema
->fieldExists('contact_message_email_settings', 'recipient_field')) {
$schema
->addField('contact_message_email_settings', 'recipient_field', $recipient_field);
}
// Get the form ids that are using the form submitter as the recipient.
$query = $connection
->select('contact_message_email_settings', 'c');
$query
->addField('c', 'id');
$query
->condition('c.recipient_is_submitter', '1');
$ids = $query
->execute()
->fetchCol();
// Update the database to store that in the recipient type field instead.
if ($ids) {
$query = $connection
->update('contact_message_email_settings');
$query
->fields([
'recipient_type' => 'submitter',
]);
$query
->condition('id', $ids, 'IN');
$query
->execute();
}
// Remove the recipient_is_submitter field.
if ($schema
->fieldExists('contact_message_email_settings', 'recipient_is_submitter')) {
$schema
->dropField('contact_message_email_settings', 'recipient_is_submitter');
}
}
}
/**
* Modify how the recipient type is stored.
*/
function contact_emails_update_8002(&$sandbox) {
$connection = \Drupal::database();
$schema = $connection
->schema();
if ($connection
->schema()
->tableExists('contact_message_email_settings')) {
// Add the new recipient field for when the recipient type is field.
$recipient_reference = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The field to send to if recipient type is reference.',
];
if (!$schema
->fieldExists('contact_message_email_settings', 'recipient_reference')) {
$schema
->addField('contact_message_email_settings', 'recipient_reference', $recipient_reference);
}
}
}
/**
* Modify how the recipient type is stored.
*/
function contact_emails_update_8003(&$sandbox) {
$connection = \Drupal::database();
$schema = $connection
->schema();
if ($connection
->schema()
->tableExists('contact_message_email_settings')) {
// Add the new recipient field for when the recipient type is field.
$reply_to_type = [
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'default' => 'default',
'description' => 'The type of reply-to.',
];
if (!$schema
->fieldExists('contact_message_email_settings', 'reply_to_type')) {
$schema
->addField('contact_message_email_settings', 'reply_to_type', $reply_to_type);
// Update the defaults for existing records.
$query = $connection
->update('contact_message_email_settings');
$query
->fields([
'reply_to_type' => 'default',
]);
$query
->execute();
}
// Reply to field.
$reply_to_field = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The field to set the reply-to as if reply-to type is field.',
];
if (!$schema
->fieldExists('contact_message_email_settings', 'reply_to_field')) {
$schema
->addField('contact_message_email_settings', 'reply_to_field', $reply_to_field);
}
// Reply to email.
$reply_to_email = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The email to set the reply-to as if reply-to type is email.',
];
if (!$schema
->fieldExists('contact_message_email_settings', 'reply_to_email')) {
$schema
->addField('contact_message_email_settings', 'reply_to_email', $reply_to_email);
}
}
}
/**
* Remove the empty malformed contact emails config if it exists.
*/
function contact_emails_update_8004(&$sandbox) {
$config = \Drupal::configFactory()
->getEditable('contact_emails.ContactEmailDelete');
if ($config) {
$config
->delete();
}
$config = \Drupal::configFactory()
->getEditable('contact_emails.ContactEmailSettings');
if ($config) {
$config
->delete();
}
}
/**
* Migrate the contact emails table to the contact emails entity table.
*/
function contact_emails_update_8005(&$sandbox) {
$connection = \Drupal::database();
// Ensure the new entity type is installed on update. It is installed
// automatically on a fresh install.
$entity_manager = \Drupal::entityTypeManager();
$update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_manager
->clearCachedDefinitions();
if ($definition = $entity_manager
->getDefinition('contact_email')) {
$update_manager
->installEntityType($definition);
}
if ($connection
->schema()
->tableExists('contact_message_email_settings')) {
// Initialize the batch update.
if (!isset($sandbox['progress'])) {
// This must be the first run. Initialize the sandbox.
$sandbox['progress'] = 0;
$sandbox['current_id'] = 0;
$sandbox['max'] = $connection
->select('contact_message_email_settings')
->countQuery()
->execute()
->fetchField();
}
if ($sandbox['max']) {
// Update in chunks of 10.
$records = $connection
->select('contact_message_email_settings', 'm')
->fields('m')
->condition('m.id', $sandbox['current_id'], '>')
->range(0, 10)
->orderBy('m.id', 'ASC')
->execute()
->fetchAll();
if ($records) {
foreach ($records as $record) {
// Create a new email.
$data = (array) $record;
unset($data['id']);
$data['status'] = (bool) (!$data['disabled']);
$data['message'] = isset($data['message']) ? $data['message'] : $data['body'];
unset($data['disabled']);
$contact_email = ContactEmail::create($data);
$contact_email
->save();
$sandbox['progress']++;
$sandbox['current_id'] = $record->id;
}
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}
else {
$sandbox['#finished'] = 1;
}
// Delete the database table.
$connection
->delete('contact_message_email_settings');
return t('All emails have been successfully updated to translatable entities.');
}
}
/**
* Add 'message' field to 'contact_email' entities and remove 'body'.
*
* ** IMPORTANT ** If you extended or used the body field with your own bespoke
* module, you must update your code appropriately to use "message" instead of
* "body". If you have been using this module as is, this update will apply
* safely without issue.
*/
function contact_emails_update_8006(&$sandbox) {
$updateManager = \Drupal::entityDefinitionUpdateManager();
$existing_message_storage_definition = $updateManager
->getFieldStorageDefinition('message', 'contact_email');
$existing_body_storage_definition = $updateManager
->getFieldStorageDefinition('body', 'contact_email');
// Initialize the batch update.
if (!isset($sandbox['progress'])) {
// Ensure we don't yet have a contact email message field.
if (!$existing_message_storage_definition) {
// Install the new field storage definition for message.
$storage_definition = BaseFieldDefinition::create('text_long')
->setLabel(t('Message'))
->setDescription(t('The email message body.'))
->setDefaultValue('')
->setTranslatable(TRUE)
->setSettings([
'max_length' => 9999,
'text_processing' => 0,
])
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => -5,
'settings' => [
'rows' => 4,
],
]);
$updateManager
->installFieldStorageDefinition('message', 'contact_email', 'contact_email', $storage_definition);
}
// This must be the first run. Initialize the sandbox.
$sandbox['progress'] = 0;
$sandbox['current_id'] = 0;
// Count the contact emails.
$sandbox['max'] = \Drupal::entityQuery('contact_email')
->count()
->execute();
}
if (!empty($sandbox['max'])) {
// Update in chunks of 10.
$contact_email_ids = \Drupal::entityQuery('contact_email')
->condition('email_id', $sandbox['current_id'], '>')
->range(0, 10)
->execute();
$connection = \Drupal::database();
$contact_emails = \Drupal::entityTypeManager()
->getStorage('contact_email')
->loadMultiple($contact_email_ids);
foreach ($contact_emails as $contact_email) {
$message = [];
// Move the body into the message. The body no longer exists in the
// definition so we must get it with a query.
if ($connection
->schema()
->fieldExists('contact_email_field_data', 'body')) {
$fields = $connection
->select('contact_email_field_data', 'c')
->fields('c', [
'body',
])
->condition('c.email_id', $contact_email
->id(), '=')
->execute()
->fetchObject();
if ($fields && !empty($fields->body)) {
// The format and value were previously stored in a serialized array
// in the single column.
$unserialised_body = unserialize($fields->body, [
'allowed_classes' => FALSE,
]);
if (isset($unserialised_body['format'], $unserialised_body['value'])) {
$message = [
'value' => $unserialised_body['value'],
'format' => $unserialised_body['format'],
];
}
}
}
// In case the user ran entity-updates prior to updb.
if (!isset($message['value']) && $connection
->schema()
->fieldExists('contact_email_field_data', 'message__value')) {
$fields = $connection
->select('contact_email_field_data', 'c')
->fields('c', [
'message__value',
'message__format',
])
->condition('c.email_id', $contact_email
->id(), '=')
->execute()
->fetchObject();
if ($fields && !empty($fields->message__value)) {
$unserialised_body = unserialize($fields->message__value, [
'allowed_classes' => FALSE,
]);
if (isset($unserialised_body['format']) && isset($unserialised_body['value'])) {
$message = [
'value' => $unserialised_body['value'],
'format' => $unserialised_body['format'],
];
}
}
}
// Save the new message format to the contact email.
if (isset($message['value'])) {
$contact_email
->set('message', $message);
$contact_email
->save();
}
$sandbox['progress']++;
$sandbox['current_id'] = $contact_email
->id();
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}
else {
$sandbox['#finished'] = 1;
}
if ($existing_body_storage_definition && $sandbox['#finished'] >= 1) {
$updateManager
->uninstallFieldStorageDefinition($existing_body_storage_definition);
}
return t('All emails have have the serialised body field converted to the message field for compatibility with rest API. ** If you extended or used the body field with your own bespoke module, you must update your code appropriately to use "message" instead of "body". If you have been using this module as is, this update will apply safely without issue. **');
}
Functions
Name | Description |
---|---|
contact_emails_update_8001 | Modify how the recipient type is stored. |
contact_emails_update_8002 | Modify how the recipient type is stored. |
contact_emails_update_8003 | Modify how the recipient type is stored. |
contact_emails_update_8004 | Remove the empty malformed contact emails config if it exists. |
contact_emails_update_8005 | Migrate the contact emails table to the contact emails entity table. |
contact_emails_update_8006 | Add 'message' field to 'contact_email' entities and remove 'body'. |