3

Working with Channel form, I have encountered some problems when working with multiple Matrix fields in a single edit form.

[EE v2.7.3 and Matrix 2.5.10]

  1. When I work with "native" channel form fields {field:field_name} everything works fine and I can edit all matrix fields and post data.

  2. When I try to use the "custom code" below (example on MetaQ), only the last Matrix field is working. When viewing source, the problem seems to be that all matrix fields before the last one get the {row_id} and the number of rows of the last Matrix in the form (the one with the highest order value in the field group they both belong to).

The template :


<!DOCTYPE html >
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="fr"> <!--<![endif]-->
    <head>

    </head>
    <body>
    {exp:channel:form  channel="test" entry_id="34"}

     <label for="title">Title</label>
    <input type="text" name="title" id="title" value="{title}" size="50" maxlength="100" onkeyup="liveUrlTitle();">

    <label for="url_title">URL Title</label>
    <input type="text" name="url_title" id="url_title" value="{url_title}" maxlength="75" size="50">


    {cf_couleur}
    <div class="field"><!--THIS IS A MATRIX ROW-->
      <input type="hidden" name="cf_couleur[row_order][]" value="row_id_{row_id}" />
      <input type="text" id="cf_couleur" name="cf_couleur[row_id_{row_id}][col_id_26]" value="{couleur1}">
    </div>
  {/cf_couleur}

   {cf_forme}
 <div class="field"><!--THIS IS A MATRIX ROW-->
      <input type="hidden" name="cf_forme[row_order][]" value="row_id_{row_id}" />
      <input type="text" id="cf_forme" name="cf_forme[row_id_{row_id}][col_id_27]" value="{form1}">
    </div>
  {/cf_forme}
    {/exp:channel:form}

   </body>
</html>

The matrix generated in the browser :


<div class="field">
  <!--THIS IS A MATRIX ROW-->
  <input type="hidden" name="cf_couleur[row_order][]" value="row_id_184" />
  <input type="text" id="cf_couleur" name="cf_couleur[row_id_184][col_id_26]" value="{couleur1}">
</div>

<div class="field">
  <!--THIS IS A MATRIX ROW-->
  <input type="hidden" name="cf_couleur[row_order][]" value="row_id_185" />
  <input type="text" id="cf_couleur" name="cf_couleur[row_id_185][col_id_26]" value="{couleur1}">
</div>

<div class="field">
  <!--THIS IS A MATRIX ROW-->
  <input type="hidden" name="cf_forme[row_order][]" value="row_id_184" />
  <input type="text" id="cf_forme" name="cf_forme[row_id_184][col_id_27]" value="cube">
</div>

<div class="field">
  <!--THIS IS A MATRIX ROW-->
  <input type="hidden" name="cf_forme[row_order][]" value="row_id_185" />
  <input type="text" id="cf_forme" name="cf_forme[row_id_185][col_id_27]" value="carrer">
</div>

The code above is a very simple form that I have done to try to narrow down the problem. On channel ad two matrix fields with just 1 text field in each of them.

I have compared my custom code and the code generated by the native channel form field and cannot see any difference in the field names.

I have then simply put every matrix field in their own embeds, passing the ID of the entry I am editing to the {exp:channel:entries} loop around each of them. That works fine, even with my custom code.

I would very much like to understand what is happening here, as I would rather have everything in the form and not make use of embeds.

2 Answers 2

3

So, a bright (Latvian) mind was kind enough to look into this. It seems the problem lies with ExpressionEngine code on this one.

Embeds or Stash will work just fine but you can also patch EE as follow:

File: system/expressionengine/modules/channel/libraries/channel_form/Channel_form_lib.php

Method: replace_tag

Find this:

ee()->api_channel_fields->apply('_init', array(array(
    'row' => $this->entry,
    'content_id' => $this->entry('entry_id')
)));

Replace with:

$field_id = $this->get_field_id($field_name);
ee()->api_channel_fields->apply('_init', array(array(
    'row' => $this->entry,
    'content_id' => $this->entry('entry_id'),
    'field_id' => $field_id
)));

That fixes the problem. Tested in EE 2.8.1 with Matrix 2.5.10

Will post bug report.

1
0

I believe the matrix row_id should be unique, so the problem might be where you have 2 matrix fields outputting the same row_id values (184 and 185 for both fields). Try looking in the db in exp_matrix_data for entry_id 34 and see what the row_id values are.

I would try using the var_prefix parameter for your matrix fields and see if that corrects the output, like:

var_prefix="mx1" and var_prefix="mx2"

Then you would have

{cf_couleur var_prefix="mx1"}
  <div class="field"><!--THIS IS A MATRIX ROW-->
    <input type="hidden" name="cf_couleur[row_order][]" value="row_id_{mx1:row_id}" />
    <input type="text" id="cf_couleur" name="cf_couleur[row_id_{mx1:row_id}][col_id_26]" value="{mx1:couleur1}">
  </div>
{/cf_couleur}

{cf_forme var_prefix="mx2"}
  <div class="field"><!--THIS IS A MATRIX ROW-->
    <input type="hidden" name="cf_forme[row_order][]" value="row_id_{mx2:row_id}" />
    <input type="text" id="cf_forme" name="cf_forme[row_id_{mx2:row_id}][col_id_27]" value="{mx2:form1}">
  </div>
{/cf_forme}
5
  • I believe you are definitely on the right track when saying the row_id are not unique / individualised. I'll let them reply but I suggested the var_prefix thing to the students and they told me it didn't correct the output (did not have time to test it in a sandbox of my own). Just to be on the safe side I also suggested appending "random" everywhere {row_id} was present {row_id random} to avoid EE caching but no dice either. Commented Apr 29, 2014 at 19:05
  • Tested it on a sandbox and I remembered right. Adding var_prefix does not help in this case. the row_id values of the first matrix field are still the wrong ones (the one corresponding to whatever matrix has the highest "order" in the field group both matrix fields are in) Commented Apr 29, 2014 at 21:23
  • I can only get it to work if I put at least one of the matrix field in its own embed Commented Apr 29, 2014 at 21:28
  • Maybe grabbing the row_ids first with channel entries and using stash to output in the channel form would save an embed. Not sure why the funkiness. Commented Apr 29, 2014 at 22:42
  • Yep, stash in definitely an option but I really feel this should work, though. It does when using the default channel form fields {field:cf_couleur} and {field:cf_forme} ... thanks for your inut anyway. Hope the P&T guys will be able to shed some light on this. Commented Apr 30, 2014 at 4:28

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.