cforge-2.0.x-dev/cforge.install
cforge.install
<?php
/**
* @file
* Installation hooks for Cforge installation profile.
*/
use Drupal\mcapi\Entity\Storage\WalletStorage;
use Drupal\file\Entity\File;
use Drupal\contact\Entity\ContactForm;
use Drupal\aggregator\FeedStorageInterface;
use Drupal\user\UserInterface;
use Drupal\user\Entity\User;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
/**
* Implements hook_requirements().
*/
function cforge_requirements($phase) {
$requirements = [];
if ($phase == 'install') {
$path = Drupal\Core\Site\Settings::get('file_private_path');
if (empty($path)) {
$requirements['private_path'] = [
'title' => t('Private files directory'),
'description' => t('$file_private_path in settings.php has not been set'),
'severity' => REQUIREMENT_ERROR,
];
}
$robotsfile = DRUPAL_ROOT . '/robots.txt';
if (is_writable($robotsfile)) {
unlink($robotsfile);
}
elseif (file_exists($robotsfile)) {
$requirements['robots'] = [
'title' => 'Robots.txt',
'value' => 1, //otherwise causes problems in install.core.inc line 2296
'description' => t('@file must be deleted from the Drupal root directory; this is normally done by Composer.', ['@file' => $robotsfile]),
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function cforge_install() {
// Needed before it is automatically set at end of installation.
module_set_weight('cforge', 100);
$configFactory = \Drupal::configFactory();
// Disable the user pictures on nodes.
$configFactory->getEditable('system.theme.global')
->set('features.node_user_picture', FALSE)
->save(TRUE);
// Allow visitor account creation, but with administrative approval.
$configFactory->getEditable('user.settings')
->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
->save(TRUE);
// Clears the captcha invitation to configure.
\Drupal::messenger()->deleteByType(MessengerInterface::TYPE_STATUS);
// Currency setup.
$template = t(
'@payer paid @payee @worth for: @description @links',
[
'@payer' => '[transaction:payer]',
'@payee' => '[transaction:payee]',
'@worth' => '[transaction:worth]',
'@description' => '[transaction:transaction_description]',
'@links' => '[transaction:links]',
]
);
$configFactory->getEditable('mcapi.misc')
->set('sentence_template', $template)
->save();
// Dynamically set the cforge.settings:referral_src to user 1's wallet
$u1wallets = WalletStorage::walletsOf(User::load(1));
$configFactory->getEditable('cforge.settings')
->set('referral_src', reset($u1wallets))
->save();
$configFactory->getEditable('system.theme')
->set('default', 'sky_seldulac')
->save();
$configFactory->getEditable('aggregator.settings')
->set('items.expire', FeedStorageInterface::CLEAR_NEVER)
->save();
ContactForm::create(
[
'id'=> 'site_admin',
'label' => t('Site Administrator'),
'recipients' => [$configFactory->get('system.site')->get('mail')]
]
)->save();
$configFactory->getEditable('contact.settings')
->set('default_form', 'site_admin')
->save();
// Copy the file from the profile directory to where it will be used.
if (!file_exists('public://pictures')) {
\Drupal::service('file_system')->mkdir('public://pictures');
}
$copied = \Drupal::service('file_system')->copy(
\Drupal::service('extension.path.resolver')->getPath('profile', 'cforge') .'/assets/anonymous.png',
'public://pictures/anonymous.png',
FileSystemInterface::EXISTS_REPLACE
);
// Save the copied entity with a uuid.
if ($copied) {
$file = File::create(['uri' => 'public://pictures/anonymous.png', 'uid' => 1]);
$file->set('uuid', 'ba411b1e-9478-4f42-ab45-69b7857303ed')->save();
}
// For some reason the smallads autoviews aren't working on installation, so run it again.
foreach (['offer', 'want'] as $t) {
$smallad_type = \Drupal\smallads\Entity\SmalladType::load($t);
smallads_smallad_type_insert($smallad_type);
}
}
/**
* Import specific serialized entities exported from default_content module but
* without using the default_content module and:
* - in the right language
* - without HAL links
* - owned by user 1
* @param string $module
* @param string $key
* Name of a directory inside 'default_content' in the module.
*
* @note New sites can migrate content with ids, upgraded site content must not have ids, in case of clashing.
* @todo check this isn't running during installation.
*/
function cforge_import_content(string $module, string $key, bool $override= FALSE) {
$entity_type_manager = \Drupal::entityTypeManager();
// Serialization module (core) is already depended on by csv_serialization
$serializer = \Drupal::service('serializer');
$user1 = \Drupal\user\Entity\User::load(1);
$path = \Drupal::service('extension.path.resolver')
->getPath('module', $module) .'/default_content/'. $key;
$langcode = \Drupal::service('language_manager')->getDefaultLanguage()->getId();
if (is_dir("$path/$langcode")) {
$default_content_path = "$path/$langcode";
}
else {
$default_content_path = "$path/und";
}
foreach (glob($default_content_path.'/*', GLOB_ONLYDIR) as $dir) {
$entity_type_id = basename($dir, 1);
$entity_storage = $entity_type_manager->getStorage($entity_type_id);
$files = glob($dir.'/*.json');
\Drupal::logger('cforge')->notice(
"Importing default content from @path: <br />@files",
['@path' => $default_content_path .'/'.$dir, '@files' => implode('<br />', $files)]
);
foreach ($files as $filename) {
$contents = file_get_contents($filename);
$class = $entity_type_manager->getDefinition($entity_type_id)->getClass();
$new_ent = $serializer->deserialize($contents, $class, 'json');
if (
($new_ent->id() and $existing = $entity_storage->load($new_ent->id()))
or
($new_ent->uuid() and $entity_storage->loadByproperties(['uuid' => $new_ent->uuid()]))
) {
if (!$override) {
continue;
}
$existing->delete();
}
if ($new_ent instanceof EntityOwnerInterface) {
$new_ent->setOwner($user1);
}
$new_ent->save();
}
}
}
/**
* Update the offers_wants_lists view
*/
function cforge_update_9001() {
foreach (['offers_wants_lists', 'smallads_auto_page'] as $v) {
if ($view = \Drupal\views\Entity\View::load('offers_wants_lists')) {
$view->delete();
}
}
$mod_path = \Drupal::service('extension.path.resolver')->getPath('module', 'cforge').'/config/install';
$source = new FileStorage($mod_path);
// Copy the new smallad_auto views from file to config.
Drupal::service('config.storage')->write(
'views.view.smallads_auto_page',
$source->read('views.view.smallads_auto_page')
);
// Update the views as if the smallad types were just added
foreach (\Drupal\smallads\Entity\SmalladType::loadMultiple() as $type) {
smallads_smallad_type_insert($type);
}
}
/**
* Reset userdata privacy storage setting so that default is zero and change name to show
*/
function cforge_update_9002() {
\Drupal::database()->delete('users_data')
->condition('name', ['phones', 'address'], 'IN')
->execute();
}
/**
* Migrate from the phones field to the new contactme field
*/
function cforge_update_9003() {
$source = new FileStorage('profiles/cforge/config/install');
\Drupal::entityTypeManager()->getStorage('field_storage_config')
->create($source->read('field.storage.user.contactme'))
->save();
\Drupal::entityTypeManager()->getStorage('field_config')
->create($source->read('field.field.user.user.contactme'))
->save();
$result = \Drupal::database()->select('user__phones', 'p')
->fields('p', ['entity_id', 'langcode', 'delta', 'phones_value'])
->execute();
$users = [];
foreach ($result as $phone) {
$users[$phone->entity_id][$phone->delta] = $phone->phones_value;
}
if ($users) {
$query = \Drupal::database()->insert('user__contactme')->fields([
'bundle', 'entity_id', 'revision_id', 'langcode', 'delta', 'contactme_mob', 'contactme_tel'
]);
foreach ($users as $uid => $phones) {
$vals = ['user', $uid, $uid, $phone->langcode, 0, substr($phones[0], 0, 32), substr(@$phones[1], 0, 32)];
$query->values($vals);
}
$query->execute();
}
// Now remove the user phones field.
if ($field = FieldConfig::loadByName('user', 'user', 'phones')) {
$field->delete();
}
if ($fs = FieldStorageConfig::loadByName('user', 'phones')) {
$fs->delete();
}
EntityFormDisplay::load('user.user.default')
->setComponent('contactme', ['weight' => 14])
->save();
Drupal\Core\Entity\Entity\EntityViewDisplay::load('user.user.default')
->setComponent('contactme', ['weight' => 11])
->save();
}
/** Update the transaction categories widget to shs */
function cforge_update_9004() {
EntityFormDisplay::load('mcapi_transaction.mcapi_transaction.bill')
->setComponent('category', ['weight' => 5, 'type' => 'options_shs', 'region' => 'content'])
->save();
EntityFormDisplay::load('mcapi_transaction.mcapi_transaction.credit')
->setComponent('category', ['weight' => 5, 'type' => 'options_shs', 'region' => 'content'])
->save();
}
