This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
archivesspace.module
109 lines (98 loc) · 3.88 KB
/
archivesspace.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* @file
* Contains archivesspace.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function archivesspace_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the archivesspace module.
case 'help.page.archivesspace':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides integration between Drupal and ArchivesSpace') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function archivesspace_theme() {
return [
'archivesspace' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_install().
*
* Drupal complains about "Mismatched entity and/or field definitions" after
* installing title_length. We fix it with db_change_varchar_field.
* See https://www.drupal.org/project/title_length/issues/2950092
*/
function archivesspace_install(){
db_change_varchar_field('node', 'title', 8000);
}
/**
* Change length of a varchar entity field with data, safe with entity-updates.
*
* This updates the storage schema, the databasse schema, and the last
* installed schema.
*
* The entity schema must also be changed in code in the entities
* baseFieldDefinitions() or in an alter.
*
* @param string $entity_type_id
* The entity type.
* @param string $field_name
* The field name to change.
* @param int $field_length
* The new length of the field, must be larger than the previous value.
*/
function db_change_varchar_field($entity_type_id, $field_name, $field_length) {
// Ignore entity manager caches.
/** @var \Drupal\Core\Entity\EntityManager $entity_manager */
$entity_manager = \Drupal::service('entity.manager');
$entity_manager->useCaches(FALSE);
/** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */
$schema_repository = \Drupal::service('entity.last_installed_schema.repository');
/** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
$entity_field_manager = \Drupal::service('entity_field.manager');
$base_field_definitions = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
$schema_repository->setLastInstalledFieldStorageDefinition($base_field_definitions[$field_name]);
$field_storage_definitions = $schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
$field_storage_definitions[$field_name]['schema'] = $field_storage_definitions[$field_name]->getSchema();
$field_storage_definitions[$field_name]['schema']['columns']['value']['length'] = $field_length;
$schema_repository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);
$is_revisionable = $field_storage_definitions[$field_name]->isRevisionable();
// Update the storage schema.
$key_value = \Drupal::keyValue('entity.storage_schema.sql');
$key_name = $entity_type_id . '.field_schema_data.' . $field_name;
$storage_schema = $key_value->get($key_name);
$storage_schema[$entity_type_id . '_field_data']['fields'][$field_name]['length'] = $field_length;
if ($is_revisionable) {
$storage_schema[$entity_type_id . '_field_revision']['fields'][$field_name]['length'] = $field_length;
}
$key_value->set($key_name, $storage_schema);
// Change node title length (data table).
$schema = Drupal\Core\Database\Database::getConnection()->schema();
$schema->changeField($entity_type_id . '_field_data', $field_name, $field_name, [
'type' => 'varchar',
'length' => $field_length,
'binary' => FALSE,
'not null' => TRUE,
]);
// Change node title length (revisions table).
$schema = Drupal\Core\Database\Database::getConnection()->schema();
$schema->changeField($entity_type_id . '_field_revision', $field_name, $field_name, [
'type' => 'varchar',
'length' => $field_length,
'binary' => FALSE,
'not null' => TRUE,
]);
}