142 lines
4.9 KiB
PHP
142 lines
4.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Contains install and update hooks.
|
|
*/
|
|
|
|
use Drupal\Core\Url;
|
|
use Drupal\Core\Config\InstallStorage;
|
|
use Drupal\Core\Config\FileStorage;
|
|
use Drupal\Core\Field\BaseFieldDefinition;
|
|
|
|
/**
|
|
* Implements hook_module_preinstall().
|
|
*/
|
|
function contact_storage_module_preinstall($module) {
|
|
if ($module !== 'contact_storage') {
|
|
return;
|
|
}
|
|
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
|
$original_contact_message = $entity_definition_update_manager->getEntityType('contact_message');
|
|
$original_contact_form = \Drupal::entityTypeManager()->getDefinition('contact_form');
|
|
$entity_type_contact_message = clone $original_contact_message;
|
|
$entity_definition_update_manager->uninstallEntityType($original_contact_message);
|
|
|
|
// Update the entity type definition and make it use the default SQL storage.
|
|
// @see contact_storage_entity_type_alter()
|
|
$entity_types = [
|
|
'contact_message' => $entity_type_contact_message,
|
|
'contact_form' => $original_contact_form,
|
|
];
|
|
contact_storage_entity_type_alter($entity_types);
|
|
$entity_definition_update_manager->installEntityType($entity_types['contact_message']);
|
|
}
|
|
|
|
/**
|
|
* Make sure the fields are added.
|
|
*/
|
|
function contact_storage_update_8001() {
|
|
_contact_storage_ensure_fields();
|
|
}
|
|
|
|
/**
|
|
* Ensure fields are added.
|
|
*/
|
|
function _contact_storage_ensure_fields() {
|
|
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
|
|
$field_manager = \Drupal::service('entity_field.manager');
|
|
|
|
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
|
foreach (['id', 'created', 'uid', 'ip_address'] as $field_name) {
|
|
$field_definition = $field_manager->getFieldStorageDefinitions('contact_message')[$field_name];
|
|
$entity_definition_update_manager->installFieldStorageDefinition($field_name, 'contact_message', 'contact_storage', $field_definition);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Save the bulk delete action to config.
|
|
*/
|
|
function contact_storage_update_8002() {
|
|
$entity_type_manager = \Drupal::entityTypeManager();
|
|
$module_handler = \Drupal::moduleHandler();
|
|
|
|
// Save the bulk delete action to config.
|
|
$config_install_path = $module_handler->getModule('contact_storage')->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
|
|
|
|
// Create action if it doesn't exist.
|
|
$action_storage = $entity_type_manager->getStorage('action');
|
|
$action = $action_storage->load('message_delete_action');
|
|
if (!$action) {
|
|
$storage = new FileStorage($config_install_path);
|
|
$entity_type_manager
|
|
->getStorage('action')
|
|
->create($storage->read('system.action.contact_message_delete_action'))
|
|
->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Defines fields for the user id and ip address, for the contact messages.
|
|
*/
|
|
function contact_storage_update_8003() {
|
|
$storage_definition = BaseFieldDefinition::create('entity_reference')
|
|
->setLabel(t('User ID'))
|
|
->setDescription(t('The user ID.'))
|
|
->setSetting('target_type', 'contact_form')
|
|
->setDefaultValueCallback('contact_storage_contact_message_default_uid');
|
|
\Drupal::entityDefinitionUpdateManager()
|
|
->installFieldStorageDefinition('uid', 'contact_message', 'contact_storage', $storage_definition);
|
|
|
|
$storage_definition = BaseFieldDefinition::create('string')
|
|
->setLabel(t('IP address'))
|
|
->setDescription(t('The IP address of the submitter.'))
|
|
->setDefaultValueCallback('contact_storage_contact_message_default_ip_address');
|
|
\Drupal::entityDefinitionUpdateManager()
|
|
->installFieldStorageDefinition('ip_address', 'contact_message', 'contact_storage', $storage_definition);
|
|
}
|
|
|
|
/**
|
|
* Updates the redirect paths to the core property redirect path.
|
|
*/
|
|
function contact_storage_update_8200() {
|
|
$config_factory = \Drupal::configFactory();
|
|
// Iterate on all text formats config entities.
|
|
foreach ($config_factory->listAll('contact.form.') as $name) {
|
|
if ($redirect_page = $config_factory->get($name)->get('third_party_settings.contact_storage.redirect_uri')) {
|
|
$config = $config_factory->getEditable($name);
|
|
$config->clear('third_party_settings.contact_storage.redirect_uri');
|
|
try {
|
|
$url = '/' . Url::fromUri($redirect_page)->getInternalPath();
|
|
}
|
|
catch (Exception $e) {
|
|
continue;
|
|
}
|
|
if (!$config->get('redirect')) {
|
|
$config->set('redirect', $url);
|
|
}
|
|
$config->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enables the options module as it is now a dependency.
|
|
*/
|
|
function contact_storage_update_8201() {
|
|
\Drupal::service('module_installer')->install(['options']);
|
|
}
|
|
|
|
/**
|
|
* Fix the last installed definition for the 'contact_message' entity type.
|
|
*/
|
|
function contact_storage_update_8202() {
|
|
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('contact_message');
|
|
$keys = $entity_type->getKeys();
|
|
if (empty($keys['langcode'])) {
|
|
$keys['langcode'] = 'langcode';
|
|
$entity_type->set('entity_keys', $keys);
|
|
\Drupal::entityDefinitionUpdateManager()->updateEntityType($entity_type);
|
|
}
|
|
}
|