在drupal中如何将名称textfield添加到简单的新闻块中

时间:2010-09-15 09:37:40

标签: php drupal

在drupal中如何将名称字段添加到简单的新闻块。如果我们安装简单的新闻模块,我们可以获得一个电子邮件字段,单选按钮订阅取消订阅并保存按钮。我可以添加名称和文本框

2 个答案:

答案 0 :(得分:2)

您可以使用hook_form_alter()添加名称字段。您还需要添加提交处理程序,以便将名称存储在数据库中。像这样......

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
    // ...
    // ...

  }
}

答案 1 :(得分:1)

改为使用webform模块

在您的module / webform / component目录中创建一个名为simplenes.inc的文件,然后复制下面的代码。 您将拥有一个名为“simplenews”的新Webform组件。然后,您可以选择此字段应订阅的新闻稿。

根本没有经过严格测试,使用它需要您自担风险。

<?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'>Simpnews</a> 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'];
}

来源:http://drupal.org/node/127178