1

Newbie to Craft CMS. I am trying to send a push notification to an app when an entry of specific type (NewsItem) is saved, and I'm doing so via a Craft Module. I got this code snippet from another StackExchange question, and I have changed it a bit to implement it according to my needs:

        Craft::$app->elements->on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {

            // Make sure it's an entry.
            if ($event->element instanceof craft\elements\Entry) {
                $entry = $event->element;

                // Check if it's in the section you care about
                if ($entry->sectionId == 1) {
                    // Do your custom logic

                        if(isset($entry->photo))
                            $image_name = $entry->photo->getUrl();
                        else
                            $image_name = 'http://img.youtube.com/vi/' . $entry->youtubeVideoCode . '/0.jpg';

I'm getting this error:

Calling unknown method: craft\elements\db\AssetQuery::getUrl()

I may be wrong, but I'm assuming I have to cast $entry to (NewsItem) somewhere in the code to access the getUrl() function? How and where do I do that?

Thanks in advance for your help.

1

The actual answer is mostly unrelated to your question, but I wanted to provide some context, first!

Entry Types don't get their own PHP classes. All Entries are just instances of the craft\elements\Entry class, distinguished by their section ($entry->getSection()) and type ($entry->getType()) values.

If you need to check the section and type of your entry, you can compare against the sectionId and typeId properties. Keep in mind that these IDs may be different, across environments, so it's better to use the handle and keep it in sync with your project.yml:

if ($entry->getSection()->handle == 'mySectionHandle' && $entry->getType()->handle == 'myEntryTypeHandle') {
  // Your logic!
}

Ok, now on to the error you're seeing!

Calling unknown method: craft\elements\db\AssetQuery::getUrl()

This is actually telling you that the accessed field/property (photo) returns an object that doesn't have a getUrl() method.

Unless relational field data is eager-loaded, accessing its property will return an ElementQuery (in your case, an AssetQuery), prepped to return the related Elements.

So, before accessing methods on the expected value (an Asset Element), you have to execute the query:

$photo = $entry->photo->one();

if ($photo) {
  // At least one photo was attached! You can safely access the Asset object's properties + methods:
  $url = $photo->getUrl();
} else {
  // Set a default:
  $url = 'https://...';
}
| improve this answer | |
  • Understood well & working perfectly. Thanks v.much for providing the detailed context. Marking your answer as the correct one. Thanks again! – nparabz Apr 26 at 4:17
  • Hi again. I'm facing a new issue with Craft. Since you are knowledgeable about Craft, maybe you could help me out with this one too? Thanks in advance. craftcms.stackexchange.com/questions/35208/… – nparabz Apr 26 at 4:45

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.