social_profile_fields.install in Open Social 10.2.x
Same filename and directory in other branches
- 8.9 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 8.2 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 8.3 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 8.4 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 8.5 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 8.6 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 8.7 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 8.8 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 10.3.x modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 10.0.x modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
- 10.1.x modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.install
The social profile fields install file.
File
modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.installView source
<?php
/**
* @file
* The social profile fields install file.
*/
use Drupal\search_api\Entity\Index;
use Drupal\user\Entity\User;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_install().
*/
function social_profile_fields_install() {
// Set some default permissions.
// Clear the entity field manager cached field definitions as the address
// field overrides settings need to be applied.
_social_profile_fields_set_permissions();
_social_profile_fields_update_search_index();
_social_profile_fields_update_search_index('social_all');
_social_profile_fields_nationalities();
}
/**
* Disable and enable the search index, so the nickname field is added.
*/
function social_profile_fields_update_8001() {
_social_profile_fields_update_search_index();
}
/**
* Disable and enable the main search index, so the nickname field is added.
*/
function social_profile_fields_update_8002() {
_social_profile_fields_update_search_index('social_all');
}
/**
* Function to set permissions.
*/
function _social_profile_fields_set_permissions() {
user_role_grant_permissions('sitemanager', [
'social profile fields change used profile fields',
]);
}
/**
* Disable and enable the search index, so the nickname field is added.
*
* @param string $index_id
* The search index ID.
*/
function _social_profile_fields_update_search_index($index_id = 'social_users') {
/** @var \Drupal\search_api\IndexInterface $index */
$index = Index::load($index_id);
$logger = \Drupal::logger('social_profile_fields');
$logger
->info('Loaded search index');
// If currently enabled we will first disabled and enable the index.
if ($index !== NULL && $index
->status()) {
$logger
->info('Search index exists');
// Elevate permissions so we can index *all* the items.
$accountSwitcher = \Drupal::service('account_switcher');
$account = User::load(1);
$accountSwitcher
->switchTo($account);
// Disable and enable the index so the tagging field is properly added.
$index
->disable()
->save();
$logger
->info('Search index disabled');
$index
->enable()
->save();
$logger
->info('Search index enabled');
// Restore user account.
$accountSwitcher
->switchBack();
}
}
/**
* Add a field for nationality.
*/
function social_profile_fields_update_8003() {
$entity_type_ids = [
'taxonomy.vocabulary.' => 'taxonomy_vocabulary',
'field.storage.profile.field_profile_' => 'field_storage_config',
'field.field.profile.profile.field_profile_' => 'field_config',
];
$path = drupal_get_path('module', 'social_profile_fields') . '/config/static/';
foreach ($entity_type_ids as $prefix => $entity_type_id) {
$config_file = $path . $prefix . 'nationality_8003.yml';
$settings = Yaml::parseFile($config_file);
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
$storage
->createFromStorageRecord($settings)
->save();
}
_social_profile_fields_nationalities();
}
/**
* Create taxonomy terms for nationalities in the "Nationality" vocabulary.
*/
function _social_profile_fields_nationalities() {
$path = drupal_get_path('module', 'social_profile_fields') . '/content/';
$data = Yaml::parseFile($path . 'nationalities.yml');
$storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
foreach ($data['nationalities'] as $nationality) {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $storage
->create([
'vid' => 'nationality',
]);
$term
->setName($nationality)
->save();
}
}
Functions
Name![]() |
Description |
---|---|
social_profile_fields_install | Implements hook_install(). |
social_profile_fields_update_8001 | Disable and enable the search index, so the nickname field is added. |
social_profile_fields_update_8002 | Disable and enable the main search index, so the nickname field is added. |
social_profile_fields_update_8003 | Add a field for nationality. |
_social_profile_fields_nationalities | Create taxonomy terms for nationalities in the "Nationality" vocabulary. |
_social_profile_fields_set_permissions | Function to set permissions. |
_social_profile_fields_update_search_index | Disable and enable the search index, so the nickname field is added. |