|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Driver\Wrapper\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Driver\Plugin\DriverPluginManagerInterface; |
|
6
|
|
|
use Drupal\Driver\Plugin\DriverNameMatcher; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A Driver wrapper for Drupal 8 entities. |
|
10
|
|
|
*/ |
|
11
|
|
|
class DriverEntityDrupal8 extends DriverEntityBase implements DriverEntityWrapperInterface { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The Drupal version being driven. |
|
15
|
|
|
* |
|
16
|
|
|
* @var int |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $version = 8; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct( |
|
24
|
|
|
$type, |
|
25
|
|
|
$bundle = NULL, |
|
26
|
|
|
DriverPluginManagerInterface $entityPluginManager = NULL, |
|
27
|
|
|
DriverPluginManagerInterface $fieldPluginManager = NULL, |
|
28
|
|
|
$projectPluginRoot = NULL |
|
29
|
|
|
) { |
|
30
|
|
|
// Set Drupal environment variables used by default plugin manager. |
|
31
|
|
|
$this->namespaces = \Drupal::service('container.namespaces'); |
|
|
|
|
|
|
32
|
|
|
$this->cache_backend = $cache_backend = \Drupal::service('cache.discovery'); |
|
|
|
|
|
|
33
|
|
|
$this->module_handler = $module_handler = \Drupal::service('module_handler'); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
parent::__construct($type, $bundle, $entityPluginManager, $fieldPluginManager, $projectPluginRoot); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function create(array $fields, $type, $bundle = NULL) { |
|
42
|
|
|
$entity = new DriverEntityDrupal8( |
|
43
|
|
|
$type, |
|
44
|
|
|
$bundle |
|
45
|
|
|
); |
|
46
|
|
|
$entity->setFields($fields); |
|
47
|
|
|
return $entity; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setBundle($identifier) { |
|
54
|
|
|
// Don't set a bundle if the entity doesn't support bundles. |
|
55
|
|
|
$supportsBundles = $this->getProvisionalPlugin()->supportsBundles(); |
|
56
|
|
|
if ($supportsBundles) { |
|
57
|
|
|
$bundles = $this->getProvisionalPlugin()->getBundles(); |
|
58
|
|
|
$matcher = new DriverNameMatcher($bundles); |
|
59
|
|
|
$result = $matcher->identify($identifier); |
|
60
|
|
|
if (is_null($result)) { |
|
61
|
|
|
throw new \Exception("'$identifier' could not be identified as a bundle of the '" . $this->getEntityTypeId() . "' entity type."); |
|
62
|
|
|
} |
|
63
|
|
|
parent::setBundle($result); |
|
64
|
|
|
} |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set the entity type. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $identifier |
|
72
|
|
|
* A human-friendly name for an entity type . |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function setType($identifier) { |
|
75
|
|
|
$typeDefinitions = \Drupal::EntityTypeManager()->getDefinitions(); |
|
76
|
|
|
$candidates = []; |
|
77
|
|
|
foreach ($typeDefinitions as $machineName => $typeDefinition) { |
|
78
|
|
|
$label = (string) $typeDefinition->getLabel(); |
|
79
|
|
|
$candidates[$label] = $machineName; |
|
80
|
|
|
} |
|
81
|
|
|
$matcher = new DriverNameMatcher($candidates); |
|
82
|
|
|
$result = $matcher->identify($identifier); |
|
83
|
|
|
if (is_null($result)) { |
|
84
|
|
|
throw new \Exception("'$identifier' could not be identified as an entity type."); |
|
85
|
|
|
} |
|
86
|
|
|
$this->type = $result; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: