加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Drupal 中对Node页面的Ajax修改

发布时间:2020-12-16 01:44:47 所属栏目:百科 来源:网络整理
导读:项目中有个对新增的机器进行自行编号保证编号的唯一和快速,下面把程序丢出来记录下: ?php/** * @file * Defines "warehouse import" Feeds import. * * @TODO: Split admin functions into their own file. *//* function node_modfiy_form_alter($form,$f

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读