5 use Contracts\BulkRetrieverInterface;
6 use Contracts\RemoverInterface;
7 use Contracts\RetrieverInterface;
8 use Contracts\StorerInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
16 class Data implements StorerInterface, RetrieverInterface, BulkRetrieverInterface, RemoverInterface {
23 private $entityTypeManager;
38 public function __construct(EntityTypeManagerInterface $entityTypeManager) {
39 $this->entityTypeManager = $entityTypeManager;
49 $this->schemaId = $schema_id;
59 if (!isset($this->schemaId)) {
60 throw new \Exception(
"Data schemaId not set in retrieveAll().");
63 $nodeStorage = $this->getNodeStorage();
65 $node_ids = $nodeStorage->getQuery()
66 ->condition(
'type', $this->getType())
67 ->condition(
'field_data_type', $this->schemaId)
71 foreach ($node_ids as $nid) {
73 $node = $nodeStorage->load($nid);
74 $fieldList = $node->get(
'field_json_metadata');
75 $field = $fieldList->get(0);
76 $data = $field->getValue();
77 $all[] = $data[
'value'];
87 public function retrieve(
string $id): ?string {
89 if (!isset($this->schemaId)) {
90 throw new \Exception(
"Data schemaId not set in retrieve().");
93 if (FALSE !== ($node = $this->getNodeByUuid($id))) {
94 $value = $node->get(
'field_json_metadata')->get(0)->getValue();
95 return $value[
'value'];
98 throw new \Exception(
"No data with the identifier {$id} was found.");
106 public function remove(
string $id) {
108 if (FALSE !== ($node = $this->getNodeByUuid($id))) {
109 return $node->delete();
118 public function store($data,
string $id = NULL): string {
120 if (!isset($this->schemaId)) {
121 throw new \Exception(
"Data schemaId not set in store().");
124 $data = json_decode($data);
125 $data = $this->filterHtml($data);
127 $id = (!$id && isset($data->identifier)) ? $data->identifier : $id;
130 $node = $this->getNodeByUuid($id);
135 return $this->updateExistingNode($node, $data);
139 return $this->createNewNode($id, $data);
146 private function updateExistingNode($node, $data) {
147 $node->field_data_type = $this->schemaId;
148 $new_data = json_encode($data);
149 $node->field_json_metadata = $new_data;
151 $node->setNewRevision(TRUE);
152 $node->isDefaultRevision(TRUE);
153 $node->setRevisionLogMessage(
"Updated on " . $this->formattedTimestamp());
156 return $node->uuid();
162 private function createNewNode($id, $data) {
163 $title = isset($data->title) ? $data->title : $data->name;
164 $node = $this->getNodeStorage()
170 'field_data_type' => $this->schemaId,
171 'field_json_metadata' => json_encode($data),
174 $node->setRevisionLogMessage(
"Created on " . $this->formattedTimestamp());
177 return $node->uuid();
186 private function getNodeStorage() {
187 return $this->entityTypeManager
188 ->getStorage(
'node');
197 private function getType() {
207 private function getNodeByUuid($uuid) {
209 $nodes = $this->getNodeStorage()->loadByProperties(
211 'type' => $this->getType(),
217 return current($nodes);
229 private function filterHtml($input) {
230 switch (gettype($input)) {
232 return $this->htmlPurifier($input);
236 foreach ($input as &$value) {
237 $value = $this->filterHtml($value);
260 private function htmlPurifier(
string $input) {
261 $filter =
new HTMLPurifier();
262 return $filter->purify($input);
271 private function formattedTimestamp() : string {
272 $now = new \DateTime(
'now');
273 return $now->format(DateTime::ATOM);