tags:

views:

61

answers:

2

In drupal how to add name field to simple news block.If we install simple news module we can get a email field,radio buttons Subscribe Unsubscribe and save button .how can i add Name and text box

A: 

Use webform module instead

Create a file called simplenes.inc in your module/webform/component directory and copy the code below. You'll have a new Webform component called "simplenews". You can then select which newsletter this field should subscribe to.

This was not heavily tested at all, use it at your own risk.

<?php

function _webform_submit_simplenews(&$data, $component) {
  $news_vid = $data[0];
  $email = $data[1];
  if($email && $news_vid) {
    simplenews_subscribe_user($email, $news);
  }
}

function _webform_edit_simplenews($currfield) {
  if (!module_exists("simplenews")) {
    drupal_set_message(t("Using simplenews components in webform requires the <a href='http://drupal.org/project/simplenews'&gt;Simpnews&lt;/a&gt; module."), "error");
  }

  $edit_fields = array();
  $options = array();

  foreach( taxonomy_get_tree(_simplenews_get_vid()) as $newsletter) {
    $options[$newsletter->tid] = $newsletter->name;
  }

  $edit_fields['extra']['newsletter'] = array(
    '#type' => 'select',
    '#title' => t("Newsletter"),
    '#default_value' =>  $currfield['extra']['newsletter'],
    '#description' => t('Select which newsletter can be chosen'),
    '#required' => TRUE,
    '#multiple' => FALSE,
    '#size' => sizeof($options),
    '#options' => $options,
  );

  $edit_fields['mandatory'] = array(
    '#type' => 'hidden',
    '#value' => 1,
  );
  $edit_fields['extra']['description'] = array(); // Hide the description box

  return $edit_fields;
}

function _webform_render_simplenews($component) {
  $form_item[] = array(
    '#type'          => 'hidden',
    '#value'         => $component['extra']['newsletter'],
  );
  $form_item[] = array(
    '#title'    => htmlspecialchars($component['name'], ENT_QUOTES),
    '#type'     => 'textfield',
    '#required' => 1,
    '#validate' => array('_webform_validate_email' => array('submitted]['. $component['cid'])),
  );
  $form_item['#weight'] = $component['weight'];

  return $form_item;
}

function _webform_submission_display_simplenews($data, $component) {
  $form_item = _webform_render_hidden($component);
  $form_item['#value']         = $data['value']['0'];
  $form_item['#type']          = 'textfield';
  $form_item['#title']         = htmlspecialchars($component['name'], ENT_QUOTES) ." (hidden)";
  $form_item['#attributes']    = array("disabled" => "disabled");
  return $form_item;
}

function _webform_help_simplenews($section) {
  switch ($section) {
    case 'admin/settings/webform#simplenews_description':
      $output = t("Subscribe to newsletters.");
      break;
  }
  return $output;
}

function _webform_analysis_rows_simplenews($component) {  
  $query = 'SELECT data '.
    ' FROM {webform_submitted_data} '.
    ' WHERE nid = %d '.
    ' AND cid = %d';
  $nonblanks = 0;
  $submissions = 0;
  $wordcount = 0;

  $result = db_query($query, $component['nid'], $component['cid']);
  while ($data = db_fetch_array($result)) {
    if ( strlen(trim($data['data'])) > 0 ) {
      $nonblanks++;
      $wordcount += str_word_count(trim($data['data']));
    }
    $submissions++;
  }
  $rows[0] = array( t('Submissions'), $submissions);
  return $rows;
}

function _webform_table_data_simplenews($data) {
  return check_plain(empty($data['value']['1']) ? "" : $data['value']['1']);
}

function _webform_csv_headers_simplenews($component) {
  $header = array();
  $header[0] = '';
  $header[1] = '';
  $header[2] = $component['name'];
  return $header;
}

function _webform_csv_data_simplenews($data) {
  return empty($data['value']['1']) ? "" : $data['value']['1'];
}

Source: http://drupal.org/node/127178

Loveleen Kaur
A: 

You can add a Name field using hook_form_alter(). You will also need to add a submit handler so you can store the name in the database. Something like this...

function mymodule_form_alter(&$form, &$form_state, $form_id) {  
  switch($form_id) {  
    case 'simplenews_block_form_5':// <-- 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'][] = 'mymodule_simplenews_block_form_submit';
    break;
  }  
} 

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

  }
}
kidrobot