Completed
Pull Request — master (#157)
by
unknown
01:50
created

UserDrupal8::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Drupal\Driver\Plugin\DriverEntity;
3
4
use Drupal\Driver\Plugin\DriverEntityPluginDrupal8Base;
5
6
/**
7
 * A driver field plugin used to test selecting an arbitrary plugin.
8
 *
9
 * @DriverEntity(
10
 *   id = "user8",
11
 *   version = 8,
12
 *   weight = -100,
13
 *   entityTypes = {
14
 *     "user",
15
 *   },
16
 *   labelKeys = {
17
 *     "name",
18
 *     "mail",
19
 *   },
20
 * )
21
 */
22
class UserDrupal8 extends DriverEntityPluginDrupal8Base
23
{
24
25
  /**
26
   * The id of the attached user.
27
   *
28
   * @var integer;
29
   *
30
   * @deprecated Use id() instead.
31
   */
32
    public $uid;
33
34
  /**
35
   * {@inheritdoc}
36
   */
37
    public function delete()
38
    {
39
        user_cancel(array(), $this->id(), 'user_cancel_delete');
40
    }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
    public function load($entityId)
46
    {
47
        $entity = parent::load($entityId);
48
        $this->uid = is_null($this->entity) ? NULL : $this->id();
0 ignored issues
show
Deprecated Code introduced by
The property Drupal\Driver\Plugin\Dri...ntity\UserDrupal8::$uid has been deprecated with message: Use id() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
49
        return $entity;
50
    }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
    public function save()
56
    {
57
        parent::save();
58
        $this->uid = $this->id();
0 ignored issues
show
Deprecated Code introduced by
The property Drupal\Driver\Plugin\Dri...ntity\UserDrupal8::$uid has been deprecated with message: Use id() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
59
    }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
    public function set($identifier, $field)
65
    {
66
      // Ignore the role key passed by Drupal extension.
67
      if ($identifier !== 'role') {
68
        parent::set($identifier, $field);
69
      }
70
    }
71
72
  /**
73
   * {@inheritdoc}
74
   */
75
    protected function getNewEntity()
76
    {
77
        $entity = parent::getNewEntity();
78
        $entity->set('status', 1);
79
        return $entity;
80
    }
81
82
  /**
83
   * Add a role by human-friendly identifier.
84
   *
85
   * @param string $roleIdentifier
86
   *   A human-friendly string identifying a role.
87
   */
88
    public function addRole($roleIdentifier)
89
    {
90
        // Use a driver field to convert identifier to id.
91
        $driverField = $this->getNewDriverField('roles', $roleIdentifier);
92
        $roleId = $driverField->getProcessedValues()[0]['target_id'];
93
94
        $roles = $this->getEntity()->getRoles(true);
95
        $roles[] = $roleId;
96
        $this->getEntity()->set('roles', array_unique($roles));
97
    }
98
}
99