Drupal 中对Node页面的Ajax修改

前端之家收集整理的这篇文章主要介绍了Drupal 中对Node页面的Ajax修改前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

项目中有个对新增的机器进行自行编号保证编号的唯一和快速,下面把程序丢出来记录下:

  1. <?PHP
  2.  
  3. /**
  4. * @file
  5. * Defines "warehouse import" Feeds import.
  6. *
  7. * @TODO: Split admin functions into their own file.
  8. */
  9. /* function node_modfiy_form_alter(&$form,$form_state,$form_id) {
  10. if ($form_id == 'fixed_assets_node_form') {
  11. drupal_set_message(t('some message.'));
  12. dpm($form);
  13. dpm($node);
  14. }
  15. } */
  16. function node_modfiy_form_alter(&$form,&$form_state,$form_id) {
  17.  
  18. if($form_id=='fixed_assets_node_form'){
  19. //dpm($form);
  20. //dpm($form_state);
  21. if(!isset($form['nid']['#value'])){
  22. $form['title']['#ajax']=array(
  23. 'event'=>'click','wrapper'=>'form-item-form-type-textfield-form-item-title','callback'=>'node_modfiy_title_ajax_callback',);
  24. $form['title']['#prefix']='<div id=form-item-form-type-textfield-form-item-title>';
  25. $form['title']['#suffix']='</div>';
  26. $form['title']['#validate']=TRUE;
  27. //form_set_cache($form['#build_id'],$form,$form_state);
  28. $form['#validate'][]='node_modfiy_form_validate';
  29. //dpm($form['#validate']);
  30. }
  31. }
  32. }
  33.  
  34. /**
  35. * Implementation of hook_validate().
  36. *验证输入的条码不可以有重复
  37. */
  38. function node_modfiy_form_validate($form,&$form_state){
  39. if(strlen($form_state['values']['title'])<10 )
  40. form_set_error('title','请选择正确的位置信息和机器分类信息');
  41. $myresult=db_query("select n.nid FROM {node} n Where n.title=:title And n.type=:type",array('title'=>$form_state['values']['title'],'type'=>"fixed_assets"));
  42. $nid=$myresult->fetchColumn(1);
  43. if($nid)
  44. form_set_error('title','重复的条码!');
  45. }
  46.  
  47.  
  48. function node_modfiy_title_ajax_callback($form,$form_state){
  49. $fixed_barcode=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_localtion-und']['hierarchical_select']['selects'][0]['#default_value'])->name;
  50. $fixed_barcode.=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][0]['#default_value'])->name;
  51. $fixed_barcode.=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value'])->name;
  52. $fixed_class=$form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value'];
  53. $count=db_query("select count(nid) from {taxonomy_index} where tid in (SELECT tid FROM {taxonomy_term_hierarchy} WHERE parent=".$form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value'].")")->fetchField();
  54. //$count = $query->execute()->fetchField();
  55. $myid=$fixed_barcode.(sprintf("%04d",$count+1));
  56. //$myresult=db_result(db_query("select 1 FROM {node} Where title=%s And type=%s",$myid,"fixed_assets"));
  57. $form['title']=array(
  58. '#type'=>'textfield','#title'=>'衣车编号','#required'=>TRUE,'#value'=>$myid,'#maxlength'=>255,'#weight'=>-7,'#prefix'=>'<div id=form-item-form-type-textfield-form-item-title>','#suffix' => '</div>','#validated' => TRUE,'#name' => 'title','#id'=>'edit-title','#class'=>'form-text required ajax-processed'
  59. );
  60. //if(strlen($myid)<10 || $myresult){
  61. if(strlen($myid)<10){
  62. form_set_error('title','请选择正确的位置信息和机器分类信息,将生成衣车编号');
  63. }
  64. return $form['title'];
  65. }

程序很简单但其中有较多的知识点:AJAX的使用,sprintf 函数,form_set_error ,二个Hook,还有找到应的变量的结构等等。

程序参考了以下资料:

SQL查询方面的,

https://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery%3A%3AaddExpression/7

https://api.drupal.org/api/drupal/includes%21database%21database.inc/function/db_query/7

https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7

https://www.drupal.org/node/1051242

https://www.drupal.org/node/494026

一个验证Hook,

http://www.thinkindrupal.com/node/1016

AJAX例子,

https://www.drupal.org/node/2465119

猜你在找的Ajax相关文章