Hi,
I am currently trying to add and save a new (name) field to simplenews subscription block. At the time being, simplenews subscription block contains only an email field.

http://i51.tinypic.com/2lw1ydl.png (This is the block that I willing to add)

I have created a module (modify_simplenews) so I can alter the form and save the field in the database .

function modify_simplenews_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'simplenews_block_form_1':// <-- change 5 to the ID of your newsletter
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => TRUE,
'#size' => 20,
'#weight' => 1,
);

// Add submit handler so we can store the name
$form['#submit'][] = 'modify_simplenews_block_form_submit';
break;
}
}

function modify_simplenews_block_form_submit($form, &$form_state) {
if ($form['#id'] == 1) {
$name = $form_state['values']['name'];
// Do something here to store the name in the database
// ...
// ...

}
}

However, I don't know what should I do to save the name field in the database. The subscriber table in simplenews only contains an email field, should I add a name field to the database structure of the Subscriber table? What is the code that can be used to store the name variable in the database ?

Thanks!

Comments

miro_dietiker’s picture

Category: task » support
Status: Active » Fixed

Use this module for that. It does what you're trying to.
http://drupal.org/project/simplenews_realname

However, for Drupal 7 it is not yet existing. But you could help porting it... At least it's a better start than your code only.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

heyehren’s picture

Status: Closed (fixed) » Active

The simplenews realname module doesn't work properly at the moment. It throws errors when trying to add the name field: http://drupal.org/node/1912712

emilorol’s picture

Hi,

I have spend a big part of todays day fixing the issues mention on the linked post as I need the functionality of the module in a current project. I plan to later on submit a patch that will wrap all the changes in a single file. Meanwhile all the fixes can be apply manually. Scroll down in the page http://drupal.org/node/1912712 to the fixes.

yuseferi’s picture

Issue summary: View changes

I achieve it by myself idea, you can do something like me

in custom form_alter

function  mymodule_form_alter(&$form,&$form_state,$form_id){
  global $user;
    if($form_id=="simplenews_block_form_1"){
      $email=user_is_logged_in()?$user->mail:"";
      $form['mail']=array(
        '#type'=>'textfield',
        '#title' => t('Email'),
        '#size' => 20,
        '#maxlength' => 128,
        '#required' =>true,
        '#default_value' =>$email,
      );
    }
....

It cause show the email box even user is logged in , I hope this help others that like something like this, email box for logged in user in simplenews subscription form