How to use hook_form_BASE_FORM_ID_alter() to save variable? I am attempting to use hook_form_BASE_FORM_ID_alter() to 1) add a field to the node edit form, which then is 2) turned into a token, and 3) used in Rules. This question is only about the first part of this process.

The following code is in place. It creates a field, but nothing is getting saved to the variable in question, additional_email, after the node edit form is submitted. I know I'm doing something very wrong here, but I don't know what. 

How can I save the variable entered by the user to be accessed by a different function in my module?

Edit Form Code

function MYMODULE_form_node_form_alter(&amp;$form, $form_state) {
   $email_default = 'john@example.com';
   $form['additional_email'] = array(
      '#title' =&gt; t('Additional E-mail'),
      '#type' =&gt; 'textfield',
      '#default' =&gt; variable_get('additional_email', $email_default),
      '#description' =&gt; t('If you would like to notify an additional person, please supply the e-mail address.'),
   );

   $form['options']['additional_email'] = $form['additional_email'];
   $form['options']['additional_email']['#title'] = t('Additional Notification'); 

   $form['#submit'][] = 'MYMODULE_submit';
}

function MYMODULE_submit($form, $form_state) {
   if (isset($form_state['values']['additional_email'])) {
      variable_set('myvariable', $form_state['values']['additional_email']);
   }
 }


Token Creation Code

 function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
    ...
    $notify_email = variable_get('additional_email', '0');
    $concat_array = implode(' ',$notify_email);
    $section_users['add_email'] = $concat_array;
    ...
  }


Update: I added a submit handler, shown above (and clarified the variable I accidentally omitted when copying the code). Unfortunately, I'm still getting the old garbled array that was somehow saved in there previously.

The code which assigns the variable's value to the token is above, in case that's the problem.
return system_settings_form($form); will do nothing in this context. An alter function like this has no return.

What you should do if you want to save the value is add a submit handler into the form instead:

function MYMODULE_form_node_form_alter(&amp;$form, $form_state) {
   $form['additional_email'] = array(
      '#title' =&gt; t('Additional E-mail'),
      '#type' =&gt; 'textfield',
      '#default' =&gt; variable_get('additional_email', $email_default),
      '#description' =&gt; t('If you would like to notify an additional person, please supply the e-mail address.'),
   );

   $form['options']['additional_email'] = $form['additional_email'];
   $form['options']['additional_email']['#title'] = t('Additional Notification'); 

   $form['submit'][] = 'mymodule_submit';
}

function mymodule_submit($form, $form_state) {
  if (isset($form_state['values']['additional_email'])) {
    variable_set('additional_email', $form_state['values']['additional_email']);
  }
}


This adds a new submit handler to the form, which will trigger upon submit. If a value was supplied, it will set the variable.

What is $email_default? That is never defined.

However I would suggest a different approach, because:


Variable can only hold one value, and this form item exists on every node. Every submission with a value will override the previous.
Rules can read fields, why not add an email field on the node, and use that in the Rule
You should do extra validation on the entered value to make sure it is a legit email value and not just any string with valid_email_address
You can default the email field to be the $email_default you are checking for
You could add cardinality for the email field and adjust the Rule if multiple people need to be notified


If you didn't use a custom form field, and instead added a real field on the node, you could ensure that any node can have any email address without relying on variable_set which as stated, will only ever allow one value to exist.

Rules is able to inspect your node and fetch a field value from it in order to use as a data variable in a later action, in this case, to send an email. If you do that, you don't really need any code at all.