Drupal 中对Node页面的Ajax修改

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

<?php

/**
 * @file
 * Defines "warehouse import" Feeds import.
 *
 * @TODO: Split admin functions into their own file.
 */
/* function node_modfiy_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'fixed_assets_node_form') {    
    drupal_set_message(t('some message.'));
	dpm($form);	
	dpm($node);
  }  
} */
function node_modfiy_form_alter(&$form, &$form_state, $form_id) {

	if($form_id=='fixed_assets_node_form'){	
			//dpm($form);
			//dpm($form_state);		
		  if(!isset($form['nid']['#value'])){
			$form['title']['#ajax']=array(
				'event'=>'click',
				'wrapper'=>'form-item-form-type-textfield-form-item-title',
				'callback'=>'node_modfiy_title_ajax_callback',
				);
			$form['title']['#prefix']='<div id=form-item-form-type-textfield-form-item-title>';
			$form['title']['#suffix']='</div>';
			$form['title']['#validate']=TRUE;
			//form_set_cache($form['#build_id'],$form,$form_state);	
			
			$form['#validate'][]='node_modfiy_form_validate';
			//dpm($form['#validate']);
		  }
			
	}  
}

/**
 * Implementation of hook_validate().
 *验证输入的条码不可以有重复
 */
function node_modfiy_form_validate($form,&$form_state){
		
	if(strlen($form_state['values']['title'])<10 )	
		form_set_error('title','请选择正确的位置信息和机器分类信息');
	$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"));
	$nid=$myresult->fetchColumn(1);	
	if($nid)
		form_set_error('title','重复的条码!');
}


function node_modfiy_title_ajax_callback($form, $form_state){	
		
		$fixed_barcode=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_localtion-und']['hierarchical_select']['selects'][0]['#default_value'])->name;
		$fixed_barcode.=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][0]['#default_value'])->name;
		$fixed_barcode.=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value'])->name;
		$fixed_class=$form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value'];
		
		$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();
				
		//$count = $query->execute()->fetchField();		
		
		$myid=$fixed_barcode.(sprintf("%04d",$count+1));
		//$myresult=db_result(db_query("select 1 FROM {node} Where title=%s And type=%s",$myid,"fixed_assets"));
		
		$form['title']=array(
		'#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'
		);
		
		//if(strlen($myid)<10 || $myresult){
		if(strlen($myid)<10){	
			form_set_error('title','请选择正确的位置信息和机器分类信息,将生成衣车编号');
		}
		return $form['title'];
}

程序很简单但其中有较多的知识点: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 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值