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

这里的技术是共享的

You are here

drupal7 d7 drupal_get_form user_profile_form 在另一个页面中包含 user 表单 有大用 有大大用

Use part of the user_profile_form on another page

My users enter a phone number on registration. The amount of values that can be entered is set to unlimited. Setting this changes the field so that you can add another field below to add additional phone numbers. I would like the users to be able to add more numbers after registration without having to go to /user/%user/edit

I am trying to return just the phone number field on another page. However I cannot seem to get any of that form displayed on another page. When I have been displaying other forms I have called them like this:

drupal_render(drupal_get_form('user_profile_form'));




This does return part of the form, First Name, Last Name, Country (all custom fields) but not the phone number field. I also get quite a few errors
Undefined index: user_profile_form in drupal_retrieve_form()
Undefined index: #user_category in block_form_user_profile_form_alter
Undefined index: #user_category in overlay_form_user_profile_form_alter()
Undefined index: #user_category in system_form_user_profile_form_alter()
Undefined offset: 0 in uc_roles_form_user_profile_form_alter()

Obviously I am doing something very wrong...

正确答案

1) module_load_include('inc', 'user', 'user.pages'); should be added to hook_init()

 otherwise all profile form functionalities that require AJAX calls will return the error "Call to undefined function user_profile_form_validate()." 

 2) 在form_alter中,给这个表单加上一句  form_load_include($form_state, 'inc''user''user.pages');





 

module_load_include('inc', 'user', 'user.pages');
global $user;
print(drupal_get_form('user_profile_form', $user));









在另一个页面上使用部分 user_profile_form


3个                
           

我的用户在注册时输入电话号码。可以输入的值的数量设置为无限制。设置此项会更改字段,以便您可以在下方添加另一个字段以添加其他电话号码。我希望用户能够在注册后添加更多号码,而无需转到 /user/%user/edit                

我试图只返回另一页上的电话号码字段。但是我似乎无法在另一页上显示任何该表格。当我一直在展示其他形式时,我这样称呼它们:                

drupal_render(drupal_get_form('user_profile_form'));
               

这确实会返回部分表单、名字、姓氏、国家/地区(所有自定义字段),但不返回电话号码字段。我也遇到了很多错误
未定义索引:drupal_retrieve_form() 中的 user_profile_form 未定义索引
:block_form_user_profile_form_alter 中的#user_category
未定义索引:overlay_form_user_profile_form_alter() 中的#user_category 未定义索引:
system_form_user_profile_form_alter() 中的#user_category
未定义偏移量:uc_roles_form_user_profile_form_alter() 中的 0
               

显然我做错了什么......                

                               
改进这个问题                                
  • 2个                            
    module_load_include('inc', 'user', 'user.pages');应该添加到hook_init()否则所有需要 AJAX 调用的配置文件表单功能将返回错误“调用未定义的函数 user_profile_form_validate()”。 
    – 帕维尔杜比尔                                
     2012 年 5 月 15 日 10:43                             
   

5 个答案                

   
5个                    
               

我根据 Erik 的作品制作了这部作品。我想你可以['#access']=false为所有你不想要的价值观做。                    

//Provide a custom page at /custom-profile to access our custom profile form
function mymodule_menu() {
  $items['custom-profile'] = array(
   'title' => 'Custom Profile!',
   'page callback' => 'mymodule_custom_profile',
   'access callback' => TRUE, 
   'type' => MENU_NORMAL_ITEM,
 );
}

//displays the custom form at /custom-profile
function mymodule_custom_profile() {
  module_load_include('inc', 'user', 'user.pages');

  global $user;  
  $user = user_load($user->uid);

  $form = drupal_get_form('user_profile_form', $user, 'account', 'custom'); 

  return $form;
}

function mymodule_form_user_profile_form_alter(&$form, &$form_state){
  //modifies the form_user_profile DO CUSTOM STUFF BELOW!
  if(isset($form_state['build_info']['args'][2]) && 
           $form_state['build_info']['args'][2] = 'custom'){

   //rename submit button
   $form['actions']['submit']['#value'] = t("dooo it!");

   //remove a few account things from this page
   $form['account']['#access']=FALSE;

   //needs to be there for the picture to work
   form_load_include($form_state, 'inc', 'user', 'user.pages');
  }
}
               
                                   
改进这个答案                                    
   
3个                    
               

尝试这个                    

module_load_include('inc', 'user', 'user.pages');
global $user;
print(drupal_get_form('user_profile_form', $user));
                   

这会起作用。                    

                                   
改进这个答案                                    
  • 太好了,是的,这确实显示了 user_profile_form。你知道如何返回单个字段吗?在这种情况下,值限制设置为无限制的字段。另外我刚刚注意到,添加到此表单的自定义字段不显示其内容。 
    – 傻瓜                                    
     2012 年 4 月 3 日 9:28                                 
  • “无限”值字段上的 AJAX 也不起作用。调用未定义函数 user_profile_form_validate() 
    – 傻瓜                                    
     2012 年 4 月 3 日 9:42                                
  • 我认为我们不能只返回一个字段。制作一个 hook_form_alter 并预加载值。使用 user_load 获取值 
    – 尼克斯马克                                    
     2012 年 4 月 3 日 9:45                                
  • 尝试打印 drupal_render(drupal_get_form('user_profile_form')); 
    – 尼克斯马克                                    
     2012 年 4 月 3 日 9:49                                
  • 恐怕 drupal_render 没有影响,我仍然有 Call to undefined function user_profile_form_validate() 错误。我是否必须单独加载表单的验证?我假设获取表单会自动加载验证功能。 
    – 傻瓜                                    
     2012 年 4 月 3 日 9:57                                
   
1个                    
               
drupal_get_form('user_profile_form',$user,'account', 'my_parameter');      

function my_module_form_alter((&$form, &$form_state, $form_id) {
  switch($form_id) {   
      case 'user_profile_form':
        $info =  $form_state['build_info']['args'][2];
        if ( $info == 'my_parameter' ) {
           //do something with the form
           $form['my_field']['#access'] = false ;
           //prevent ajax errors
           form_load_include($form_state, 'inc', 'user', 'user.pages');
        } 
      break ;       
   }
}
               
                                   
改进这个答案                                    
   
0                    
               

基于@pawel-dubiel 的评论,我将 user.pages.inc 直接添加到 hook_menu():                    

function mymod_menu() {
   return array(
     'my-path' => array(
        'page callback' => 'mymod_page_callback',
        'file' => 'user.pages.inc',
        'file path' => drupal_get_path('module', 'user'),
     )
   );
}
                   

像魅力一样工作并且避免使用 hook_init() 。                    

                                   
改进这个答案                                    
   
-1                    
               

恕我直言,尝试从表单中获取单个字段没有任何意义。                    

如果您想为该字段更新数据库中的单个值,您最好编写自己的表单并管理提交和验证功能以自行更新数据库。                    

                                   
改进这个答案                                    


来自  https://drupal.stackexchange.com/questions/27335/use-part-of-the-user-profile-form-on-another-page


普通分类: