comparison core/modules/rdf/src/Entity/RdfMapping.php @ 0:4c8ae668cc8c

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