1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace Drupal\TqExtension\Cores; |
6
|
|
|
|
7
|
|
|
use Behat\Behat\Hook\Scope\BeforeFeatureScope; |
8
|
|
|
use Drupal\Core\Field\FieldDefinitionInterface; |
9
|
|
|
use Drupal\Core\Entity\FieldableEntityInterface; |
10
|
|
|
use Drupal\Core\Session\AccountInterface; |
11
|
|
|
use Drupal\Core\Database\Database; |
12
|
|
|
use Drupal\Core\Database\Query\Select; |
13
|
|
|
use Drupal\Core\StringTranslation\TranslatableMarkup; |
14
|
|
|
use Drupal\Component\Render\FormattableMarkup; |
15
|
|
|
use Drupal\Component\Serialization\Json; |
16
|
|
|
use Drupal\TqExtension\Utils\Database\FetchField; |
17
|
|
|
|
18
|
|
|
final class Drupal8Placeholder extends DrupalKernelPlaceholder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public static function beforeFeature(BeforeFeatureScope $scope) |
24
|
|
|
{ |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public static function t($string, array $arguments = [], array $options = []) |
31
|
|
|
{ |
32
|
|
|
return (string) new TranslatableMarkup($string, $arguments, $options); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public static function formatString($string, array $arguments = []) |
39
|
|
|
{ |
40
|
|
|
return (string) new FormattableMarkup($string, $arguments); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public static function arg() |
47
|
|
|
{ |
48
|
|
|
return explode('/', \Drupal::service('path.current')->getPath()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public static function tokenReplace($text, array $data = [], array $options = []) |
55
|
|
|
{ |
56
|
|
|
return \Drupal::token()->replace($text, $data, $options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public static function sitePath() |
63
|
|
|
{ |
64
|
|
|
return \Drupal::service('site.path'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public static function jsonEncode($data) |
71
|
|
|
{ |
72
|
|
|
return Json::encode($data); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* |
78
|
|
|
* @return AccountInterface |
79
|
|
|
*/ |
80
|
|
|
public static function getCurrentUser() |
81
|
|
|
{ |
82
|
|
|
return \Drupal::currentUser()->getAccount(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
* |
88
|
|
|
* @param AccountInterface $user |
89
|
|
|
*/ |
90
|
|
|
public static function setCurrentUser($user) |
91
|
|
|
{ |
92
|
|
|
\Drupal::currentUser()->setAccount($user); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public static function setCurrentPath($path) |
99
|
|
|
{ |
100
|
|
|
\Drupal::service('path.current')->setPath($path); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public static function getUidByName($username) |
107
|
|
|
{ |
108
|
|
|
return (int) (new FetchField('users_field_data', 'uid')) |
109
|
|
|
->condition('name', $username) |
110
|
|
|
->execute(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public static function deleteUser($user_id) |
117
|
|
|
{ |
118
|
|
|
$user_storage = \Drupal::entityTypeManager()->getStorage('user'); |
119
|
|
|
$user_storage->delete([$user_storage->load($user_id)]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
* |
125
|
|
|
* @return Select |
126
|
|
|
*/ |
127
|
|
|
public static function selectQuery($table, $alias = null, array $options = []) |
128
|
|
|
{ |
129
|
|
|
return \Drupal::database()->select($table, $alias, $options); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public static function getFieldDefinitions($entityType, $bundle) |
136
|
|
|
{ |
137
|
|
|
$definitions = []; |
138
|
|
|
|
139
|
|
|
/** @var FieldDefinitionInterface $definition */ |
140
|
|
|
foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions($entityType, $bundle) as $name => $definition) { |
141
|
|
|
$definitions[$name] = [ |
142
|
|
|
'label' => (string) $definition->getLabel(), |
143
|
|
|
'required' => $definition->isRequired(), |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $definitions; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public static function getDatabaseConnectionInfo($connection) |
154
|
|
|
{ |
155
|
|
|
return Database::getConnectionInfo($connection); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public static function entityCreate($entityType, array $values) |
162
|
|
|
{ |
163
|
|
|
$entity = \Drupal::entityTypeManager() |
164
|
|
|
->getStorage($entityType) |
165
|
|
|
->create($values); |
166
|
|
|
|
167
|
|
|
$entity->save(); |
168
|
|
|
|
169
|
|
|
return [$entity->id(), $entityType, $entity->bundle()]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public static function entityLoad($entityType, $id) |
176
|
|
|
{ |
177
|
|
|
return \Drupal::entityTypeManager()->getStorage($entityType)->load($id); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
* |
183
|
|
|
* @param FieldableEntityInterface $entity |
184
|
|
|
*/ |
185
|
|
|
public static function entityHasField($entity, $fieldName) |
186
|
|
|
{ |
187
|
|
|
return $entity->hasField($fieldName); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
* |
193
|
|
|
* @param FieldableEntityInterface $entity |
194
|
|
|
*/ |
195
|
|
|
public static function entityFieldValue($entity, $fieldName) |
196
|
|
|
{ |
197
|
|
|
if ($entity instanceof FieldableEntityInterface) { |
|
|
|
|
198
|
|
|
return $entity->get($fieldName)->value; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
throw new \InvalidArgumentException(sprintf( |
202
|
|
|
'First argument for "%s" method must be of "%s" type.', |
203
|
|
|
__METHOD__, |
204
|
|
|
FieldableEntityInterface::class |
205
|
|
|
)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public static function switchMailSystem($useTesting) |
212
|
|
|
{ |
213
|
|
|
static $original = 'php_mail'; |
214
|
|
|
|
215
|
|
|
$systemMail = \Drupal::configFactory() |
216
|
|
|
->getEditable('system.mail'); |
217
|
|
|
|
218
|
|
|
if ($useTesting) { |
219
|
|
|
// Store original mail system to restore it after scenario. |
220
|
|
|
$original = $systemMail->get('interface.default') ?: $original; |
221
|
|
|
// Set the mail system for testing. It will store an emails in |
222
|
|
|
// "system.test_mail_collector" Drupal state instead of sending. |
223
|
|
|
$value = 'test_mail_collector'; |
224
|
|
|
} else { |
225
|
|
|
// Bring back the original mail system. |
226
|
|
|
$value = $original; |
227
|
|
|
// Flush the email buffer to be able to reuse it from scratch. |
228
|
|
|
// @see \Drupal\Core\Mail\Plugin\Mail\TestMailCollector |
229
|
|
|
\Drupal::state()->set('system.test_mail_collector', []); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$systemMail->set('interface.default', $value)->save(true); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public static function getEmailMessages() |
239
|
|
|
{ |
240
|
|
|
return \Drupal::state()->get('system.test_mail_collector') ?: []; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public static function getContentTypeName($contentType) |
247
|
|
|
{ |
248
|
|
|
$nodeTypeStorage = \Drupal::entityTypeManager() |
249
|
|
|
->getStorage('node_type'); |
250
|
|
|
|
251
|
|
|
if ($nodeTypeStorage->load($contentType)) { |
252
|
|
|
return $contentType; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// The result will be like: ['article' => 'article']. |
256
|
|
|
$result = $nodeTypeStorage->getQuery() |
257
|
|
|
->condition('name', $contentType) |
258
|
|
|
->execute(); |
259
|
|
|
|
260
|
|
|
return empty($result) ? '' : reset($result); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* {@inheritdoc} |
265
|
|
|
*/ |
266
|
|
|
public static function injectCustomJavascript($file, $delete = false) |
267
|
|
|
{ |
268
|
|
|
// @todo |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.