Skip to content

Commit

Permalink
Updates address field
Browse files Browse the repository at this point in the history
  • Loading branch information
BenParizek committed Jul 5, 2024
1 parent b5954bc commit 74ec6df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
40 changes: 31 additions & 9 deletions src/meta/controllers/GlobalMetadataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use BarrelStrength\Sprout\meta\globals\Globals;
use BarrelStrength\Sprout\meta\MetaModule;
use Craft;
use craft\elements\Address;
use craft\helpers\Cp;
use craft\helpers\DateTimeHelper;
use craft\helpers\UrlHelper;
use craft\models\Site;
use craft\web\Controller;
use http\Exception\RuntimeException;
use yii\web\ForbiddenHttpException;
use yii\web\Response;

Expand Down Expand Up @@ -49,16 +51,16 @@ public function actionEditGlobalMetadata(string $selectedTabHandle, Globals $glo
$globals->siteId = $site->id;
}

//$locationField = Cp::addressCardsHtml(
// addresses: [$globals->addressModel],
// config: [
// 'name' => 'locationAddressId',
// 'maxAddresses' => 1,
// ]
//);
if (!$globals->addressModel) {
$address = $this->createAddress($globals, $site);
$globals->addressModel = $address;
}

// @todo - Cp::addressCardsHtml no longer exists. Explore options. None provided in C5 upgrade notes.
$locationField = '';
$locationField = Cp::elementCardHtml($globals->addressModel, [
'context' => 'field',
'inputName' => 'locationAddressId',
'showActionMenu' => true,
]);

$sites = Craft::$app->getSites()->getEditableSites();

Expand Down Expand Up @@ -166,4 +168,24 @@ public function actionSaveVerifyOwnership(): ?Response

return $this->redirectToPostedUrl($globals);
}

public function createAddress(Globals $globals, Site $site): Address
{
$address = new Address();
$address->title = Craft::t('sprout-module-meta', 'Website Identity');
Craft::$app->getElements()->saveElement($address);

$updatedGlobals = new Globals([
'siteId' => $site->id,
'identity' => array_merge($globals->getIdentity(), [
'locationAddressId' => $address->id,
]),
]);

if (!MetaModule::getInstance()->globalMetadata->saveGlobalMetadata('identity', $updatedGlobals)) {
throw new RuntimeException('Error configuring global address.');
}

return $address;
}
}
1 change: 1 addition & 0 deletions src/meta/globals/AddressHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ public static function registerEditAddressAuthorizationEvents(): void

Event::on(Address::class, Elements::EVENT_AUTHORIZE_VIEW, $checkAuth);
Event::on(Address::class, Elements::EVENT_AUTHORIZE_SAVE, $checkAuth);
Event::on(Address::class, Elements::EVENT_AUTHORIZE_CREATE_DRAFTS, $checkAuth);
}
}
8 changes: 0 additions & 8 deletions src/meta/globals/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ public function init(): void

/** @var Address $address */
$address = Craft::$app->getElements()->getElementById($elementId);
$this->addressModel = $address;
} elseif (Craft::$app->getRequest()->getIsCpRequest()) {
// @todo - this elseif should probably be delegated elsewhere
$address = new Address();
$address->title = Craft::t('sprout-module-meta', 'Address');

Craft::$app->getElements()->saveElement($address);

$this->identity['locationAddressId'] = $address->id;
$this->addressModel = $address;
}

Expand Down

0 comments on commit 74ec6df

Please sign in to comment.