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

这里的技术是共享的

You are here

drupal 7 custom autocomplete drupal7 d7 自定义字段 自动完成 自动补全 自己亲自做的 有大用 有大大用

https://www.drupal.org/docs/7/modules/entity-reference/using-a-view-to-display-the-elements-in-the-autocomplete-widget 

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

https://x-team.com/blog/extending-drupal-7-autocomplete-fields-output/

https://www.bounteous.com/insights/2019/01/30/drupal-how-customize-autocomplete-labels

https://drupal.stackexchange.com/questions/51089/creating-module-with-autocomplete-and-accepting-multiple-values-in-textfield

https://www.webomelette.com/how-create-autocomplete-form-element-drupal-7

https://antistatique.net/en/blog/how-to-create-a-custom-autocomplete-using-the-drupal-8-form-api

https://www.drupal.org/project/webform/issues/2962972

https://www.drupal.org/forum/support/module-development-and-code-questions/2016-02-12/custom-autocomplete-fields-like 

https://www.drupal.org/forum/support/module-development-and-code-questions/2015-02-12/autocomplete-does-not-work-when-call  



下面四个函数是自己亲自做的,注意红色的字,它们之间的关系就可以了


function custom_twenty_two_jses_menu()
{
 
   
$items['batch_dhcps'] = array(
       
'title' => '批量添加dhcp',
       
'page callback' => 'custom_twenty_two_jses_batch_dhcps',
       
'access arguments' => array('create wp_blog_clone_2 content'),
   );
   
$items['network_segment_autocomplete'] = array(
       
'title' => '批量添加dhcp',
       
'page callback' => 'custom_twenty_two_jses_network_segment_autocomplete',
       
'access arguments' => array('create wp_blog_clone_2 content'),
   );
   
return $items;
}


function batch_dhcps_form()
{
   
$form['dchp_network_segment'] = array(
       
'#type' => 'textfield',
       
'#title' => t('网段名称'),
       
'#size' => 60,
       
'#autocomplete_path' => 'network_segment_autocomplete',
   );
   
$form['mac_address'] = array(
       
'#type' => 'textarea',
       
'#title' => t('mac address'),
       
'#cols' => 60,
       
'#rows'  => 10,
       
'#default_value' => $_SESSION['transfor_form_textarea'],
       
'#description' => '一行一个mac地址中短横,冒号及连在一起的mac都可以;'
   
);

   
$form['submit'] = array('#type' => 'submit', '#value' => t('submit'));

   
return $form;
}


function custom_twenty_two_jses_batch_dhcps()
{
   
return drupal_get_form('batch_dhcps_form'); //记住,这里千万要用 drupal_get_form
}


function custom_twenty_two_jses_network_segment_autocomplete($string)
{
   
$string = _removeCnEnSpace($string);
   
$results =  array();
     // 为什么drupal7 中后面没有fetchAll() 方法也行
   
$results_db =  db_query("select n.nid,net_name.field_network_name_value,network_ip.field_network_ip_value,server_ip.field_dhcp_server_ip_value from node n
         inner join field_data_field_network_name net_name on n.nid=net_name.entity_id
         inner join  field_data_field_network_ip network_ip  on n.nid=network_ip.entity_id
         inner join  field_data_field_dhcp_server_ip server_ip  on  n.nid=server_ip.entity_id
       where type='dchp_network_segment'
         and net_name.entity_type='node' and net_name.bundle='dchp_network_segment'
        and network_ip.entity_type='node' and network_ip.bundle='dchp_network_segment'
        and server_ip.entity_type='node' and server_ip.bundle='dchp_network_segment'
        and net_name.field_network_name_value  like :field_network_name_value"
,array(':field_network_name_value' => '%' . db_like($string) . '%'));

   
foreach ($results_db as $row) {
       
$results[$row->field_network_name_value.'-'.$row->field_network_ip_value.'-'.$row->field_dhcp_server_ip_value] = check_plain($row->field_network_name_value.'-'.$row->field_network_ip_value.'-'.$row->field_dhcp_server_ip_value);
   }
   drupal_json_output(
$results);
   
exit;
}


普通分类: