digitalpulsebe / craft-multi-translator

Translate Entry content using external services
Other
0 stars 1 forks source link

Error thrown - Attempt to read property "cpEditUrl" on null #1

Closed tigerchick closed 6 months ago

tigerchick commented 6 months ago

Whenever I try to translate an entry (I'm using the sidepanel within the entry itself) I get the following error within craft's UI:

digitalpulsebe\craftmultitranslator\services\TranslateService::translateEntry(): Argument #1 ($source) must be of type craft\elements\Entry, null given, called in /vendor/digitalpulsebe/craft-multi-translator/src/controllers/SidebarController.php on line 28

and this PHP warning:

PHP Warning – [yii\base\ErrorException](https://www.yiiframework.com/doc-2.0/yii-base-errorexception.html)
Attempt to read property "cpEditUrl" on null

1. in /vendor/digitalpulsebe/craft-multi-translator/src/controllers/SidebarController.phpat line 33
242526272829303132333435363738        $sourceSite = Craft::$app->sites->getSiteById($sourceSiteId);
        $targetSite = Craft::$app->sites->getSiteById($targetSiteId);

        try {
            $translatedElement = MultiTranslator::getInstance()->translate->translateEntry($element, $sourceSite, $targetSite);
            return $this->asSuccess('Entry translated', ['elementId' => $elementId], $translatedElement->cpEditUrl);
        } catch (\Throwable $throwable) {
            $target = Entry::find()->status(null)->id($elementId)->siteId($targetSiteId)->one();
            Craft::$app->session->setError($throwable->getMessage());
            return $this->redirect($target->cpEditUrl);
        }

    }

}

If I try to translate an entry via the entry overview list instead then it appears to be sent to the queue and to successfully process within the queue but then on viewing the entry (in the backed) the original text remains - no translation. Any help appreciated.

JodebaDigitalPulse commented 6 months ago

Hi there,

Thank you for your message. My first guess is that you are trying to save a draft. Is that correct? That indeed, seems not possible yet. I'll look for a solution as soon as possible.

If it is not a draft, can you provide some more information please:

Best regards, Jonathan

tigerchick commented 6 months ago

Thanks for getting back so quickly @JodebaDigitalPulse My first guess is that you are trying to save a draft. Is that correct? I don't believe so, no - not sure how I'd even do that using the Multi Translator side panel. Within an entry I simply select the source entry I wish to translate from on the side panel.

JodebaDigitalPulse commented 6 months ago

Thank you for the information.

Instead of a Draft, maybe there are still pending "unsaved changes", so it might be a revision.

I altered the button to start from the canonicalId instead of the entry.id, so "unsaved changes" won't affect the source. But still: when the target has "unsaved changes", they will show on top of the translated item. So you will have to discard the "unsaved changes" too see the translated result.

Can you try to update the plugin to 1.5.0? Or look for "unsaved changes"

tigerchick commented 6 months ago

Hi, thanks so much for looking at this. The update has prevented the error from appearing - and yes, in the source entry there were unsaved changed (not any more). However - it appears that often the translated text does not appear to populate the fields. Sometimes it works, sometimes not. I haven't been able to find a pattern. There are 6 different languages - and it's not always the same one. Sometimes it will work for one entry in one language but not another - sometimes on a different entry it will work for languages that didn't work on the first entry. It seems random. This is using both the side panel within an entry and also the bulk tool. When it doesn't work, there is no error. Everything indicates that it is behaving as expected - it's just that the translated text doesn't actually appear within the fields to replace the original English.

JodebaDigitalPulse commented 6 months ago

Hi,

can you check the settings of each field? Because the translation method of each field is respected as well. When you have a matrix field, It needs to be translatable. But each field of each matrix block type has a translation method as well. So make sure to check that.

If this doesn't help, I added a debug option in 1.5.1 Then add a file config/multi-translator.php

<?php

return [
    "debug" => true,
];

make sure you have the API keys as environment variables, so they don't get logged. You can send me the log entry of something that goes wrong, with a description of what is missing, and I will have a look at it.

Thank you! I hope this helps.

tigerchick commented 6 months ago

Hi there, just installed the update but it seems to make no difference... (to the Portuguese issue and I also can't see anything getting logged) did you commit the code changes?

Matrix blocks themselves no longer seem to have a translation method (I remember they used to). But the sub-fields are all set to be translatable where relevant - and it does work sometimes; it's just sporadic. Also makes no difference whether its a matrix field or not - plain text fields have the same issue. It either translates everything (all translatable fields) correctly - or not at all.

JodebaDigitalPulse commented 6 months ago

Hi, You're right, my apologies, I used the wrong gitflow flow. Please update to 1.5.2

tigerchick commented 6 months ago

Thanks, this is logging now. The Portuguese error is gone too. Where would you like me to send the log? I can see that fields are translated within the log (there's the original English and translated Portuguese text in there) but that the translated text isn't making it's way into the entry form for some reason.

JodebaDigitalPulse commented 6 months ago

Yes please, can you send the logs? I can think of two reasons:

tigerchick commented 6 months ago

Yes please, can you send the logs? I can think of two reasons:

  • there are unsaved changes on the Entry, that's why you might not see the translated value
  • there are validation errors, why it is not saving. For the moment they are not processed. Do you have versioning enabled? Do you see a new version saved after the translation?

There don't seem to be unsaved changes on either the source or target entries. To be totally sure I saved both (successfully) before starting. There don't see to be any validation errors either. I can save the entry normally without issue. Versioning is enabled, yes. A new version isn't created automatically after the translation is invoked - but I can save fine (albeit without the words actually displaying as translated).

JodebaDigitalPulse commented 6 months ago

Thank you @tigerchick, you can delete the attachment now.

Indeed, the validation may complete in the original Entry. But might still fail before the translated will be saved. I added logging for each validation error, and for the sidebar action there is a error popup in case of validation errors. Can you try version 1.5.3 please? Thx 🙏

tigerchick commented 6 months ago

I upgraded and ran it again. It's now showing an error though I'm not sure what the variable is that it refers to ("About Us" is the section name): Undefined variable $About Us

JodebaDigitalPulse commented 6 months ago

sorry, typo 🙃 can you update once more please, your validation errors were not actually logged.

tigerchick commented 6 months ago

Ah ha! Validation errors {"metaDescription":["Meta Description should contain at most 170 characters."]}

This probably solves it. I should have realised that of course translated values might be longer than the original - and therefore cause such an issue.

tigerchick commented 6 months ago

Yes, I removed that character limit from those two fields and now it works just fine. The validation errors / logs are a big help so thank you for implementing these - hopefully it will help other users too. Good job :)

JodebaDigitalPulse commented 6 months ago

Thanks, I'm glad it is resolved :) Thank you for your feedback!