2

I've got a Matrix field called Emergency Contacts (handle: emergencyContact) that I'm setting up so that users can add/update on the front end. It has one block type - contact - with 4 fields:

  • Name (Plain Text - handle: contactName)
  • Relationship (Dropdown - handle: contactRelationship)
  • Phone (Input Mask - handle: contactPhone)
  • Email (Plain Text - handle: contactEmail)

I have everything displaying correct when I show existing information. However, when they go to enter a new emergency contact, I can't figure out the right syntax to populate the dropdown menu to populate with the options for them to select.

Below is my code. I've tried several different things, most of which gave me an error. This is my latest attempt.

<input type="hidden" name="fields[emergencyContact][new1][type]" value="contact">
<input type="hidden" name="fields[emergencyContact][new1][enabled]" value="1">
<div class="grid-x grid-padding-x">
    <fieldset class="cell small-3">
        <label for="contactName">Name</label><input type="text" name="fields[emergencyContact][new1][fields][contactName]">
    </fieldset>
    <fieldset class="cell small-3">
        <label for="contactRelationship">Relationship</label>
        <select name="fields[emergencyContact][new1][fields][contactRelationship]">
        {% for option in contactRelationship.options %}
            <option value="{{ option.value }}">{{ option.label }}</option>
        {% endfor %}
        </select>
    </fieldset>
    <fieldset class="cell small-3">
        <label for="contactPhone">Phone</label><input type="text" name="fields[emergencyContact][new1][fields][contactPhone]">
    </fieldset>
    <fieldset class="cell small-3">
        <label for="contactEmail">Email</label><input type="email" name="fields[emergencyContact][new1][fields][contactEmail]">
    </fieldset>
</div>

Anyone able to help me get this resolved?

  • Maybe you could tell us what happens with your current code. Does craft not save the contactRelationship field but the rest? Do you receive any errors or does it store the wrong value? Does it store nothing at all? Is contactRelationship the field with the correct options attribute or do you have issues with rendering the dropdown in the first place? – Robin Schambach Nov 27 '17 at 15:37
  • With the above code, I get an error that "Variable "contactRelationship" does not exist." – Todd Nov 27 '17 at 17:02
  • 1
    During the render process? Of course because the variable does not seem to be defined. You have to get the field by id or handle something like set contactRelationship = craft.field.getFieldById(id) I'm at my smartphone currently so maybe the syntax is wrong but it should be something like that. Edit stupid me since it is in a matrix you can get the field only by id – Robin Schambach Nov 27 '17 at 17:08
  • So now I'm using this - {% set dropdownField = craft.fields.getFieldByHandle('contactRelationship') %} {% for option in dropdownField.settings.options %} <option value="{{ option.value }}">{{ option.label }}</option> {% endfor %} and I get an error message - "Impossible to access an attribute ("settings") on a null variable." – Todd Nov 27 '17 at 23:57
  • Like I said you can't get the field by handle inside a matrix element and you have to fetch the dropDown field, not your matrix field contactRelationship = craft.fields.getFieldById(yourDateTimeFieldId) is what you want – Robin Schambach Nov 28 '17 at 13:17
3

Your Code has two errors: 1. was missing. 2. The way you were fetching options was wrong.

Here is the correct code:

<input type="hidden" name="fields[emergencyContact]" value="">
<input type="hidden" name="fields[emergencyContact][new1][type]" value="contact">
<input type="hidden" name="fields[emergencyContact][new1][enabled]" value="1">
<div class="grid-x grid-padding-x">
    <fieldset class="cell small-3">
        <label for="contactName">Name</label><input type="text" name="fields[emergencyContact][new1][fields][contactName]">
    </fieldset>
    <fieldset class="cell small-3">
        <label for="contactRelationship">Relationship</label>
        {% set matrixField = craft.fields.getFieldByHandle('emergencyContact') %}
        {% set blockTypes = matrixField.getFieldType.getSettings.getBlockTypes %}
            {% for blockType in blockTypes %}
                {% if blockType.handle == "contact" %}
                     {% for field in blockType.getFields() %}
                       {% if field.handle == "contactRelationship" %}
                          <select>
                          {% for option in field.settings.options %}
                             <option value="{{ option.value }}">{{ option.label }}</option>
                           {% endfor %}
                           </select>
                        {% endif %}
                      {% endfor %}
                 {% endif %}
              {% endfor %}
    </fieldset>
    <fieldset class="cell small-3">
        <label for="contactPhone">Phone</label><input type="text" name="fields[emergencyContact][new1][fields][contactPhone]">
    </fieldset>
    <fieldset class="cell small-3">
        <label for="contactEmail">Email</label><input type="email" name="fields[emergencyContact][new1][fields][contactEmail]">
    </fieldset>

I hope it helps you.

  • Thank you. I had finally come up with a working solution similar to what you presented, but ended up using yours instead. Much appreciated! – Todd Nov 28 '17 at 15:52
1

I don't think I have a specific answer, but I can point you to a few posts that I think will help get you rolling. How are you creating a new row for your matrix field?

  • Right now, there are blank fields on the page where the new row would get entered. I haven't done any additional JS/jQuery until I can get everything else working. – Todd Nov 28 '17 at 0:01

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.