1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\Driver\Cores; |
4
|
|
|
|
5
|
|
|
use function bovigo\assert\assert; |
6
|
|
|
use function bovigo\assert\predicate\isNotEmpty; |
7
|
|
|
use function bovigo\assert\predicate\isNotEqualTo; |
8
|
|
|
use Drupal\Driver\Cores\Drupal8 as OriginalDrupal8; |
9
|
|
|
use Drupal\node\Entity\Node; |
10
|
|
|
use Drupal\node\Entity\NodeType; |
11
|
|
|
use Drupal\taxonomy\Entity\Term; |
12
|
|
|
use Drupal\taxonomy\Entity\Vocabulary; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Drupal8. |
16
|
|
|
* |
17
|
|
|
* @package NuvoleWeb\Drupal\Driver\Cores |
18
|
|
|
*/ |
19
|
|
|
class Drupal8 extends OriginalDrupal8 implements CoreInterface { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function convertLabelToNodeTypeId($type) { |
|
|
|
|
25
|
|
|
// First suppose that the id has been passed. |
26
|
|
|
if (NodeType::load($type)) { |
27
|
|
|
return $type; |
28
|
|
|
} |
29
|
|
|
$storage = \Drupal::entityTypeManager()->getStorage('node_type'); |
30
|
|
|
$result = $storage->loadByProperties(['name' => $type]); |
31
|
|
|
assert($result, isNotEmpty()); |
32
|
|
|
return key($result); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function convertLabelToTermTypeId($type) { |
|
|
|
|
39
|
|
|
// First suppose that the id has been passed. |
40
|
|
|
if (Vocabulary::load($type)) { |
41
|
|
|
return $type; |
42
|
|
|
} |
43
|
|
|
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary'); |
44
|
|
|
$result = $storage->loadByProperties(['name' => $type]); |
45
|
|
|
assert($result, isNotEmpty()); |
46
|
|
|
return key($result); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function loadNodeByName($title) { |
|
|
|
|
53
|
|
|
$result = \Drupal::entityQuery('node') |
54
|
|
|
->condition('title', $title) |
55
|
|
|
->condition('status', NODE_PUBLISHED) |
56
|
|
|
->range(0, 1) |
57
|
|
|
->execute(); |
58
|
|
|
assert($result, isNotEmpty()); |
59
|
|
|
$nid = current($result); |
60
|
|
|
return Node::load($nid); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getEntityIdByLabel($entity_type, $bundle, $label) { |
67
|
|
|
/** @var \Drupal\node\NodeStorage $storage */ |
68
|
|
|
$storage = \Drupal::entityTypeManager()->getStorage($entity_type); |
69
|
|
|
$bundle_key = $storage->getEntityType()->getKey('bundle'); |
70
|
|
|
$label_key = $storage->getEntityType()->getKey('label'); |
71
|
|
|
$result = \Drupal::entityQuery($entity_type) |
72
|
|
|
->condition($bundle_key, $bundle) |
73
|
|
|
->condition($label_key, $label) |
74
|
|
|
->range(0, 1) |
75
|
|
|
->execute(); |
76
|
|
|
assert($result, isNotEmpty()); |
77
|
|
|
return current($result); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function loadUserByName($name) { |
84
|
|
|
$user = user_load_by_name($name); |
85
|
|
|
assert($user, isNotEqualTo(FALSE)); |
86
|
|
|
return $user; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function nodeAccess($op, $name, $node) { |
93
|
|
|
$account = $this->loadUserByName($name); |
94
|
|
|
return $node->access($op, $account); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function getNodeId($node) { |
101
|
|
|
return $node->id(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function loadTaxonomyTermByName($type, $name) { |
|
|
|
|
108
|
|
|
$result = \Drupal::entityQuery('taxonomy_term') |
109
|
|
|
->condition('name', $name) |
110
|
|
|
->condition('vid', $type) |
111
|
|
|
->range(0, 1) |
112
|
|
|
->execute(); |
113
|
|
|
assert($result, isNotEmpty()); |
114
|
|
|
$id = current($result); |
115
|
|
|
return Term::load($id); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getTaxonomyTermId($term) { |
122
|
|
|
return $term->id(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.