function google_analytics_counter_update_8009 in Google Analytics Counter 8.3
Issue #3025051: Google Analytics Counter field should be a number
File
- ./
google_analytics_counter.install, line 236 - Update, and uninstall functions for the Google Analytics Counter module.
Code
function google_analytics_counter_update_8009() {
$config_factory = \Drupal::configFactory();
// Get the content types.
$content_types = \Drupal::service('entity.manager')
->getStorage('node_type')
->loadMultiple();
$gac_types = [];
foreach ($content_types as $machine_name => $content_type) {
// Get the {gac_type_}.
$gac_type = $config_factory
->getEditable('google_analytics_counter.settings')
->get("general_settings.gac_type_{$machine_name}");
// Store content types that have the custom field in a lookup table.
if ($gac_type == 1) {
$gac_types[] = $machine_name;
}
}
// Uninstall the Google Analytics Counter field.
$bundles = [
'node',
];
$fields['field_google_analytics_counter'] = [
'entity_type' => 'node',
];
// Delete the field config.
foreach ($bundles as $bundle) {
foreach ($fields as $field_name => $config) {
$field = FieldConfig::loadByName($config['entity_type'], $bundle, $field_name);
if (!empty($field)) {
$field
->delete();
}
}
}
// Delete the field storage.
$field_storage = FieldStorageConfig::loadByName('node', 'field_google_analytics_counter');
if (isset($field_storage)) {
$field_storage
->delete();
}
// Add the field storage as an integer this time.
$config_path = 'modules/contrib/google_analytics_counter/config/optional';
$source = new FileStorage($config_path);
\Drupal::entityTypeManager()
->getStorage('field_storage_config')
->create($source
->read('field.storage.node.field_google_analytics_counter'))
->save();
// Finally, if the {gac_type_} was checked, create the field config, and check {gac_type_} again.
foreach ($content_types as $machine_name => $content_type) {
foreach ($gac_types as $key => $gac_type) {
if ($gac_type == $content_type
->id()) {
/* @var \Drupal\google_analytics_counter\GoogleAnalyticsCounterCustomFieldGeneratorInterface $custom_field */
$custom_field = \Drupal::service('google_analytics_counter.custom_field_generator');
$custom_field
->gacAddField($content_type);
$config_factory
->getEditable('google_analytics_counter.settings')
->set("general_settings.{$machine_name}", 1)
->save();
}
}
}
}