|
5 | 5 | use Craft; |
6 | 6 | use craft\web\UrlManager; |
7 | 7 | use Illuminate\Support\Arr; |
| 8 | +use craft\elements\Entry; |
| 9 | +use craft\elements\Category; |
8 | 10 | use craft\services\Elements; |
9 | 11 |
|
10 | 12 | use yii\web\NotFoundHttpException; |
@@ -51,7 +53,11 @@ public function actionIndex(): array|string |
51 | 53 | // We're probably validating an element in a form |
52 | 54 | // And need to give to give it the user to get validation errors |
53 | 55 | $element = Craft::$container->get('currentElement'); |
54 | | - $templateVariables['element'] = $element; |
| 56 | + if ($element instanceof Entry) { |
| 57 | + $templateVariables['entry'] = $element; |
| 58 | + } else if ($element instanceof Category) { |
| 59 | + $templateVariables['category'] = $element; |
| 60 | + } |
55 | 61 | } |
56 | 62 |
|
57 | 63 | try { |
@@ -294,30 +300,35 @@ private function extractUriParameters($uri, $uriFormat) |
294 | 300 |
|
295 | 301 | private function handleElementRequest($element, $uri) |
296 | 302 | { |
297 | | - $section = $element->getSection(); |
| 303 | + $sectionOrGroup = $element instanceof Entry ? $element->getSection() : $element->getGroup(); |
298 | 304 |
|
299 | 305 | $site = Craft::$app->getSites()->getCurrentSite(); |
300 | 306 | $siteID = $site->id; |
301 | 307 |
|
302 | | - /** @var array $sectionSiteSettings */ |
303 | | - $sectionSiteSettings = $section->getSiteSettings(); |
304 | | - $sectionSiteSetting = null; |
305 | | - foreach ($sectionSiteSettings as $setting) { |
| 308 | + /** @var array $siteSettings */ |
| 309 | + $siteSettings = $sectionOrGroup->getSiteSettings(); |
| 310 | + $siteSetting = null; |
| 311 | + foreach ($siteSettings as $setting) { |
306 | 312 | if ($setting->siteId === $siteID) { |
307 | | - $sectionSiteSetting = $setting; |
| 313 | + $siteSetting = $setting; |
308 | 314 | break; |
309 | 315 | } |
310 | 316 | } |
311 | 317 |
|
312 | | - if ($sectionSiteSetting === null) { |
| 318 | + if ($siteSetting === null) { |
313 | 319 | throw new \Exception('No section site setting found for the current site.'); |
314 | 320 | } |
315 | 321 |
|
316 | | - $uriFormat = $sectionSiteSetting->uriFormat; |
| 322 | + $uriFormat = $siteSetting->uriFormat; |
317 | 323 |
|
318 | | - $specifiedTemplate = $sectionSiteSetting->template; |
| 324 | + $specifiedTemplate = $siteSetting->template; |
319 | 325 | $templateVariables = $this->extractUriParameters($uri, $uriFormat); |
320 | | - $templateVariables['element'] = $element; |
| 326 | + |
| 327 | + if ($element instanceof Entry) { |
| 328 | + $templateVariables['entry'] = $element; |
| 329 | + } elseif ($element instanceof Category) { |
| 330 | + $templateVariables['category'] = $element; |
| 331 | + } |
321 | 332 |
|
322 | 333 | $matchesTwigTemplate = Craft::$app->getView()->doesTemplateExist($specifiedTemplate); |
323 | 334 | return [$matchesTwigTemplate, $specifiedTemplate, $templateVariables]; |
|
0 commit comments