1

I'm using a row layout SuperTable to create a recipe (ingredients are entries). Is there a way to show another field inside the related entry (not just the title) together with the entry title? I need to specify the measure (like tbsp) otherwise it's hard to know the quantity for what you're editing.

supertable connected entries

I need to show Spinach (100g) in the ingredient row. The 100g is a field called ingredientMeasure inside the ingredient entry.

ingredient entry

I was thinking about using a hook (modifyEntryTableAttributes) - but not sure how to do it with ST. This is my hook example (from Ben's plugin tutorial):

// ADD A HOOK - modify the contents of table cell on entry index page
public function getEntryTableAttributeHtml(EntryModel $entry, $attribute) {
    // check if setting is enabled and we're editing our column
    if ($this->getSettings()->showCountOnEntryIndex AND $attribute == 'count')
    {
        return 'C<b>' . craft()->entryCount->getCount($entry->id)->count . '</b>';
    }
}
3

Basically, what you want to do isn't possible – there's no hook for modifying the content in a relational field's input HTML.

The getEntryTableAttributeHtml hook is meant for element indexes (such as the entry list displayed at /admin/settings/entries), not relational fields (such as Entries, which is what you're using in your SuperTable).

The easiest workaround would be to add the value of the ingredientMeasure field to your ingredient entries' title format.

First, you'll need to create a new custom field for your entries' titles – the handle title is reserved, so call it ingredientTitle or the like, and add it to your "Ingredients" section's entry type. Make the field required. Second, you'll need to go into your "Ingredients" section's entry type settings, and uncheck the "Show the Title field" checkbox. Then, add something like this to "Title format" field:

{ingredientTitle} ({ingredientMeasure})

Finally – you'll need to re-save all your ingredient entries in order to have their titles updated to the new format.

Obviously, the big tradeoff with this method is that your ingredient won't actually be titled "Spinach" anymore – it'll be titled something like "Spinach (100g)". In your templates, this can easily be remedied by using

<h1>{{ entry.ingredientTitle }}</h1>

Instead of the normal entry.title, wherever appropriate.

Also, unless you want the ingredientsMeasure field to be part of the Ingredient entries' slugs, you should replace {slug} in your Ingredients Section's URL format with {ingredientsTitle|kebab}.

  • Thanks Mats. This approach works fine - I was thinking about it at first but thought a hook would be DRY-er.It works for now but many-to-many relationships with fields in the connecting table still feel a bit hacky in Craft - it would be good if fields could be added to relations as well (eg. when an asset is linked, it gets "link fields"). – Jure Srpcic Oct 23 '16 at 8:08

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.