1

I have my users setup with a category field that links to a category of countries and each country has states/provinces.

I have my form setup to only display the states listed under United States. The plugin I created saves this data as expected.

The problem I'm having is accessing the state which was saved for the user. This is my current code. It works BUT there has to be a better way:

{% if user.addressMatrix | length %}

    {% for addressInfo in user.addressMatrix %}

        {% set cat = addressInfo.region[1] %}

        {{ cat.title }}

   {% endfor %}

{% endif %}

Please advise and thanks in advance.

  • 1
    The reason this is complex is because you are storing the address info in a matrix field. If you only need one address you might consider creating individual fields (i.e. address, city, region, postCode, etc)—you could then access the region using {{ user.region[0].title }}. Otherwise, I think your solution is the way to do it (although you can remove the 'if' conditional; if the matrix is empty then the 'for' loop won't execute anyway). – Douglas McDonald Apr 27 '15 at 17:24
  • I don't see what a matrix field has to do with anything. My category field is a regular old category field with the matrix. Accessing the data should be the same process as if it were standalone. – Matt V Apr 27 '15 at 17:49
  • I'm assuming that 'addressMatrix' is a matrix field? With a category field called 'region' defined in one of the blockTypes? – Douglas McDonald Apr 27 '15 at 17:59
  • Correct. I just now realized I didn't list the block in the above code. I was trying to provide a simplified version of the template for readability sake. Yes 'region' is a category field in a block, in a matrix. What I don't like about how I'm outputting the title is I have no idea if the thing I'm outputting is a child, parent, child of a child, etc. There is no context. – Matt V Apr 27 '15 at 18:13
1

Ok so I figured out the issue. I had the category field setup to have a limit of 1, which I assumed would be ok. My thinking was that you're only choosing 1 state. But because state is a sub category of country I need to allow for country and state AKA a limit of 2. Once I did this I was able to structure my template accordingly:

{% if user.addressMatrix | length %}

    {% for addressInfo in user.addressMatrix %}

         {% if addressInfo.type == "address" %}

            {% for item in addressInfo.region %}

                {% if item.level == 2 %}

                {{ item.title }}

                {% endif %}

            {% endfor %}

        {% endif %}

   {% endfor %}

{% endif %}

I think that future releases of Craft ought to have more options for category fields, like: Restricting selections to a sub category, limiting the number of sub categories.

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.