Drupal 6:修改提交节点的uid

时间:2010-01-03 16:52:56

标签: forms drupal drupal-6

我的情况是,我希望一组用户(员工)能够创建节点,但要将uid(用户ID)替换为当前显示的用户配置文件。

换句话说,我有一个块,它调用内容类型的表单。如果员工(uid = 20)进入客户页面(uid = 105)并填写表单,我希望与表单关联的uid是客户端(105),而不是员工的。

我正在使用arg(1)来抓住客户端的uid - 这就是我所拥有的......

 <?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
       if (arg(0) == 'user' && is_numeric(arg(1))) {
        $form['#submit'][] = 'addSR_submit_function';
    }
}

function addSR_submit_function($form, $form_state) {
    $account = user_load(arg(1));
    $form_state['values']['uid'] = $account->uid;
    $form_state['values']['name'] = $account->name;
}

?>

表单正在块中加载,但在提交时,仍然显示员工uid。我不想使用hook_form_alter,因为我不想修改实际的表单,因为客户端可以直接填写表单,在这种情况下,我根本不想修改表单。

我也很惭愧,我把它放在一个区块中,但是我想不出把它放在一个模块中的方法,所以对此的任何建议也会受到赞赏......

2 个答案:

答案 0 :(得分:1)

PHP块很糟糕。你可以将它们放在一个模块中。

function hook_block($op, $delta = 0) {
  // Fill in $op = 'list';
  if ($op == 'view' && $delta = 'whatever') {
    $account = user_load(arg(1));
    $node = array('uid' => $account->uid, 'name' => $account->name, 'type' => 'service_request', 'language' => '', '_service_request_client' => $account->uid);
    $output = drupal_get_form('service_request_node_form', $node);
    // Return properly formatted array.
  }
}

此外,您希望form_alter只是强制执行值。这很丑,但它确实有效。

function hook_form_service_request_node_form_alter(&$form, $form_state) {
  if (isset($form_state['node']['_service_request_client'])) {
    $form['buttons']['submit']['#submit'] = array('yourmodule_node_form_submit', 'node_form_submit');
  }
}

function yourmodule_node_form_submit($form, &$form_state) {
  $account = user_load($form_state['node']['_service_request_cilent'])l
  $form_state['values']['uid'] = $account->uid;
  $form_state['values']['name'] = $account->name;
}

答案 1 :(得分:1)

要在块中创建表单,可以使用formblock module。特别是如果您不习惯使用Drupal API。然后,如果要将自己的提交处理程序添加到表单,那么剩下的就剩下了。这是在提交表单时运行的一段代码。您只想在客户端页面上执行此操作,以便使用hook_form_alter函数执行此操作。

/**
 * Hooks are placed in your module and are named modulename_hookname().
 * So if a made a module that I called pony (the folder would then be called
 * pony and it would need a pony.info and pony.module file I would create this function
 */
function pony_form_service_request_node_form_alter(&$form, $form_state) {
    // Only affect the form, if it is submitted on the client/id url
    if (arg(0) == 'client' && is_numeric(arg(1))) {
        $form['#submit'][] = 'pony_my_own_submit_function';
    }
}

function pony_my_own_submit_function($form, &$form_state) {
    $account = user_load(arg(1));
    $form_state['values']['uid'] = $account->uid;
    $form_state['values']['name'] = $account->name;
}

此代码背后的想法是仅在满足条件时更改表单 - 它是在客户端页面上提交的。我猜测arg(0)将是客户端,所以如果它是其他东西你需要改变原因。我们只需要添加一个提交函数,因为我们想要的是在表单通过验证时更改值。

然后,如果是这种情况,我们的第二个函数就会运行,它会对值进行实际更改。