Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\rdf\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
7 use Drupal\rdf\RdfMappingInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Config entity for working with RDF mappings.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @ConfigEntityType(
|
Chris@0
|
13 * id = "rdf_mapping",
|
Chris@0
|
14 * label = @Translation("RDF mapping"),
|
Chris@0
|
15 * label_singular = @Translation("RDF mapping item"),
|
Chris@0
|
16 * label_plural = @Translation("RDF mappings items"),
|
Chris@0
|
17 * label_count = @PluralTranslation(
|
Chris@0
|
18 * singular = "@count RDF mapping item",
|
Chris@0
|
19 * plural = "@count RDF mapping items",
|
Chris@0
|
20 * ),
|
Chris@0
|
21 * config_prefix = "mapping",
|
Chris@0
|
22 * entity_keys = {
|
Chris@0
|
23 * "id" = "id"
|
Chris@0
|
24 * },
|
Chris@0
|
25 * admin_permission = "administer site configuration",
|
Chris@0
|
26 * config_export = {
|
Chris@0
|
27 * "id",
|
Chris@0
|
28 * "targetEntityType",
|
Chris@0
|
29 * "bundle",
|
Chris@0
|
30 * "types",
|
Chris@0
|
31 * "fieldMappings",
|
Chris@0
|
32 * }
|
Chris@0
|
33 * )
|
Chris@0
|
34 */
|
Chris@0
|
35 class RdfMapping extends ConfigEntityBase implements RdfMappingInterface {
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Unique ID for the config entity.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var string
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $id;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Entity type to be mapped.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @var string
|
Chris@0
|
48 */
|
Chris@0
|
49 protected $targetEntityType;
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Bundle to be mapped.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @var string
|
Chris@0
|
55 */
|
Chris@0
|
56 protected $bundle;
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * The RDF type mapping for this bundle.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @var array
|
Chris@0
|
62 */
|
Chris@0
|
63 protected $types = [];
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * The mappings for fields on this bundle.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @var array
|
Chris@0
|
69 */
|
Chris@0
|
70 protected $fieldMappings = [];
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * {@inheritdoc}
|
Chris@0
|
74 */
|
Chris@0
|
75 public function getPreparedBundleMapping() {
|
Chris@0
|
76 return ['types' => $this->types];
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * {@inheritdoc}
|
Chris@0
|
81 */
|
Chris@0
|
82 public function getBundleMapping() {
|
Chris@0
|
83 if (!empty($this->types)) {
|
Chris@0
|
84 return ['types' => $this->types];
|
Chris@0
|
85 }
|
Chris@0
|
86 return [];
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 */
|
Chris@0
|
92 public function setBundleMapping(array $mapping) {
|
Chris@0
|
93 if (isset($mapping['types'])) {
|
Chris@0
|
94 $this->types = $mapping['types'];
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 return $this;
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * {@inheritdoc}
|
Chris@0
|
102 */
|
Chris@0
|
103 public function getPreparedFieldMapping($field_name) {
|
Chris@0
|
104 $field_mapping = [
|
Chris@0
|
105 'properties' => NULL,
|
Chris@0
|
106 'datatype' => NULL,
|
Chris@0
|
107 'datatype_callback' => NULL,
|
Chris@0
|
108 'mapping_type' => NULL,
|
Chris@0
|
109 ];
|
Chris@0
|
110 if (isset($this->fieldMappings[$field_name])) {
|
Chris@0
|
111 $field_mapping = array_merge($field_mapping, $this->fieldMappings[$field_name]);
|
Chris@0
|
112 }
|
Chris@0
|
113 return empty($field_mapping['properties']) ? [] : $field_mapping;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public function getFieldMapping($field_name) {
|
Chris@0
|
120 if (isset($this->fieldMappings[$field_name])) {
|
Chris@0
|
121 return $this->fieldMappings[$field_name];
|
Chris@0
|
122 }
|
Chris@0
|
123 return [];
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * {@inheritdoc}
|
Chris@0
|
128 */
|
Chris@0
|
129 public function setFieldMapping($field_name, array $mapping = []) {
|
Chris@0
|
130 $this->fieldMappings[$field_name] = $mapping;
|
Chris@0
|
131 return $this;
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * {@inheritdoc}
|
Chris@0
|
136 */
|
Chris@0
|
137 public function id() {
|
Chris@0
|
138 return $this->targetEntityType . '.' . $this->bundle;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * {@inheritdoc}
|
Chris@0
|
143 */
|
Chris@0
|
144 public function calculateDependencies() {
|
Chris@0
|
145 parent::calculateDependencies();
|
Chris@0
|
146
|
Chris@0
|
147 // Create dependency on the bundle.
|
Chris@0
|
148 $entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType);
|
Chris@0
|
149 $this->addDependency('module', $entity_type->getProvider());
|
Chris@0
|
150 $bundle_config_dependency = $entity_type->getBundleConfigDependency($this->bundle);
|
Chris@0
|
151 $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
|
Chris@0
|
152
|
Chris@0
|
153 return $this;
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * {@inheritdoc}
|
Chris@0
|
158 */
|
Chris@0
|
159 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
160 parent::postSave($storage, $update);
|
Chris@0
|
161
|
Chris@0
|
162 if (\Drupal::entityManager()->hasHandler($this->targetEntityType, 'view_builder')) {
|
Chris@0
|
163 \Drupal::entityManager()->getViewBuilder($this->targetEntityType)->resetCache();
|
Chris@0
|
164 }
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 }
|