1

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(&$form, $form_state) {
   $email_default = 'john@example.com';
   $form['additional_email'] = array(
      '#title' => t('Additional E-mail'),
      '#type' => 'textfield',
      '#default' => variable_get('additional_email', $email_default),
      '#description' => 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.

2

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(&$form, $form_state) {
   $form['additional_email'] = array(
      '#title' => t('Additional E-mail'),
      '#type' => 'textfield',
      '#default' => variable_get('additional_email', $email_default),
      '#description' => 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.

  • Thanks for your detailed response. The reason I have chosen this method is because this functionality is being added to a custom module on my site already. It is only useful so long as the module is in place, so I want the field to go away if the module is disabled. But also, I prefer that every submission override the previous, since the only time I need the value is as soon as the form is saved and Rules is triggered. After that, whether the same person is sent another notification is up to the user on a node-by-node basis. Please let me know if those reasons still don't justify this route! – Travis Mar 13 '17 at 19:05
  • Then you just need to add a submit handler, and call the function. Check the value and use valid_email_address before saving it with variable_set. – Kevin Mar 13 '17 at 19:07
  • I accidentally forgot to swap the last "myvariable" out. Thank you so much! Would you by any chance know why I'm still seeing an instance of the field inside the vertical tabs section AND outside? – Travis Mar 13 '17 at 19:21

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.