Skip to content

Commit

Permalink
fix(SFT-1449): site translation breadcrumbs in edit event details page (
Browse files Browse the repository at this point in the history
#330)

* fix(SFT-1449): site translation breadcrumbs in edit event details page
  • Loading branch information
seandelaney authored Oct 9, 2024
1 parent fb73012 commit 5934503
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/src/Controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
],
];
}
Expand Down
92 changes: 92 additions & 0 deletions packages/plugin/src/Library/Helpers/CpHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Solspace\Calendar\Library\Helpers;

use craft\helpers\ArrayHelper;
use craft\helpers\Cp;
use craft\helpers\UrlHelper;
use craft\models\Site;
use Illuminate\Support\Collection;

class CpHelper extends Cp
{
/**
* Returns a menu item array for the given sites, possibly grouping them by site group but using specific Calendar URL structure.
*
* @param array<int,array{site:Site,status?:string}|Site> $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<int,array{site:Site,status?:string}> $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;
}
}

0 comments on commit 5934503

Please sign in to comment.