2010-09-15 12 views
0

在Drupal如何名稱字段添加到簡單的新聞block.If我們安裝簡單的新聞模塊,我們可以得到一個電子郵件字段,單選按鈕訂閱退訂和保存按鈕i。如何能添加名稱和文本框在Drupal如何名稱文本字段添加到簡單的新聞塊

+0

我沒有看到一個問題或任何會導致我考慮回答。 – halfdan 2010-09-15 09:42:04

回答

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

使用網絡表單模塊,而不是

創建一個名爲simplenes.inc在你的模塊/表單/組件目錄下的文件,並複製下面的代碼。 你有一個名爲「simplenews」的新網絡表單組件。然後,您可以選擇該字段應該訂閱的通訊。

這是不是在所有嚴格測試,在您自己的風險使用它。

<?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

+0

我們可以使用http://drupal.org/project/simplenews_realname模塊 – Anil 2011-01-12 09:53:14

相關問題