ercore-8.x-1.20/modules/ercore_publication/ercore_publication.install
modules/ercore_publication/ercore_publication.install
<?php
/**
* @file
* The ercore_publication.install file for updates.
*/
use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityDefinitionUpdateManager;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Switching from plain-text citation field to long-text.
*/
function ercore_publication_update_8001() {
// Update config before updating fields.
\Drupal::service('config.installer')->installDefaultConfig('module', 'ercore_publication');
// Get an array of all Publication node ids.
$nids = \Drupal::entityQuery('node')
->condition('type', 'ercore_publication')
->execute();
// Load all the publications.
$nodes = Node::loadMultiple($nids);
foreach ($nodes as $node) {
$old_value = $node->get('field_ercore_pb_citation')->getValue();
$node->set('field_ercore_pb_full_citation', $old_value);
$node->save();
}
}
/**
* Uninstall old citation field.
*/
function ercore_publication_update_8002() {
$configs = [
'field.field.node.ercore_publication.field_ercore_pb_citation',
'field.storage.node.ercore_publication.field_ercore_pb_citation',
'field.storage.node.field_ercore_pb_citation',
];
foreach ($configs as $config) {
\Drupal::configFactory()->getEditable($config)->delete();
}
}
/**
* Uninstall old citation field.
*/
function ercore_publication_update_8703() {
// $entity_update_manager = \Drupal::entityDefinitionUpdateManager();
// $entity_type = $entity_update_manager->getEntityType('field_ercore_pb_citation');
// $entity_update_manager->uninstallEntityType($entity_type);
FieldStorageConfig::loadByName('node', 'field_ercore_pb_citation')->delete();
// $entity_type_manager = \Drupal::entityTypeManager();
// $bundle_of = 'node';
// $storage = $entity_type_manager->getStorage($bundle_of);
// $bundle_definition = $entity_type_manager->getDefinition($bundle_of);
// // Sometimes the primary key isn't 'id'. e.g. 'eid' or 'item_id'.
// $id_key = $bundle_definition->getKey('eid');
// // If there is no data table defined then use the base table.
// $table_name = $storage->getDataTable() ?: $storage->getBaseTable();
// $database = \Drupal::database();
// $definition_manager = \Drupal::entityDefinitionUpdateManager();
// // // Store the existing values.
// // $status_values = $database->select($table_name)
// // ->fields($table_name, [$id_key, 'status_field'])
// // ->execute()
// // ->fetchAllKeyed();
// // Clear out the values.
// $database->update($table_name)
// ->fields(['field_ercore_pb_citation' => NULL])
// ->execute();
// // Uninstall the field.
// $field_storage_definition = $definition_manager->getFieldStorageDefinition('field_ercore_pb_citation', $bundle_of);
// $definition_manager->uninstallFieldStorageDefinition($field_storage_definition);
}