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