【问题标题】:Drupal - Include more than one user_profile_form on a pageDrupal - 在页面上包含多个 user_profile_form
【发布时间】:2009-10-14 22:56:13
【问题描述】:

编辑:

我认为这是因为动作相同或其他。我尝试使用此修改操作:

function mytheme_user_profile_form($form) {
        global $user;
        $uid = $user->uid;
        //print '<pre>'; print_r($form); print '</pre>';
    $category = $form['_category']['#value'];

    switch($category) {
            case 'account':
            $form['#action'] = '/user/'.$uid.'/edit?destination=user/'.$uid;
                        break;
        case 'education':
                        $form['#action'] = '/user/'.$uid.'/edit/education?destination=user/'.$uid;
                        break;
        case 'experience':
                        $form['#action'] = '/user/'.$uid.'/edit/experience?destination=user/'.$uid;
                        break;
            case 'publications':
                        $form['#action'] = '/user/'.$uid.'/edit/publications?destination=user/'.$uid;
                        break;
        case 'conflicts':
                        $form['#action'] = '/user/'.$uid.'/edit/conflicts?destination=user/'.$uid;
                        break;
    }

        //print '<pre>'; print_r($form); print '</pre>';
        //print $form['#action'];
        $output .= drupal_render($form);
        return $output;
}

但是,实际呈现表单时的表单操作并没有改变。他们都是/user/%uid

我可以修改表单动作吗?

我在一个页面上包含了用户配置文件表单的几个不同“类别”,代码将正确输出我指定的表单。每个表单都在一个单独的可折叠 div 中。

我的问题是双重的。

(1) 字段的现有值未预先填充,并且

(2) 点击某个部分的“保存”将导致警告:无论您实际保存的是哪种形式,都需要电子邮件字段

我很确定对于问题 #2,这是因为按钮的名称在所有情况下都是相同的,表单 id 也是如此。

print '<h3>&ndash; Account Settings</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'account'));
print '</div>';

print '<h3>&ndash; My Info</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'Personal'));
print '</div>';

print '<h3>&ndash; Experience</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'experience'));
print '</div>';

print '<h3>&ndash; Education</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'education'));
print '</div>';

【问题讨论】:

  • 你有没有在源头上查看这些表格是否真的是独一无二的?

标签: forms drupal profile


【解决方案1】:

问题 #1: ?你能发布html源代码吗? 对于问题 #2: 好的,我将在此处逐步执行代码: 用户配置文件表单 (user_profile_form_validate()) 调用的验证处理程序

user_module_invoke('validate', $form_state['values'], $form_state['values']['_account'], $form_state['values']['_category']);

看起来像

<?php
/**
* Invokes hook_user() in every module.
*
* We cannot use module_invoke() for this, because the arguments need to
* be passed by reference.
*/
function user_module_invoke($type, &$array, &$user, $category = NULL) {
  foreach (module_list() as $module) {
    $function = $module .'_user'; 
    if (function_exists($function)) {
      $function($type, $array, $user, $category);
    }
  }
}
?>

因此,此表单的验证处理程序正在遍历每个模块以查找用户挂钩函数并使用$type = 'validate' 调用它们。 (请注意,'category' 参数在这里是可选的 - contrib 模块不需要使用它)

我们以 user.module 的用户钩子为例,看看会发生什么:

function user_user($type, &$edit, &$account, $category = NULL) {
  if ($type == 'view') {
    $account->content['user_picture'] = array(
      '#value' => theme('user_picture', $account),
      '#weight' => -10,
    );
    if (!isset($account->content['summary'])) {
      $account->content['summary'] = array();
    }
    $account->content['summary'] += array(
      '#type' => 'user_profile_category',
      '#attributes' => array('class' => 'user-member'),
      '#weight' => 5,
      '#title' => t('History'),
    );
    $account->content['summary']['member_for'] = array(
      '#type' => 'user_profile_item',
      '#title' => t('Member for'),
      '#value' => format_interval(time() - $account->created),
    );
  }
  if ($type == 'form' && $category == 'account') {
    $form_state = array();
    return user_edit_form($form_state, (isset($account->uid) ? $account->uid : FALSE), $edit);
  }
//<-- LOOK HERE -->
  if ($type == 'validate' && $category == 'account') {
    return _user_edit_validate((isset($account->uid) ? $account->uid : FALSE), $edit);
  }

  if ($type == 'submit' && $category == 'account') {
    return _user_edit_submit((isset($account->uid) ? $account->uid : FALSE), $edit);
  }

  if ($type == 'categories') {
    return array(array('name' => 'account', 'title' => t('Account settings'), 'weight' => 1));
  }
}

所以,它只应该验证类别 == 'account'

在_use_edit_validate函数中,我们发现:

  // Validate the e-mail address:
  if ($error = user_validate_mail($edit['mail'])) {
    form_set_error('mail', $error);
  }

这是您的错误消息。

由于该表单仅应在类别 == 'account' 时进行验证,而您的问题 (#2) 似乎是无论类别如何,它总是会验证,因此您的表单可能没有被呈现为唯一的表单实例? Drupal 可能每次都会渲染一个完整的表单,并且只是将隐藏的表单值设置为任何类别(就像在 user_pages.inc $form['_category'] = array('#type' =&gt; 'value', '#value' =&gt; $category); 中的此表单的定义函数中一样)

查看实际的 html 源输出会很有帮助。

==编辑 10-15-09 以响应更新的问题===

好的,看起来您的方法(在主题层中手动编辑$form['#action'])可能无法实现(请参阅this 帖子以供参考)。如果您想更改表单操作,您需要编写一个实现hook_form_alter() 的自定义模块(它在主题模板文件中不起作用)。此功能允许您修改表单的呈现方式,在您的情况下是用户修改表单。有更多关于表单修改的细节here

我不是 100% 确定这是您想要做的; (因为看起来您已经必须创建一个模块)也许您想改为挂接到hook_user();此功能“...允许模块在对用户帐户执行操作时做出反应。”。您可以对此功能中的类别做出反应,并阻止/允许您喜欢的任何用户更改。

但是,如果只是电子邮件地址验证存在问题,并且如果您正在与现有用户打交道,为什么不确保在保存之前设置电子邮件地址?

【讨论】:

  • 是的,这就是问题所在...我正在编辑我的原始帖子以对此进行详细说明...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 2012-01-23
  • 2023-04-07
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多