diff --git a/src/meta/controllers/GlobalMetadataController.php b/src/meta/controllers/GlobalMetadataController.php index d26b3b85..8f620bba 100755 --- a/src/meta/controllers/GlobalMetadataController.php +++ b/src/meta/controllers/GlobalMetadataController.php @@ -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; @@ -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(); @@ -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; + } } diff --git a/src/meta/globals/AddressHelper.php b/src/meta/globals/AddressHelper.php index 02be13dd..740fe57c 100644 --- a/src/meta/globals/AddressHelper.php +++ b/src/meta/globals/AddressHelper.php @@ -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); } } diff --git a/src/meta/globals/Globals.php b/src/meta/globals/Globals.php index 256a6797..d45d033b 100755 --- a/src/meta/globals/Globals.php +++ b/src/meta/globals/Globals.php @@ -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; }