1

I have a custom module that sends an email with an order to the customer. I hardcoded the email body text in a string inside the module code but my client want to have control over it and I need to create an admin page for him: enter image description here

I need to save it and use that text as a variable.

I started the page like this:

function shop_admin_settings($form, &$form_state) {
     $form = array();
     $form['mail_template'] = array(
       '#type' => 'fieldset',
       '#title' => t('Shop Email Template'),
       '#colapsed' => FALSE,
       '#collapsible' => TRUE,  
     );

     $form['mail_template']['body'] = array(
       '#type' => 'textarea',
       '#title'  => t('Email template'),
       '#rows' => 10,  
     );
     return system_settings_form($form);
 }

and use hook_mail_alter to append $form['mail_template']['body'] to the existing email body

function shop_mail_alter(&$message, $form_state) {
$client_body = $form_state['mail_template']['body']
if (is_array($message['body'])) {
    $message['body'][] .= $client_body;
  }
}

Now if you please help me, I am a bit stuck:

  1. I need to retrieve the body provided by client, I don't know how (my code is not correct)
  2. Do I need to create a separate admin.inc in order to work? Or do I simply need to add a menu path like admin/config/system/shop and a corresponding callback?
  3. Is it possible for client to use tokens inside the body?
4

First of all hook_mail_alter() only has 1 parameter, no $form_state. Second of all, the system_settings_form() will save the form values into the variables table in the database. To access that value you should use variable_get().

Now, your code should look like this:

// try to change body to something more specific to your module, like custom_email_body
$form['mail_template']['custom_email_body'] = array( 
  '#type' => 'textarea',
  '#title'  => t('Email template'),
  '#default_value' => variable_get('custom_email_body', ''),
  '#rows' => 10,  
);

then:

function shop_mail_alter(&$message) {
$client_body = variable_get('custom_email_body', '');
if (is_array($message['body'])) {
    $message['body'][] .= $client_body;
  }
}
2
  • Thanks but sadly system_settings_form() does not save any form content and also ['custom_email_body'] form field does not keep persistent info. Apr 17 '12 at 14:23
  • to display existing value in form, change $form['mail_template']['custom_email_body'] and add property '#default_value' = variable_get('custom_email_body', ''); Apr 17 '12 at 14:43
0

for point 2) you are correct you need to create one custom menu hook in custom module in that hook

   $items['admin/config/system/shop'] = array(
       'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('shop_admin_settings'), //put the name of the form here
    'access callback' => TRUE
    );

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.