function media_acquiadam_update_8102 in Media: Acquia DAM 8
Consolidate Acquia DAM bundle source fields to a single field.
File
- ./
media_acquiadam.install, line 79 - Installation related hooks and functions.
Code
function media_acquiadam_update_8102(&$sandbox) {
$default_field_name = 'field_acquiadam_asset_id';
$destination_table = 'media__' . $default_field_name;
$connection = Drupal::database();
$db_schema = $connection
->schema();
$entity_type_manager = Drupal::service('entity_type.manager');
// Create asset id field if it doesn't exist.
if (!$db_schema
->tableExists($destination_table)) {
$entity_type_manager
->getStorage('field_storage_config')
->create([
'entity_type' => 'media',
'field_name' => $default_field_name,
'type' => 'integer',
])
->save();
}
// Get asset id fields and reduce to those that don't match the default.
$asset_id_fields = media_acquiadam_get_bundle_asset_id_fields();
$fields_to_migrate = array_diff($asset_id_fields, [
'field_acquiadam_asset_id',
]);
// Loop through existing bundles/fields and transfer field table data
// as well as set the source_field for media types.
foreach ($fields_to_migrate as $bundle => $field) {
// Select all fields from origin.
$origin_table = 'media__' . $field;
$origin_query = $connection
->select($origin_table, 'o')
->fields('o');
// Insert origin fields into destination.
$connection
->insert($destination_table)
->from($origin_query)
->execute();
// The bundle config.
$config = Drupal::service('config.factory')
->getEditable('media.type.' . $bundle);
// Set the new source_field.
$config
->set('source_configuration', [
'source_field' => $default_field_name,
]);
// Map the asset id to the old field to avoid potential regressions.
$config
->set('field_map.id', $field);
$config
->save();
// Add field to bundle.
$label = 'Asset ID';
$field_storage = FieldStorageConfig::loadByName('media', $default_field_name);
$field = FieldConfig::loadByName('media', $bundle, $default_field_name);
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $bundle,
'label' => $label,
]);
$field
->save();
// Assign widget settings for the 'default' form mode.
media_acquiadam_entity_get_form_display('media', $bundle, 'default')
->setComponent($default_field_name, [
'type' => 'number',
])
->save();
// Hide the id field in available view modes.
$view_modes = Drupal::service('entity_display.repository')
->getViewModes('media');
foreach ($view_modes as $view_mode => $view_mode_config) {
media_acquiadam_entity_get_display('media', $bundle, $view_mode)
->removeComponent($default_field_name)
->save();
}
}
}
}