-
Notifications
You must be signed in to change notification settings - Fork 1
/
wmsettings.module
103 lines (92 loc) · 2.82 KB
/
wmsettings.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
<?php
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\wmsettings\Field\IndexableBaseFieldDefinition;
/**
* Get the entity for a key.
*/
function wmsettings_get($key)
{
return Drupal::service('wmsettings.settings')
->read($key);
}
/**
* Implements hook_entity_base_field_info().
*
* More intel at https://www.drupal.org/node/1879542
*/
function wmsettings_entity_base_field_info(EntityTypeInterface $entity_type)
{
$fields = [];
// Attach a machine name field to our bundle.
if ($entity_type->id() == 'settings') {
$fields['wmsettings_key'] = IndexableBaseFieldDefinition::create('string')
->setLabel(t('Key'))
->setDescription(t('The key machine name of this config'))
->setRequired(true)
->setReadOnly(true)
->setDisplayConfigurable('view', false)
->setDisplayConfigurable('form', true)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'region' => 'hidden',
])
->addIndex('value');
}
return $fields;
}
function wmsettings_entity_insert(EntityInterface $entity)
{
\Drupal::getContainer()
->get('wmsettings.settings_subscriber')
->dispatchCacheTags($entity);
}
function wmsettings_entity_update(EntityInterface $entity)
{
\Drupal::getContainer()
->get('wmsettings.settings_subscriber')
->dispatchCacheTags($entity);
}
/**
* Disable the form action if they do not have the permission.
*
* @param $form
* @param $form_state
* @param $form_id
*/
function wmsettings_form_alter(&$form, $form_state, $form_id)
{
if (strpos($form_id, 'settings_') === 0) {
// check the permission for delete.
/** @var \Drupal\user\Entity\User $user */
$user = \Drupal::currentUser();
if (!$user->hasPermission('delete wmsettings group')) {
unset($form['actions']['delete']);
}
// Since the entity is NOT in the form we load it via the route.
if ($entity = \Drupal::routeMatch()->getParameter('settings')) {
$form['#title'] = t(
'Edit setting %settingName',
['%settingName' => $entity->type->entity->label()]
);
}
}
}
/**
* Prevent delete on settings if you don't have the proper rights.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity
* @param string $operation
* @param \Drupal\Core\Session\AccountInterface $user
* @return AccessResult
*/
function wmsettings_settings_access($entity, $operation, $user)
{
// Forbid.
if ($operation == 'delete') {
return AccessResult::allowedIfHasPermission($user, 'delete wmsettings group');
}
// Other wise...
return AccessResult::neutral();
}