solspace / craft-freeform

Freeform for Craft: The most reliable form builder that's ready for wherever your project takes you.
https://docs.solspace.com/craft/freeform/v5/
Other
46 stars 58 forks source link

Elements integration issue on multi-site setup #1308

Open danystad opened 1 month ago

danystad commented 1 month ago

What happened?

On a multisite setup with site 1 and site 2, the second site (site 2) has a form with Elements Integration enabled for an entry type of site B. This entry type is only available on site 2.

When submitting a form, Craft CMS returns an error saying that Entry’s section (5) is not enabled for site 1.

Errors and Stack Trace (if available)

Invalid Configuration – yii\base\InvalidConfigException
Entry’s section (5) is not enabled for site 1

1. in /var/www/html/vendor/craftcms/cms/src/elements/Entry.phpat line 953
944945946947948949950951952953954955956957958959960961962    /**
     * @inheritdoc
     * @throws InvalidConfigException if [[siteId]] is not set to a site ID that the entry’s section is enabled for
     */
    public function getUriFormat(): ?string
    {
        $sectionSiteSettings = $this->getSection()->getSiteSettings();

        if (!isset($sectionSiteSettings[$this->siteId])) {
            throw new InvalidConfigException('Entry’s section (' . $this->sectionId . ') is not enabled for site ' . $this->siteId);
        }

        return $sectionSiteSettings[$this->siteId]->uriFormat;
    }

    /**
     * @inheritdoc
     */
    protected function route(): array|string|null
2. in /var/www/html/vendor/craftcms/cms/src/helpers/ElementHelper.php at line 128– craft\elements\Entry::getUriFormat()
122123124125126127128129130131132133134     *
     * @param ElementInterface $element
     * @throws OperationAbortedException if a unique URI could not be found
     */
    public static function setUniqueUri(ElementInterface $element): void
    {
        $uriFormat = $element->getUriFormat();

        // No URL format, no URI.
        if ($uriFormat === null) {
            $element->uri = null;
            return;
        }
3. in /var/www/html/vendor/craftcms/cms/src/services/Elements.php at line 1138– craft\helpers\ElementHelper::setUniqueUri(craft\elements\Entry)
1132113311341135113611371138113911401141114211431144            $this->trigger(self::EVENT_SET_ELEMENT_URI, $event);
            if ($event->handled) {
                return;
            }
        }

        ElementHelper::setUniqueUri($element);
    }

    /**
     * Merges recent canonical element changes into a given derivative, such as a draft.
     *
     * @param ElementInterface $element The derivative element
4. in /var/www/html/vendor/craftcms/cms/src/validators/ElementUriValidator.php at line 55– craft\services\Elements::setElementUri(craft\elements\Entry)
49505152535455565758596061            )
        ) {
            return;
        }

        try {
            Craft::$app->getElements()->setElementUri($model);
        } catch (OperationAbortedException) {
            // Not a big deal if the element isn't enabled yet
            if (
                $model->enabled &&
                $model->getEnabledForSite() &&
                !$model->getIsUnpublishedDraft()
5. in /var/www/html/vendor/yiisoft/yii2/validators/Validator.php at line 260– craft\validators\ElementUriValidator::validateAttribute(craft\elements\Entry, 'uri')
6. in /var/www/html/vendor/yiisoft/yii2/base/Model.php at line 368– yii\validators\Validator::validateAttributes(craft\elements\Entry, ['uri'])
7. in /var/www/html/vendor/craftcms/cms/src/base/Element.php at line 2458– yii\base\Model::validate(['id', 'contentId', 'parentId', 'root', ...], true)
2452245324542455245624572458245924602461246224632464    /**
     * @inheritdoc
     */
    public function validate($attributeNames = null, $clearErrors = true)
    {
        $this->_attributeNames = $attributeNames ? array_flip((array)$attributeNames) : null;
        $result = parent::validate($attributeNames, $clearErrors);
        $this->_attributeNames = null;
        return $result;
    }

    /**
     * @inheritdoc
8. in /var/www/html/vendor/solspace/craft-freeform/packages/plugin/src/Integrations/Elements/ElementsBundle.php at line 83– craft\base\Element::validate()
77787980818283848586878889            );

            if (!$event->isValid) {
                continue;
            }

            $element->validate();

            Event::trigger(
                ElementIntegrationInterface::class,
                ElementIntegrationInterface::EVENT_AFTER_VALIDATE,
                $event,
            );
9. Solspace\Freeform\Integrations\Elements\ElementsBundle::validate(Solspace\Freeform\Events\Integrations\ElementIntegrations\ValidateEvent)
10. in /var/www/html/vendor/yiisoft/yii2/base/Event.php at line 312– call_user_func([Solspace\Freeform\Integrations\Elements\ElementsBundle, 'validate'], Solspace\Freeform\Events\Forms\ValidationEvent)
11. in /var/www/html/vendor/solspace/craft-freeform/packages/plugin/src/Form/Form.php at line 870– yii\base\Event::trigger('Solspace\Freeform\Form\Form', 'before-validate', Solspace\Freeform\Events\Forms\ValidationEvent)
864865866867868869870871872873874875876        $this->setSubmission($event->getSubmission());
    }

    private function validate(): void
    {
        $event = new ValidationEvent($this);
        Event::trigger(self::class, self::EVENT_BEFORE_VALIDATE, $event);

        if (!$event->isValid) {
            $this->valid = $event->getValidationOverride();

            return;
        }
12. in /var/www/html/vendor/solspace/craft-freeform/packages/plugin/src/Form/Form.php at line 550– Solspace\Freeform\Form\Form::validate()
544545546547548549550551552553554555556 
        if (!$event->isValid) {
            return false;
        }

        if ($this->isPagePosted()) {
            $this->validate();
        }

        $event = new HandleRequestEvent($this, $request);
        Event::trigger(self::class, self::EVENT_AFTER_HANDLE_REQUEST, $event);

        return $event->isValid;
13. in /var/www/html/vendor/solspace/craft-freeform/packages/plugin/src/controllers/SubmitController.php at line 43– Solspace\Freeform\Form\Form::handleRequest(craft\web\Request)
37383940414243444546474849    {
        $request = \Craft::$app->getRequest();
        $isAjaxRequest = $request->getIsAjax();

        $form = $this->getFormFromRequest();

        $requestHandled = $form->handleRequest($request);
        $submissionsService = $this->getSubmissionsService();
        if ($requestHandled && $form->isFormPosted() && $form->isValid() && !$form->getActions() && $form->isFinished()) {
            $submissionsService->handleSubmission($form);

            $form->reset();
            $form->persistState();
14. Solspace\Freeform\controllers\SubmitController::actionIndex()
15. in /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php at line 57– call_user_func_array([Solspace\Freeform\controllers\SubmitController, 'actionIndex'], [])
16. in /var/www/html/vendor/yiisoft/yii2/base/Controller.php at line 178– yii\base\InlineAction::runWithParams([])
17. in /var/www/html/vendor/yiisoft/yii2/base/Module.php at line 552– yii\base\Controller::runAction('', [])
18. in /var/www/html/vendor/craftcms/cms/src/web/Application.php at line 340– yii\base\Module::runAction('freeform/submit', [])
334335336337338339340341342343344345346     * @param string $route
     * @param array $params
     * @return BaseResponse|null The result of the action, normalized into a Response object
     */
    public function runAction($route, $params = []): ?BaseResponse
    {
        $result = parent::runAction($route, $params);

        if ($result === null || $result instanceof BaseResponse) {
            return $result;
        }

        $response = $this->getResponse();
19. in /var/www/html/vendor/craftcms/cms/src/web/Application.php at line 641– craft\web\Application::runAction('freeform/submit', [])
635636637638639640641642643644645646647        if ($request->getIsActionRequest()) {
            $route = implode('/', $request->getActionSegments());

            try {
                Craft::debug("Route requested: '$route'", __METHOD__);
                $this->requestedRoute = $route;
                return $this->runAction($route, $_GET);
            } catch (Throwable $e) {
                $this->_unregisterDebugModule();
                if ($e instanceof InvalidRouteException) {
                    throw new NotFoundHttpException(Craft::t('yii', 'Page not found.'), $e->getCode(), $e);
                }
                throw $e;
20. in /var/www/html/vendor/craftcms/cms/src/web/Application.php at line 302– craft\web\Application::_processActionRequest(craft\web\Request)
296297298299300301302303304305306307308                    }
                }
            }
        }

        // If this is an action request, call the controller
        if (($response = $this->_processActionRequest($request)) !== null) {
            return $response;
        }

        // If we’re still here, finally let Yii do its thing.
        try {
            return parent::handleRequest($request);
21. in /var/www/html/vendor/yiisoft/yii2/base/Application.php at line 384– craft\web\Application::handleRequest(craft\web\Request)
22. in /var/www/html/web/index.php at line 12– yii\base\Application::run()
6789101112// Load shared bootstrap
require dirname(__DIR__) . '/bootstrap.php';

// Load and run Craft
/** @var craft\web\Application $app */
$app = require CRAFT_VENDOR_PATH . '/craftcms/cms/bootstrap/web.php';
$app->run();

How can we reproduce this?

  1. Set up a form that is published on site 2.
  2. Enable Elements integration for this form with an Entry Type that is enabled for site 2.
  3. Submitting the form returns an error message saying the section isn't enabled on site 1.

The above Element Integration was working on site 2 before upgrading from Craft CMS 3.9.13 and Freeform 3.13.37.

Freeform Edition

Pro

Freeform Version

5.3.1

Craft Version

4.9.5

When did this issue start?

After upgrading from older Freeform version

Previous Freeform Version

3.13.37 with Craft CMS 3.9.13

kjmartens commented 1 month ago

Hi @danystad,

Sorry for the trouble you're experiencing. I have played around with this now and have been able to duplicate the issue. I've queued it up and we'll try to get a fix for it soon. 🙂

kjmartens commented 2 weeks ago

This should now be resolved in Freeform 5.3.5+ 🙂

danystad commented 2 weeks ago

Hi @kjmartens,

I updated Freeform to 5.3.5, and I'm getting almost the same error, except it now says "site 0" instead of "site 1".

Invalid Configuration – [yii\base\InvalidConfigException](https://www.yiiframework.com/doc-2.0/yii-base-invalidconfigexception.html)
Entry’s section (5) is not enabled for site 0
1. in /var/www/html/vendor/craftcms/cms/src/elements/Entry.phpat line 953
944945946947948949950951952953954955956957958959960961962    /**
     * @inheritdoc
     * @throws InvalidConfigException if [[siteId]] is not set to a site ID that the entry’s section is enabled for
     */
    public function getUriFormat(): ?string
    {
        $sectionSiteSettings = $this->getSection()->getSiteSettings();

        if (!isset($sectionSiteSettings[$this->siteId])) {
            throw new InvalidConfigException('Entry’s section (' . $this->sectionId . ') is not enabled for site ' . $this->siteId);
        }

        return $sectionSiteSettings[$this->siteId]->uriFormat;
    }

    /**
     * @inheritdoc
     */
    protected function route(): array|string|null

Please let me know if you need anything else.

Thank you!

kjmartens commented 3 hours ago

Sorry that this continues to be an issue for you @danystad.

Can you create a more detailed step-by-step guide on how we can reproduce this issue reliably? Currently we can't seem to duplicate this issue on our end. 😕