diff --git a/CHANGELOG.md b/CHANGELOG.md index 6733fd48..89ceadec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### Added - Added `calendar/calendars/fix-field-layout-tabs-elements-uids --rebuildProjectConfig` CLI command to resolve issues for Calendar 4.x and earlier versions of 5.0.x where unique UIDs were not being generated for field layout elements when duplicating calendars. This CLI command is only applicable to sites on Craft 4 that may have duplicated calendars in earlier versions of Calendar 4.x and 5.x. This command will not work on Craft 5 due to bigger differences in how the data is stored. +### Fixed +- Fixed a bug where Calendar event titles and fields were not translating correctly in Craft 5. + ### Removed - Removed the migration that attempted to resolve the issue of unique UIDs not being generated for field layout elements when duplicating calendars. - Removed unnecessary Craft version helper. diff --git a/packages/plugin/src/Controllers/EventsController.php b/packages/plugin/src/Controllers/EventsController.php index 9aa4e0b5..83553d13 100644 --- a/packages/plugin/src/Controllers/EventsController.php +++ b/packages/plugin/src/Controllers/EventsController.php @@ -17,6 +17,7 @@ use Solspace\Calendar\Calendar; use Solspace\Calendar\Elements\Event; use Solspace\Calendar\Library\Exceptions\EventException; +use Solspace\Calendar\Library\Helpers\CpHelper; use Solspace\Calendar\Library\Helpers\PermissionHelper; use Solspace\Calendar\Library\Helpers\SitesHelper; use Solspace\Calendar\Library\Transformers\EventToUiDataTransformer; @@ -551,7 +552,7 @@ private function renderEditForm(Event $event, string $title): Response 'label' => \Craft::t('site', $site->name), 'menu' => [ 'label' => \Craft::t('site', 'Select site'), - 'items' => Cp::siteMenuItems($sites, $site), + 'items' => CpHelper::siteMenuItems($sites, $site), ], ]; } diff --git a/packages/plugin/src/Library/Helpers/CpHelper.php b/packages/plugin/src/Library/Helpers/CpHelper.php new file mode 100644 index 00000000..eb36d414 --- /dev/null +++ b/packages/plugin/src/Library/Helpers/CpHelper.php @@ -0,0 +1,92 @@ + $sites + * + * @since 5.0.0 + */ + public static function siteMenuItems( + ?array $sites = null, + ?Site $selectedSite = null, + array $config = [], + ): array { + if (null === $sites) { + $sites = \Craft::$app->getSites()->getEditableSites(); + } + + $config += [ + 'showSiteGroupHeadings' => null, + 'includeOmittedSites' => false, + ]; + + $items = []; + + $siteGroups = \Craft::$app->getSites()->getAllGroups(); + $config['showSiteGroupHeadings'] ??= \count($siteGroups) > 1; + + // Normalize and index the sites + /** @var array $sites */ + $sites = Collection::make($sites) + ->map(fn (array|Site $site) => $site instanceof Site ? ['site' => $site] : $site) + ->keyBy(fn (array $site) => $site['site']->id) + ->all() + ; + + $request = \Craft::$app->getRequest(); + // Strip off events site handle + $segments = $request->getSegments(); + array_pop($segments); + $path = implode('/', $segments); + $params = $request->getQueryParamsWithoutPath(); + unset($params['fresh']); + + foreach ($siteGroups as $siteGroup) { + $groupSites = $siteGroup->getSites(); + if (!$config['includeOmittedSites']) { + $groupSites = array_filter($groupSites, fn (Site $site) => isset($sites[$site->id])); + } + + if (empty($groupSites)) { + continue; + } + + $groupSiteItems = array_map(fn (Site $site) => [ + 'status' => $sites[$site->id]['status'] ?? null, + 'label' => \Craft::t('site', $site->name), + // Use the selected site handle + 'url' => UrlHelper::cpUrl($path.'/'.$site->handle, ['site' => $site->handle] + $params), + 'hidden' => !isset($sites[$site->id]), + 'selected' => $site->id === $selectedSite?->id, + 'attributes' => [ + 'data' => [ + 'site-id' => $site->id, + ], + ], + ], $groupSites); + + if ($config['showSiteGroupHeadings']) { + $items[] = [ + 'heading' => \Craft::t('site', $siteGroup->name), + 'items' => $groupSiteItems, + 'hidden' => !ArrayHelper::contains($groupSiteItems, fn (array $item) => !$item['hidden']), + ]; + } else { + array_push($items, ...$groupSiteItems); + } + } + + return $items; + } +}