2

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?

2
  • 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? Commented Aug 7, 2014 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. Commented Aug 8, 2014 at 8:47

2 Answers 2

6

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.one().title }} - {{ object.userBooking.one().username }}

You need to use the one() 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.one().title} - {userBooking.one().username}
3
  • That shorthand syntax works perfectly, thank you! Commented Aug 8, 2014 at 8:45
  • This no longer seems to be working, as of Craft 4. Instead, {fieldName.one().title} is working for me.
    – tremby
    Commented Dec 15, 2023 at 7:05
  • Good note. That changed a while ago. I updated the answer to reflect the .one() syntax. Commented Dec 15, 2023 at 18:35
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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