1

I'm trying to get some auto translating done in a craft module. In order to do this I need to get the entry of another language in a save event. Is it possible to get this by language?

Event::on(
        Entry::class,
        Entry::EVENT_AFTER_SAVE,
        function(ModelEvent $event) {
            $entry = $event->sender;
            $isNew = $event->isNew;

            if ($isNew) {
                $enEntry = $entry->getEntryByLanguage('en');
            }
        }
    );
0

Something like this should work:

Event::on(
        Entry::class,
        Entry::EVENT_AFTER_SAVE,
        function(ModelEvent $event) {
            $entry = $event->sender;
            $isNew = $event->isNew;

            if ($isNew) {
                $enEntry = Entry::find()->id($entry->id)->site('enSiteHandle')->one();
            }
        }
    );

Just swap out enSiteHandle with your actual English site handle.

3
  • Thank you for you answer. When I try Entry::find()->id($entry->id) I get an object with NULL values. Jun 15 '20 at 18:05
  • You would need to do Entry::find()->id($entry->id)->one()
    – Brad Bell
    Jun 16 '20 at 0:57
  • I get NULL if I do this, so I guess this entry does not exist at this point yet. Jun 16 '20 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.