0

I am very new to Drupal and trying to write a module. I have 2 entities - Membership and MembershipRequest and both are fieldable. A MembershipRequest is a request to the admin of the website to create or update (renew/delete) a Membership entity. On the MembeshipRequest form, I want to populate the form elements with Membership entity values that user are requesting to update. For example, on the request form's First name text box, I want to populate the Membership's First name. I have membership_id property in the the request entity. But the First name and other fields are attached to both entities via Field API.

I am not sure which object (and how) - $form, $form_state, $request - I need to modify so that the request form will show up with populated values.

Thanks a lot.

  • Assuming your MembershipRequest entity is already referencing a Membership entity by ID, why not just display the referenced entity when the request is being viewed? – othermachines Jun 24 '15 at 17:50
  • Yes, I am displaying it. But on the request form, I just want to populate values from membership entity so that user doesn't have to fill out the whole form. – vabii Jun 24 '15 at 17:54
  • Is the user able to request changes to specific fields? From the OP it sounds like the request is only to create or delete, not to modify. – othermachines Jun 24 '15 at 18:02
  • No, the user can request to update also (any field) – vabii Jun 24 '15 at 18:04
2

If you insist on programming this, and not use and configure contrib modules -

If fields were added to entity bundle via drupal admin UI

You can use hook_form_alter() or hook_form_FORM_ID_alter, and in the code there have the particular field populated with desired default value.

  • hook_form_alter() would be invoked for all forms and you would have to have an if condition to target just the desired form id.
  • hook_form_FORM_ID_alter() would be invoked for particular form.

If field was attached using Field API code (not the case for this question)

You would have an implementation of hook_field_attach_form() to attach fields to your MembeshipRequest entity. It would be in that hook implementation that you would set a #default_value to the desired form field.

Code to use in either of the cases above

$form['foo']['bar'] = array(
  '#type' => 'textfield', 
  '#title' => t('foobar'), 
  '#default_value' => _func_to_get_default_value($member_id), 
  '#description' => t('something something'),
  '#size' => 60, 
);  
// NOTE: Replace foo/bar with the particular field etc. 
| improve this answer | |
  • It does say 'programatically' in the title - my bad. This is a better answer. – othermachines Jun 24 '15 at 19:47
  • Thanks, could you also tell how to set the default value of the field if the field is attached using field api? I made both entities fieldable and added firstname, lastname, email, etc. using the fields UI (thought that would be easy because then I wouldn't have to program the form code) :-( – vabii Jun 24 '15 at 20:00
  • I have updated the answer. A simple hook form alter should do in that case. – Sumeet Pareek Jun 24 '15 at 20:16
  • @SumeetPareek, thanks. I tried the above code and it works. The only problem is now I have to mention #type, #title, '#description' and all properties for all fields in the code again. If I only set #default_value, the field does not show up in the form. I used the admin UI to set up the fields so that the site admin can attach/remove fields without me touching the code. For now, I have put $form[$key][LANGUAGE_NONE][0]['value']['#default_value'] = $arrayMember[$key]['und'][0]['value']; in the code. Not sure if it will work for all the fields. – vabii Jun 24 '15 at 21:27
0

Have a look at Entity reference autofill.

The Entity reference autofill module gives Entity reference fields an option to populate other form fields with data from selected referenced entities.

| improve this answer | |
  • I am programming all of this so want to know I can modify ($form/$form_state/$request) to populate the form from the values of membership entity. – vabii Jun 24 '15 at 18:54

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.