1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace Drupal\TqExtension\Utils; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait BaseEntity. |
9
|
|
|
* |
10
|
|
|
* @package Drupal\TqExtension\Utils |
11
|
|
|
*/ |
12
|
|
|
trait BaseEntity |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return string |
16
|
|
|
* An entity type. |
17
|
|
|
*/ |
18
|
|
|
abstract protected function entityType(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $argument1 |
22
|
|
|
* An argument to clarify the results. |
23
|
|
|
* @param string $argument2 |
24
|
|
|
* An argument to clarify the results. |
25
|
|
|
* |
26
|
|
|
* @return int |
27
|
|
|
* An ID of the entity. |
28
|
|
|
*/ |
29
|
|
|
abstract public function getIdByArguments($argument1, $argument2); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get identifier of current entity. |
33
|
|
|
* |
34
|
|
|
* @return int |
35
|
|
|
*/ |
36
|
|
|
public function getCurrentId() |
37
|
|
|
{ |
38
|
|
|
// We have programmatically bootstrapped Drupal core, so able to use such functionality. |
39
|
|
|
$args = arg(); |
40
|
|
|
|
41
|
|
|
return count($args) > 1 && $this->entityType() === $args[0] && $args[1] > 0 ? (int) $args[1] : 0; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $operation |
46
|
|
|
* Allowable values: "edit", "view", "visit". |
47
|
|
|
* @param string $argument1 |
48
|
|
|
* An argument to clarify the result. |
49
|
|
|
* @param string $argument2 |
50
|
|
|
* An argument to clarify the result. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* Entity URL. |
54
|
|
|
*/ |
55
|
4 |
|
public function entityUrl($operation, $argument1 = '', $argument2 = '') |
56
|
|
|
{ |
57
|
4 |
|
if ('visit' === $operation) { |
58
|
4 |
|
$operation = 'view'; |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
// An empty string could be passed when currently viewing entity expected. |
62
|
4 |
|
$id = '' === $argument1 ? $this->getCurrentId() : $this->getIdByArguments($argument1, $argument2); |
63
|
|
|
|
64
|
4 |
|
if (0 === $id) { |
65
|
|
|
throw new \RuntimeException('An ID cannot be zero.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
return $this->entityType() . "/$id/$operation"; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|