I am looking to modify a counter which is a field in a channel when i submit or update an entry. I am looking to count the number of matrix rows and then add that into the input field to act as the counter.

What is the best way for me to do this?

What hook should i use?

And what files should i create?


1 Answer 1


First - you can get this count in your template by using {my_matrix_field:total_rows} - but I'll assume you need the number stored as a value in a field for other purposes (use in orderby perhaps).

In which case - yes, you'd write an extension, and use the entry_submission_ready hook.

Then in your method, pass $data by reference, so you can modify it. Check to make sure you're in the channel you're looking for, then dump the $data array so you can see how the data is structured. From there you can figure out how to count your Matrix rows, and then set the value of $data['field_id_XX'] (which would be your "counter" field).

function entry_submission_ready($meta, &$data, $autosave)
    if($meta['channel_id'] == XX)
        // This is just to let you examine the data - you'd remove it after
  • Your correct. It is to use as a counter for the search parameter. Where you have put the print_r statement is this where I would get the matrix row count and then add it to the field, in this example the field is field_id_6. Does this function run and show and output to test? And does it automatically run in the background when live?
    – zizther
    Jul 26, 2013 at 17:37
  • Yeah, I can't recall off-hand what the Matrix field data will look like (hence the print_r() so you can examine it) - if you write and install an extension which uses the code on the hook mentioned, then yes, it will run automatically with each entry, and show you the output while you have the print_r() and exit()statements inside of it. (You'd want to remove them after you figure out your code.) Jul 26, 2013 at 19:04

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.