I want to load a field form belong to the entity,and update the value

For example, i want to let the user change his email field value independently,

this is say, the user can visit a page to see only the email field form,and update the value,

or just load the user's introduction field form,and save it to update the user's introduction field value.

or just load a node's og group field form, and update the node's og group value.not need to manually build a new form and to validate and submit the value.

I mean not need to manually build a new form and new validate and new submit to handle the data,just load the user email form ,and save it to update the user's email value.

How can i do like that?thanks for all advise!!!It can save my time so much!!!

Comments

nevets’s picture

That is not a feature of Drupal, you would need custom forms for such functionality.

Jaypan’s picture

When you create the form, you need to pass it a complete $account object:

$form = drupal_get_form('some_email_form', $account);

Then, in your form definition, you pass the $account object through to your submit function:

function some_email_form($form, &$form_state, $account)
{
  $form['#account'] = $account;
  $form['email'] = array
  (
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#default_value' => $account->mail,
  );
  $form['submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Save email'),
  );
  return $form;
}

function some_email_form_validate($form, &$form_state, $account)
{
  if(!valid_email_address($form_state['values']['email']))
  {
    form_set_error('email', t('That is not a valid email address'));
  }
}

function some_email_form_submit($form, &$form_state, $account)
{
  $account = $form['#account'];
  $new_values['mail'] = $form_state['values']['email'];
  user_save($account, $new_values);
}
315redad’s picture

Thanks so much for your advise,Jaypan

You show me a clean entire way to do it, it is a good idea,

yesterday i google a another way to do it like below, and it can save many time when solve on complex field form ,what do you think about this?

function my_awesome_form($form, $form_state, $node) {
  // ... add your fields and wrappers here

  // This is necessary for field_default_form() not to yell at us.
  $form['#parents'] = array();

  // These variables are needed to build field forms.
  $entity_type = 'node'; // Substitute with any entity type.
  $bundle_name = 'foo'; // Substitute with the entity's bundle.
  $entity = $node; // Not really necessary but we keep the parameter names consistent with the field_default_form() signature 
  $langcode = LANGUAGE_NONE; // Substitute as appropriate.

  // This is specific to the field you want to add.
  $field_name = 'field_address'; // Substitute with the field name you want to return a form for.

  // $items is used to provide default values for the field. You can set it to NULL or an empty array if desired.
  $items = field_get_items($entity_type, $entity, $field_name);

  // We need the field and field instance information to pass along to field_default_form().
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);

  // This is where the cool stuff happens.
  $field_form = field_default_form($entity_type, $entity, $field, $instance, $langcode, $items, $form, $form_state);

  // Boom! Now $field_form contains an array with the fully-built FAPI array for the address field!

  // Adding it to your form is pretty simple:
  $form += $field_form;

  // Note that if you want to alter anything or insert it into a container or fieldset that's fine too.

  // ... add a submit button and/or other fields here
}

but now,when i add a submit button,it locate above the field form,how to make it under the field form?

Jaypan’s picture

You can do that as well. I prefer my method, but both will (should) work.

315redad’s picture

Thanks for you reply so quickly,
but now,when i add a submit button,it locate above the field form,how to make it under the field form?

Jaypan’s picture

I don't see a submit button in your code.

But you can probably change it using #weight.

315redad’s picture

function my_awesome_form($form, $form_state, $node) {
  // ... add your fields and wrappers here

  // This is necessary for field_default_form() not to yell at us.
  $form['#parents'] = array();

  // These variables are needed to build field forms.
  $entity_type = 'node'; // Substitute with any entity type.
  $bundle_name = 'foo'; // Substitute with the entity's bundle.
  $entity = $node; // Not really necessary but we keep the parameter names consistent with the field_default_form() signature 
  $langcode = LANGUAGE_NONE; // Substitute as appropriate.

  // This is specific to the field you want to add.
  $field_name = 'field_address'; // Substitute with the field name you want to return a form for.

  // $items is used to provide default values for the field. You can set it to NULL or an empty array if desired.
  $items = field_get_items($entity_type, $entity, $field_name);

  // We need the field and field instance information to pass along to field_default_form().
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);

  // This is where the cool stuff happens.
  $field_form = field_default_form($entity_type, $entity, $field, $instance, $langcode, $items, $form, $form_state);

  // Boom! Now $field_form contains an array with the fully-built FAPI array for the address field!

  // Adding it to your form is pretty simple:
  $form += $field_form;

  // Note that if you want to alter anything or insert it into a container or fieldset that's fine too.

  // ... add a submit button and/or other fields here
  $form['submit'] = array(
     '#type' => 'submit',
    '#value' => $account->name,     
   );
}

this is my code with submit button,
the button will display above the field form,how to put it under the field form?
i user form['weight'],but it seems don't work.

315redad’s picture

haha,thanks so so so much,

It work!

with #weight property add to it

$form['submit'] = array(
     '#type' => 'submit',
    '#value' => $account->name,
    '#weight' => 20,    
   );
315redad’s picture

Thank you so much, Jaypan, with my full heart gratitude !you save my so a lot of time.

1.module_load_include('inc', 'comment', 'comment.pages');

2.clean entire way to do field form and update value

3.form #weight,ever make me thought of how to solve it.

kalidasan’s picture

++