forked from summitmedia/viewfield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewfield.install
78 lines (72 loc) · 2.57 KB
/
viewfield.install
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
<?php
/**
* @file
* Installation functions for Viewfield module.
*/
use Drupal\Core\Database\Database;
/**
* Implements hook_field_schema().
*/
function viewfield_field_schema($field) {
return array(
'columns' => array(
'vname' => array(
'type' => 'varchar',
'not null' => FALSE,
// Views requires at least 96 chars for the view name and display, plus
// we need 1 for our delimiter. Round up to a common value of 128.
'length' => 128,
),
'vargs' => array(
'type' => 'varchar',
'not null' => FALSE,
'length' => 255,
),
'settings' => array(
'type' => 'text',
'size' => 'normal',
),
),
);
}
/**
* Add column to fields with viewfield type.
*/
function viewfield_update_8001() {
$entity_type_manager = \Drupal::entityTypeManager();
$field_storage = $entity_type_manager->getStorage('field_storage_config');
$entity_field_manager = \Drupal::service('entity_field.manager');
if ($field_storage_configs = $field_storage->loadByProperties(array('type' => 'viewfield'))) {
foreach ($field_storage_configs as $field) {
$field_name = $field->getName();
$entity_type_id = $field->getTargetEntityTypeId();
$field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
$entity_storage = $entity_type_manager->getStorage($entity_type_id);
/** @var Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_storage->getTableMapping($field_storage_definitions);
// Table name.
$table = $table_mapping->getFieldTableName($field_name);
// See if the field has a revision table.
$entity_type = $entity_type_manager->getDefinition($entity_type_id);
$revision_table = NULL;
if ($entity_type->isRevisionable() && $field->isRevisionable()) {
if ($table_mapping->requiresDedicatedTableStorage($field)) {
$revision_table = $table_mapping->getDedicatedRevisionTableName($field);
}
elseif ($table_mapping->allowsSharedTableStorage($field)) {
$revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
}
}
$spec = array(
'type' => 'text',
'size' => 'normal',
'description' => 'View settings',
);
$schema = Database::getConnection()->schema();
$schema->addField($table, $field_name . '_settings', $spec);
if ($revision_table) {
$schema->addField($revision_table, $field_name . '_settings', $spec);
}
}
}
}