|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mmikkel\cpfieldinspect\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
|
|
7
|
|
|
use craft\elements\Asset; |
|
8
|
|
|
use craft\elements\Category; |
|
9
|
|
|
use craft\elements\Entry; |
|
10
|
|
|
use craft\elements\GlobalSet; |
|
11
|
|
|
use craft\elements\User; |
|
12
|
|
|
use craft\commerce\elements\Product; |
|
13
|
|
|
use craft\helpers\Html; |
|
14
|
|
|
use craft\helpers\UrlHelper; |
|
15
|
|
|
use craft\models\EntryType; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
use yii\base\InvalidConfigException; |
|
18
|
|
|
|
|
19
|
|
|
class CpFieldInspectHelper |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string|null $url |
|
24
|
|
|
* @return string |
|
25
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
26
|
|
|
* @throws \yii\base\Exception |
|
27
|
|
|
* @throws \yii\base\InvalidConfigException |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function getRedirectUrl(?string $url = null): string |
|
30
|
|
|
{ |
|
31
|
|
|
if (!$url) { |
|
32
|
|
|
$url = implode('?', array_filter([implode('/', Craft::$app->getRequest()->getSegments()), Craft::$app->getRequest()->getQueryStringWithoutPath()])); |
|
33
|
|
|
} |
|
34
|
|
|
// Special case for globals – account for their handles being edited before redirecting back |
|
35
|
|
|
$segments = explode('/', $url); |
|
36
|
|
|
if (($segments[0] ?? null) === 'globals') { |
|
37
|
|
|
if (Craft::$app->getIsMultiSite()) { |
|
38
|
|
|
$siteHandle = $segments[1] ?? null; |
|
39
|
|
|
$globalSetHandle = $segments[2] ?? null; |
|
40
|
|
|
} else { |
|
41
|
|
|
$siteHandle = Craft::$app->getSites()->getPrimarySite()->handle; |
|
42
|
|
|
$globalSetHandle = $segments[1] ?? null; |
|
43
|
|
|
} |
|
44
|
|
|
if ($siteHandle && $globalSetHandle && $globalSet = GlobalSet::find()->site($siteHandle)->handle($globalSetHandle)->one()) { |
|
45
|
|
|
$url = "edit/$globalSet->id?site=$siteHandle"; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
return Craft::$app->getSecurity()->hashData($url); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @throws InvalidConfigException |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function renderEditSourceLink(array $context): string |
|
55
|
|
|
{ |
|
56
|
|
|
$element = $context['element'] ?? $context['entry'] ?? $context['asset'] ?? $context['globalSet'] ?? $context['user'] ?? $context['category'] ?? $context['product'] ?? null; |
|
57
|
|
|
if (empty($element)) { |
|
58
|
|
|
return ''; |
|
59
|
|
|
} |
|
60
|
|
|
return static::getEditElementSourceButton($element); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ElementInterface|null $element |
|
65
|
|
|
* @param array $attributes |
|
66
|
|
|
* @param string|null $size |
|
67
|
|
|
* @return string |
|
68
|
|
|
* @throws \yii\base\InvalidConfigException |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function getEditElementSourceButton(?ElementInterface $element, array $attributes = [], ?string $size = null): string |
|
71
|
|
|
{ |
|
72
|
|
|
if (empty($element)) { |
|
73
|
|
|
return ''; |
|
74
|
|
|
} |
|
75
|
|
|
$html = ''; |
|
76
|
|
|
if ($element instanceof Entry) { |
|
77
|
|
|
if (!empty($element->typeId)) { |
|
78
|
|
|
$html .= static::_getEditSourceButtonHtml( |
|
79
|
|
|
label: 'Edit entry type', |
|
80
|
|
|
path: "settings/entry-types/$element->typeId", |
|
81
|
|
|
attributes: [ |
|
82
|
|
|
'data-type-id' => $element->typeId, |
|
83
|
|
|
], |
|
84
|
|
|
size: $size |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
if (!empty($element->sectionId)) { |
|
88
|
|
|
$html .= static::_getEditSourceButtonHtml(label: 'Edit section', path: "settings/sections/$element->sectionId", size: $size); |
|
89
|
|
|
} |
|
90
|
|
|
} else if ($element instanceof Asset) { |
|
91
|
|
|
$html = static::_getEditSourceButtonHtml(label: 'Edit volume', path: "settings/assets/volumes/{$element->volumeId}", size: $size); |
|
92
|
|
|
} else if ($element instanceof GlobalSet) { |
|
93
|
|
|
$html = static::_getEditSourceButtonHtml(label: 'Edit global set', path: "settings/globals/{$element->id}", size: $size); |
|
94
|
|
|
} else if ($element instanceof User) { |
|
95
|
|
|
$html = static::_getEditSourceButtonHtml('Edit settings', 'settings/users/fields', [ |
|
96
|
|
|
'style' => 'margin-top:20px;', |
|
97
|
|
|
], $size); |
|
98
|
|
|
} else if ($element instanceof Category) { |
|
99
|
|
|
$html = static::_getEditSourceButtonHtml(label: 'Edit category group', path: "settings/categories/{$element->groupId}", size: $size); |
|
100
|
|
|
} else if (class_exists(Product::class) && $element instanceof Product) { |
|
101
|
|
|
$html = static::_getEditSourceButtonHtml(label: 'Edit product type', path: "commerce/settings/producttypes/{$element->typeId}", size: $size); |
|
102
|
|
|
} |
|
103
|
|
|
if (empty($html)) { |
|
104
|
|
|
return ''; |
|
105
|
|
|
} |
|
106
|
|
|
return Html::tag('div', $html, [ |
|
107
|
|
|
...$attributes, |
|
108
|
|
|
'class' => [ |
|
109
|
|
|
'cp-field-inspect-sourcebtn-wrapper flex', |
|
110
|
|
|
...$attributes['class'] ?? [], |
|
111
|
|
|
], |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $label |
|
117
|
|
|
* @param string $path |
|
118
|
|
|
* @param array $attributes |
|
119
|
|
|
* @param string|null $size |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function _getEditSourceButtonHtml(string $label, string $path, array $attributes = [], ?string $size = null): string |
|
123
|
|
|
{ |
|
124
|
|
|
return Html::tag('a', Craft::t('cp-field-inspect', $label), [ |
|
125
|
|
|
'href' => UrlHelper::cpUrl($path), |
|
126
|
|
|
'class' => [ |
|
127
|
|
|
'btn settings icon', |
|
128
|
|
|
$size === 'small' ? 'small' : null, |
|
129
|
|
|
], |
|
130
|
|
|
'data-cp-field-inspect-sourcebtn' => true, |
|
131
|
|
|
...$attributes, |
|
132
|
|
|
]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths