I'm writing a simple plugin for 'Craft 2' and It seems that a slug of an entry is returned if you reffer it like: $entry['slug'] but calling $entry->slug returns same value as entry title.

Is there an explanation of why it happens and what is the best practice of getting values of the entry fields with a homemade plugin?


God bless ya'll.

  • Usually $entry->slug is the correct way. $entry['slug'] will trigger the Arrayable trait and tries to cast your object into an array. Do you do something with your entry or do you change certain values? Is it right after you create a new entry or do you fetch it from your DB? Best practice is to use the object syntax -> $entry->slug – Robin Schambach Apr 8 '19 at 7:10
  • The idea is more like; build a JSON and then send it to another endpoint (call it REST API). And I wanna specify fields which I'm interested in, so they should be customizable. For this kind of scenario do you have a 100% working approach? – theVoogie Apr 8 '19 at 8:05
  • Yes I do. But you didn't answer my questions at all. It doesn't matter what you want to do with the data, all that matters is what you did before you call $entry['slug'] – Robin Schambach Apr 8 '19 at 8:18
  • Right. So entries are created some time ago, and yes I fetch em from DB based on some criteria, nothing fancy. – theVoogie Apr 8 '19 at 10:26
  • 1
    Under the hood Craft does many things automatically and converts many things automatically for you. $entry->slug should work for sure.. that's why I asked for your complete code. – Robin Schambach Apr 9 '19 at 10:14

Your Answer

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

Browse other questions tagged or ask your own question.