If a booking channel contains two Entries fields (one related to a single User, one related to a single 'Event'), how can I get the booking entry title to be set automatically to the following syntax:

"Event Date - Username"

I've set up the entry to not show the title field, but my syntax is wrong. I'm using:

{{eventBooking.title}} - {{userBooking.username}}

but all I'm getting is "-", so it's obviously not recognising the fields. Is this possible?

  • Are you actually trying to set an entry's title or are you trying to display a title on a page as a combination of those values? Aug 7 '14 at 16:08
  • 1
    I was trying to set the entry's title based on those values when the entry gets created. This has been answered by Ben now below. Aug 8 '14 at 8:47

I believe this is in regards to the Title Format feature, so just to be explicit in what I'm referring to, Craft Documentation calls this a Dynamic Entry Title.

The syntax you are looking for can take two forms:

1) Long form object syntax

Just as on the front end your entry is available via the {{ entry }} variable, within your Title pattern on the back end, the current entry is available via the {{ object }} variable.

So I believe the following should work, assuming you are using the actual field names:

{{ object.eventBooking.first().title }} - {{ object.userBooking.first().username }}

You need to use the first() method here because Relations fields return an array by default. Since you need to output your content directly, you need to make sure you are accessing the title and username properties via the Model they are associated with in that array. If your fields are required, you can safely assume that the first item will exist in the array.

2) Short hand syntax

If what you are doing is simple, Craft gives you the option to make the syntax a bit easier and omit the object variable. So the above could also be written with single curly braces as such:

{eventBooking.first().title} - {userBooking.first().username}
  • That shorthand syntax works perfectly, thank you! Aug 8 '14 at 8:45

If you are on an entry page have you tried:

{{ entry.eventBooking.title }}

Because at the moment it doesn't look like your telling craft to look at the eventBooking handle on the entry but the title on a eventBooking variable

  • I understand what you're saying and it makes sense, but no I still just get "-" Aug 7 '14 at 16:02
  • You may need to specify the .first() related entry. {{entry.eventBooking.first().title}} Aug 7 '14 at 16:14

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.