3

I am working on an add-on solution where I would like to create a channel entry and set a value to a module custom tab field for that entry.

Here's some code to share what I am up to:

$this->EE->load->library('api'); 
$this->EE->api->instantiate('channel_entries');

// Build data array
$data = array(          
    'author_id' => $member_id, // Member ID
    'title' => $title,
    'entry_date' => $date,
    'modl_giving_impact_member_opportunities__opportunity_id_token' => $opportunity_id_token, // Giving Opportunity token
    );

// Add new entry into channel
$this->EE->api_channel_entries->submit_new_entry($channel_id, $data);

Here's how I am attempting to target the module field:

modl_giving_impact_member_opportunities__opportunity_id_token

[module class name]__[field name]

Entry is being created but no luck with module field data being stored.

Anyway know how to properly include module data field data in my $data array?

1 Answer 1

2

Field/entry data isn't really related to a module in EE, so it never know what to do with what you've passed it. Fields aren't part of modules, their fieldtypes, so you may need to rethink your approach and include a fieldtype along with your module.

How is your data held? Is it cross referenced in another table, or are you storing in the normal way inside channel_data automatically?

If it's the normal straight forward way, then fields aren't referenced by their field name, but their field ID, typically "field_id_x", then there's a lookup to exp_channel_fields table on the field_id to get the name or reference name (in your case "opportunity_id_token").

Depending on where the data came from that you want to set you may need to hard code (or via settings) what the field is (either ID, or name and do a lookup to find the ID). Or you could do a lookup to see which field is associated with your fieldtype (assumes only 1 field would ever use your fieldtype), then add that reference to your array:

// Build data array
$data = array(          
    'author_id' => $member_id, // Member ID
    'title' => $title,
    'entry_date' => $date,
    'field_id_xx' => $opportunity_id_token, // Giving Opportunity token
);

Note: submit_new_entry is being depreciated as of EE v2.6, you should use save_entry instead. Ref There are also examples referencing the field IDs in that link.

If you want to associate data relating to an entry in your own table - so the Giving Opportunity token isn't shown on the Control Panel edit/publish entries pages, but kept separate - then this could be done purely in your module. All you'd need to do is once you've created the entry (with basic details) via the API, use:

$this->EE->api_channel_entries->entry_id;

To get the entry just created via the API. then save your module data in your own table cross-referencing the entry_id.

1
  • Peter, thank you. The get entry_id method is helpful. I also appreciate your thoughts on alternative ways to approach my solution. As is most often the case, many ways to get to a good result. I chose going with a module tab and in reviewing the submit_new_entry (I am aware of the move to save_entry and believe the same applies) method in the API, there seems to be code to address module fields (from module tabs) and in running some experiments came to an idea to try the [class]__[field_name] approach. Though as noted, did not work. Wondering if there is still a solution to my question.
    – SethG
    Commented Aug 18, 2013 at 14:08

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.