Open Data Catalog v2.0.0
AbstractDataNodeLifeCycle.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\node\Entity\Node;
8 
13  protected $node;
14 
18  public function __construct(EntityInterface $entity) {
19  $this->validate($entity);
20  $this->node = $entity;
21  }
22 
26  protected function getDataType() {
27  return $this->node->get('field_data_type')->value;
28  }
29 
33  protected function setDataType($type) {
34  $this->node->set('field_data_type', $type);
35  }
36 
40  protected function getMetaData() {
41  /* @var $entity \Drupal\node\Entity\Node */
42  $entity = $this->node;
43  return json_decode($entity->get('field_json_metadata')->value);
44  }
45 
49  protected function setMetadata($metadata) {
50  /* @var $entity \Drupal\node\Entity\Node */
51  $entity = $this->node;
52  $entity->set('field_json_metadata', json_encode($metadata));
53  }
54 
58  private function validate(EntityInterface $entity) {
59  if (!($entity instanceof Node)) {
60  throw new DataNodeLifeCycleEntityValidationException("We only work with nodes.");
61  }
62 
63  if ($entity->bundle() != "data") {
64  throw new DataNodeLifeCycleEntityValidationException("We only work with data nodes.");
65  }
66  }
67 
68 }
Drupal\dkan_common\AbstractDataNodeLifeCycle\setDataType
setDataType($type)
Definition: AbstractDataNodeLifeCycle.php:33
Drupal\dkan_common\AbstractDataNodeLifeCycle\$node
$node
Definition: AbstractDataNodeLifeCycle.php:13
Drupal\dkan_common
Definition: AbstractDataNodeLifeCycle.php:3
Drupal\dkan_common\AbstractDataNodeLifeCycle\getDataType
getDataType()
Definition: AbstractDataNodeLifeCycle.php:26
Drupal\dkan_common\Exception\DataNodeLifeCycleEntityValidationException
Definition: DataNodeLifeCycleEntityValidationException.php:8
Drupal\dkan_common\AbstractDataNodeLifeCycle\getMetaData
getMetaData()
Definition: AbstractDataNodeLifeCycle.php:40
Drupal\dkan_common\AbstractDataNodeLifeCycle\setMetadata
setMetadata($metadata)
Definition: AbstractDataNodeLifeCycle.php:49
Drupal\dkan_common\AbstractDataNodeLifeCycle
Definition: AbstractDataNodeLifeCycle.php:12
Drupal\dkan_common\AbstractDataNodeLifeCycle\__construct
__construct(EntityInterface $entity)
Definition: AbstractDataNodeLifeCycle.php:18