|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Driver\Plugin\DriverEntity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Driver\Plugin\DriverEntityPluginDrupal8Base; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A driver field plugin used to test selecting an arbitrary plugin. |
|
9
|
|
|
* |
|
10
|
|
|
* @DriverEntity( |
|
11
|
|
|
* id = "user8", |
|
12
|
|
|
* version = 8, |
|
13
|
|
|
* weight = -100, |
|
14
|
|
|
* entityTypes = { |
|
15
|
|
|
* "user", |
|
16
|
|
|
* }, |
|
17
|
|
|
* labelKeys = { |
|
18
|
|
|
* "name", |
|
19
|
|
|
* "mail", |
|
20
|
|
|
* }, |
|
21
|
|
|
* ) |
|
22
|
|
|
*/ |
|
23
|
|
|
class UserDrupal8 extends DriverEntityPluginDrupal8Base { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The id of the attached user. |
|
27
|
|
|
* |
|
28
|
|
|
* @var int |
|
29
|
|
|
* |
|
30
|
|
|
* @deprecated Use id() instead. |
|
31
|
|
|
*/ |
|
32
|
|
|
public $uid; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function delete() { |
|
38
|
|
|
user_cancel(array(), $this->id(), 'user_cancel_delete'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function load($entityId) { |
|
45
|
|
|
$entity = parent::load($entityId); |
|
46
|
|
|
$this->uid = is_null($this->entity) ? NULL : $this->id(); |
|
|
|
|
|
|
47
|
|
|
return $entity; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function save() { |
|
54
|
|
|
parent::save(); |
|
55
|
|
|
$this->uid = $this->id(); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function set($identifier, $field) { |
|
62
|
|
|
// Ignore the role key passed by Drupal extension. |
|
63
|
|
|
if ($identifier !== 'role') { |
|
64
|
|
|
parent::set($identifier, $field); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getNewEntity() { |
|
72
|
|
|
$entity = parent::getNewEntity(); |
|
73
|
|
|
$entity->set('status', 1); |
|
74
|
|
|
return $entity; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add a role by human-friendly identifier. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $roleIdentifier |
|
81
|
|
|
* A human-friendly string identifying a role. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function addRole($roleIdentifier) { |
|
84
|
|
|
// Use a driver field to convert identifier to id. |
|
85
|
|
|
$driverField = $this->getNewDriverField('roles', $roleIdentifier); |
|
86
|
|
|
$roleId = $driverField->getProcessedValues()[0]['target_id']; |
|
87
|
|
|
|
|
88
|
|
|
$roles = $this->getEntity()->getRoles(TRUE); |
|
89
|
|
|
$roles[] = $roleId; |
|
90
|
|
|
$this->getEntity()->set('roles', array_unique($roles)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.