1

I need help with cloning an entry that has matrix fields for photos. Entries are made/edited using Channel Forms. The clone is generated using an extension to the channel_form_submit_entry_end hook

My Method:
public function generate($channel_form_obj) { $entry_id = ''; $quote_required = 0; ee()->security->restore_xid(); //I If the form has errors, don't bother if (!empty($channel_form_obj->errors) || !empty($channel_form_obj->field_errors)) { return; } // Grab the group ID $author_group_id = ee()->session->userdata('group_id'); // Forge the group ID - the contractors dont normally have quote authority ee()->session->userdata['group_id'] = 1; // Set the roofer's report to lower case before checking for the quote word $lc_roofer_report = strtolower( $channel_form_obj->entry('roofer_report') ); // Set the order status to lower case before checking for the quote word $lc_order_status = strtolower( $channel_form_obj->entry('order_status') ); // All repairs requiring further quotes have the word "required" either // in the roofer's report or the order status. Both entries are made using a slect list $reactive_category = $channel_form_obj->entry('categories')[0]; if ( preg_match("/\brequired\b/i", $lc_roofer_report) == 1 || preg_match("/\brequired\b/i", $lc_order_status) == ) { $quote_required = 1; } if($reactive_category == 1 && $quote_required == 1) { // Grab the data we need for the quote page and set spawning entry ID so we can find its spawn if we have an edit $data = array( 'title' => $channel_form_obj->entry('title'), 'field_id_50' => $channel_form_obj->entry('entry_id'), 'field_id_3' => $channel_form_obj->entry('customer_id'), 'field_id_37' => $channel_form_obj->entry('customer_reference'), 'field_id_5' => $channel_form_obj->entry('job_address'), 'field_id_40' => $channel_form_obj->entry('town'), 'field_id_30' => $channel_form_obj->entry('postcode'), 'field_id_27' => $channel_form_obj->entry('roof_type'), 'field_id_28' => $channel_form_obj->entry('roof_condition'), 'field_id_32' => $channel_form_obj->entry('job_photos'), // This causes the problem 'field_id_33' => $channel_form_obj->entry('quote_details'), 'field_id_34' => $channel_form_obj->entry('materials_and_plant'), 'field_id_46' => $channel_form_obj->entry('access'), 'field_id_41' => $channel_form_obj->entry('notepad'), 'author_id' => $channel_form_obj->entry('author_id'), 'category' => array(2), 'field_id_20' => 'Pending' ); // Set the entry_id to zero in case this is an entry or an edit not previously spawned, // otherwise ve heff crepp $data['entry_id'] = 0; $entry_id = 0; // If this is an edit, check if it is a re-edit of a previous spawn so we can correct // the spawned quote if($channel_form_obj->edit == 1) { $spawning_entry_id = $channel_form_obj->entry('entry_id'); ee()->db->select(); ee()->db->from('exp_channel_data'); ee()->db->where('exp_channel_data.field_id_50', $spawning_entry_id); $query = ee()->db->get(); if ($query->num_rows() > 1) { // Throw error message - too many spawns return; } elseif ($query->num_rows() == 1) { // We have a re-edit of a prevously spawned quote $row = $query->row_array(); $entry_id = $row['entry_id']; //echo 'Entry ID '.$entry_id.'
'; } } $group_id = $this->EE->session->userdata('group_id'); if (ee()->session->userdata('group_id') != '1') { //Error Can't Post
'; } else { // OK can post $channel_id = $channel_form_obj->entry('channel_id'); ee()->load->library('api'); ee()->api->instantiate('channel_entries'); ee()->api_channel_entries->save_entry($data, $channel_id, $entry_id, $autosave=false); } } return; }
This code generates the following warning:
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'is_draft'
Filename: matrix/ft.matrix.php
Line Number: 2373
And the matrix field entries are not cloned.
I obviously do not know how to clone the matrix fields in this situation.
EE is V2.7.3 and matrix V2.5.10 Can anyone point me at how to clone the the matrix fields?

2 Answers 2

1

Turnes out the $_POST data for my field has everything I needed, and I used it like this:

Instead of

'field_id_32' => $channel_form_obj->entry('job_photos')

I did:

'field_id_32' => $_POST['job_photos']

Makes me wonder why the channel form object did not have that in the first place?

1
  • Happy to hear you got it sorted! Nothing beats figuring it out yourself sometimes! Mar 24, 2014 at 15:04
0

Have you checked out Max Lazars' MX Cloner? The only thing I noticed when I used it is that it didn't work for me when testing locally as it (for whatever reason) didn't show up in the Extensions area. But worked fine when deployed. So you might or might not run into that.

4
  • MX Cloner is great. Been using it just fine, flawlessly, across upgrades, too.
    – notacouch
    Mar 20, 2014 at 22:14
  • Thanks Mark - MX Cloner does not do it for me - I need to clone from the front end. I hoped to learn how from MX Cloner code, but it is si highly abstracted WRT how it uses extensions that I got lost. Mar 21, 2014 at 9:43
  • Hmm I'm not sure how you are going to clone them from the front end. The only other way of doing that AND using Cloner is if you give logged in users a button that opens a lightbox to the necessary Content > Edit page. I do that for clients all the time and works well. Not sure how you'd go about it without that. You might have to ping the "big guns" in addon/extension development as that is a bit over my head :o) Mar 21, 2014 at 16:14
  • @JohndeBeer If looking at MX Cloner's code doesn't help I would start by taking a database dump before and after cloning an entry, then running a diff. It's got to be more than just duplicating the channel entry's row in the database on your own but to keep it simple and get it working for your own scenario... just try that with different channels/fields til you get a grasp for how to do a complete copy (may require rows in other tables such as categories, matrix, etc.). may not be the right way to go about it, but if it gets the job done...
    – notacouch
    Mar 23, 2014 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.