3

I have a Structure section fruit with a single entry type fruit.

I also have a Category fruitType with 2 levels:

  • Citrus
    • Orange
    • Lemon
    • Lime
  • Melons
    • Watermelon
    • Honeydew

The entry type fruit has a Category field for fruitType.

If I select a level 2 category for the fruitType field, say Orange:

Category Selection

Craft CMS dismisses the category selection pop-up and the parent category is shown instead, in this case Citrus.

Resulting Category

From the browser's network inspector, I noticed that a POST request to /admin/actions/elements/getCategoriesInputHtml with the following form data:

categoryIds[]:893
locale:en_us
id:fields-fruitCategory
name:fields[fruitCategory]
limit:1
selectionLabel:Category

However, the response from the server is:

{"html":"<input type=\"hidden\" name=\"fields[fruitCategory]\" value=\"\">\n\n<div id=\"fields-fruitCategory\" class=\"categoriesfield\">\n\t<ul class=\"elements structure\">\n\t\t\t\t\t<li id=\"fields-fruitCategory-category-887\">\n\t\t\t\t\t\t\t\t<div class=\"row\" style=\"margin-left: -0px; padding-left: 0px;\"><div class=\"element removable\" data-id=\"887\" data-locale=\"en_us\" data-status=\"enabled\" data-label=\"Faculty\" data-url=\"http:\/\/craft.local\/fruit\/citrus\" data-level=\"1\" data-editable><input type=\"hidden\" name=\"fields[fruitCategory][]\" value=\"887\"><a class=\"delete icon\" title=\"Remove\"><\/a> <div class=\"label\"><span class=\"status enabled\"><\/span><span class=\"title\">Citrus<\/span><\/div><\/div><\/div>\n\n\t\t\t\t\t\t\t<\/li>\n\t\t\t<\/ul>\n\n\t<div class=\"btn add icon dashed\">Category<\/div>\n<\/div>\n\n"}

Based on the response and what's rendered in the interface, Citrus is shown and not Orange, despite Orange being selected.

1 Answer 1

4

Unlike structured entries, when you relate a category to another element, its ancestors are also included in the relation. Due to the way Craft looks up these ancestors when relating the selected categories, to enforce this behaviour, the ancestors are added to the relation before the one you selected, and it looks like you're limiting that Categories field to 1, so only the parent is being added.

Try setting the limit on that Categories field to 2.

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.