addDependency('config'); } protected function getConfig() { return $this->getInjection('config'); } public function loadUrlField(Entity $entity) { if ($entity->get('customUrl')) { $entity->set('url', $entity->get('customUrl')); } $siteUrl = $this->getConfig()->get('siteUrl'); $siteUrl = rtrim($siteUrl , '/') . '/'; $url = $siteUrl . 'portal/'; if ($entity->id === $this->getConfig()->get('defaultPortalId')) { $entity->set('isDefault', true); $entity->setFetched('isDefault', true); } else { if ($entity->get('customId')) { $url .= $entity->get('customId') . '/'; } else { $url .= $entity->id . '/'; } $entity->set('isDefault', false); $entity->setFetched('isDefault', false); } if (!$entity->get('customUrl')) { $entity->set('url', $url); } } protected function afterSave(Entity $entity, array $options = []) { parent::afterSave($entity, $options); if ($entity->has('isDefault')) { if ($entity->get('isDefault')) { $defaultPortalId = $this->getConfig()->get('defaultPortalId'); if ($defaultPortalId !== $entity->id) { $this->getConfig()->set('defaultPortalId', $entity->id); $this->getConfig()->save(); } } else { if ($entity->isAttributeChanged('isDefault')) { if ($entity->getFetched('isDefault')) { $this->getConfig()->set('defaultPortalId', null); $this->getConfig()->save(); } } } } } }