forked from lisastreeter/commerce_fee
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommerce_fee.install
More file actions
99 lines (88 loc) · 3.35 KB
/
commerce_fee.install
File metadata and controls
99 lines (88 loc) · 3.35 KB
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
<?php
use Drupal\commerce_fee\Entity\Fee;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\Entity\Role;
function commerce_fee_update_8202() {
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$storage_definition = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('The weight of this fee in relation to others.'))
->setDefaultValue(0)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'integer',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'number',
'weight' => 4,
]);
$entity_definition_update->installFieldStorageDefinition('weight', 'commerce_fee', 'commerce_fee', $storage_definition);
}
/**
* Add created/changed timestamp fields to fees.
*/
function commerce_fee_update_8208() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$created = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setTranslatable(TRUE)
->setDescription(t('The time when the fee was created.'));
$definition_update_manager->installFieldStorageDefinition('created', 'commerce_fee', 'commerce_fee', $created);
$changed = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setTranslatable(TRUE)
->setDescription(t('The time when the fee was last edited.'))
->setDisplayConfigurable('view', TRUE);
$definition_update_manager->installFieldStorageDefinition('changed', 'commerce_fee', 'commerce_fee', $changed);
}
/**
* Add a 'uid' field to fees.
*/
function commerce_fee_update_8209() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Set the owner of the fees to be first user which which has
// the 'administrator' role. This way we avoid hard coding user ID 1 for sites
// that prefer to not give it any special meaning.
$admin_roles = \Drupal::entityTypeManager()
->getStorage('user_role')
->getQuery()
->condition('is_admin', TRUE)
->accessCheck(FALSE)
->execute();
if (!empty($admin_roles)) {
$query = \Drupal::entityTypeManager()
->getStorage('user')
->getQuery()
->condition('roles', $admin_roles, 'IN')
->condition('status', 1)
->sort('uid', 'ASC')
->accessCheck(FALSE)
->range(0, 1);
$result = $query
->execute();
}
// Defaults to user ID 1 if we could not find any other administrator users.
$owner_id = !empty($result) ? reset($result) : 1;
$entity_type = $definition_update_manager->getEntityType('commerce_fee');
$keys = $entity_type->getKeys();
$keys['owner'] = 'uid';
$entity_type->set('entity_keys', $keys);
$definition_update_manager->updateEntityType($entity_type);
$storage_definition = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Owner'))
->setDescription(t('The fee owner.'))
->setSetting('target_type', 'user')
->setTranslatable(TRUE)
->setInitialValue($owner_id)
->setDefaultValueCallback(Fee::class . '::getDefaultEntityOwner');
$definition_update_manager->installFieldStorageDefinition('uid', 'commerce_fee', 'commerce_fee', $storage_definition);
/** @var \Drupal\user\RoleInterface $role */
foreach (Role::loadMultiple() as $role) {
if (!$role->hasPermission('update commerce_fee')) {
continue;
}
$role->grantPermission("update any commerce_fee");
$role->save();
}
}