欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

druapl ahah 表单 提交 保存 drupal_add_js 增加 js 功能 有大用

shiping1 的头像

drupal ajax区块异步提交node 不指定

drupal水滴 , 2011/11/29 23:07 , drupal建站 , 评论(0) , 阅读(1655) | |
How to create a custom Drupal AJAX module
通过这个模块,我们创建了一个显示最新发布的node的区块,并且我们可以通过该区块异步提交node.
ahah_example.info

name = AHAH Example
description = A module for demonstrating how to implement AHAH in Drupal.
version = "6.x-1.0"
core = "6.x"
project = "ahah_example"


ahah_example.module

<?php
/**
* Implementation of hook_menu().
*/
function ahah_example_menu() {
  $items = array();
  $items['ahah_example/add'] = array(
    'page callback' => 'ahah_example_add',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
* Implementation of hook_block().
*/
function ahah_example_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;

  switch ($op) {
    case 'list':
      $blocks[0] = array(
        'info' => t('AHAH Example'),
      );
      return $blocks;
    case 'view': default:
      switch ($delta) {
        case 0:
          $block['subject'] = t('AHAH Example - My Recent Pages');
          $block['content'] = drupal_get_form('ahah_example_form');
          break;
      }
      return $block;
  }
}
/**
* Implementation of hook_form().
* A form to allow the user to create a new page node and show the 5 most recent pages created by the currently logged in user.
*/
function ahah_example_form($form_state) {
  global $user;

  // create a DIV to show the output of the AHAH request
  $form['new_row_wrapper'] = array(
    '#type' => 'markup',
    '#value' => '<br><div id="ahah-example-new-row-wrapper" style="clear: both;"></div>',
  );

  // show links to the latest 5 page nodes
  $sql = "SELECT n.nid, n.title
      FROM {node} AS n
      WHERE n.uid = '%d'
        AND n.type = 'page'
      ORDER BY n.nid DESC
      LIMIT 5";
  $db_result = db_query($sql, $user->uid);
  while ($row = db_fetch_object($db_result)) {
    $form['node_'.$row->nid] = array(
      '#type' => 'markup',
      '#value' => '<div><a href="'.url('node/'.$row->nid).'">Node '.$row->nid.' - '.$row->title.'</a></div>',
    );
  }  

  // create a form to allow the user to enter the new node's title and body
  $form['new_title'] = array(
    '#type' => 'textfield',
    '#title' => "Title",
    '#size' => 40,
    '#required' => TRUE,
  );
  $form['new_body'] = array(
    '#type' => 'textarea',
    '#title' => "Body",
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add Node'),
    '#submit' => array(),
    '#ahah' => array(
      'path' => 'ahah_example/add',
      'wrapper' => 'ahah-example-new-row-wrapper',
      'method' => 'prepend',
      'effect' => 'fade',
      'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
    ),
  );

  return $form;
}

function ahah_example_add() {
  global $user;

  $output = '';

  //error checking
  if (!$user->uid) {
    $output .= '<div class="messages error ahah-example-msg">Access denied.</div>';
  }
  if ($_REQUEST['new_title'] == '') {
    $output .= '<div class="messages error ahah-example-msg">Please enter a title</div>';
  }
  if ($_REQUEST['new_body'] == '') {
    $output .= '<div class="messages error ahah-example-msg">Body field is required.</div>';
  }

  //no error, save node
  if (!$output) {
    $node = new StdClass();
    $node->type = 'page';
    $node->status = 1;
    $node->uid = $user->uid;
    $node->title = $_REQUEST['new_title'];
    $node->body = $_REQUEST['new_body'];
    node_save($node);
    if ($node->nid) {
      $output = '
      <div class="messages status ahah-example-msg">Successfully saved your page.</div>
      <script type="text/javascript">
      $("#edit-new-title").val("");
      $("#edit-new-body").val("");
      </script>
      <div><a href="'.url('node/'.$node->nid).'">Node '.$node->nid.' - '.$node->title.'</a></div>';
    } else {
      $output = '<div class="messages error ahah-example-msg">An error occurred, we cannot add your page at this time.</div>';
    }
  }

  //remove status message after 10 seconds
  $output .= '
    <script type="text/javascript">
    $("#edit-new-title").val("");
    $("#edit-new-body").val("");
    setTimeout("$(\'.ahah-example-msg\').remove();", 10000);
    </script>';

  //send output back to browser
  drupal_json(array('status' => TRUE, 'data' => $output));
}
?>


http://fuseinteractive.ca/blog/how-create-custom-drupal-ajax-module-0

来自 http://www.majormoves.net/post/861/
普通分类: