-
Notifications
You must be signed in to change notification settings - Fork 8
/
AlertBannerEntityType.php
112 lines (97 loc) · 3.95 KB
/
AlertBannerEntityType.php
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
110
111
112
<?php
namespace Drupal\localgov_alert_banner\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\workflows\Entity\Workflow;
use Drupal\user\RoleInterface;
/**
* Defines the Alert banner type entity.
*
* @ConfigEntityType(
* id = "localgov_alert_banner_type",
* label = @Translation("Alert banner type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\localgov_alert_banner\AlertBannerEntityTypeListBuilder",
* "form" = {
* "add" = "Drupal\localgov_alert_banner\Form\AlertBannerEntityTypeForm",
* "edit" = "Drupal\localgov_alert_banner\Form\AlertBannerEntityTypeForm",
* "delete" = "Drupal\localgov_alert_banner\Form\AlertBannerEntityTypeDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\localgov_alert_banner\AlertBannerEntityTypeHtmlRouteProvider",
* },
* },
* config_prefix = "localgov_alert_banner_type",
* admin_permission = "administer localgov alert banner types",
* bundle_of = "localgov_alert_banner",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* config_export = {
* "id",
* "label"
* },
* links = {
* "canonical" = "/admin/structure/alert-banner-types/localgov_alert_banner_type/{localgov_alert_banner_type}",
* "add-form" = "/admin/structure/alert-banner-types/localgov_alert_banner_type/add",
* "edit-form" = "/admin/structure/alert-banner-types/localgov_alert_banner_type/{localgov_alert_banner_type}/edit",
* "delete-form" = "/admin/structure/alert-banner-types/localgov_alert_banner_type/{localgov_alert_banner_type}/delete",
* "collection" = "/admin/structure/alert-banner-types/localgov_alert_banner_type"
* }
* )
*/
class AlertBannerEntityType extends ConfigEntityBundleBase implements AlertBannerEntityTypeInterface {
/**
* The Alert banner type ID.
*
* @var string
*/
protected $id;
/**
* The Alert banner type label.
*
* @var string
*/
protected $label;
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
// Add fields and workflow when creating a new alert banner type.
if (!$update && !$this->isSyncing) {
$bundle = $this->id();
$config_directory = new FileStorage(__DIR__ . '/../../config/install');
// Fields to add to the new alert banner type.
$fields_to_add = [
'visibility',
];
foreach ($fields_to_add as $field_name) {
// Add field storage if necessary (it may have been deleted).
$field_storage = $config_directory->read('field.storage.localgov_alert_banner.' . $field_name);
if ($field_storage && !FieldStorageConfig::loadByName('localgov_alert_banner', $field_name)) {
FieldStorageConfig::create($field_storage)->save();
}
// Add field config for new bundle.
$field_record = $config_directory->read('field.field.localgov_alert_banner.localgov_alert_banner.' . $field_name);
if ($field_record && !FieldConfig::loadByName('localgov_alert_banner', $bundle, $field_name)) {
$field_record['bundle'] = $bundle;
FieldConfig::create($field_record)->save();
}
// Add alert banner workflow to new alert banner type.
if ($workflow = Workflow::load('localgov_alert_banners')) {
$workflow->getTypePlugin()->addEntityTypeAndBundle('localgov_alert_banner', $bundle);
$workflow->save();
}
}
// Default grant permissions to view.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view localgov alert banner ' . $bundle . ' entities']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view localgov alert banner ' . $bundle . ' entities']);
}
}
}