Drupal Privatemsg Pre-Populate Recipients

时间:2016-09-12 15:33:22

标签: drupal-7

我在Privatemsg模块中预先填充了收件人,但无法弄清楚我哪里出错了。

我成功将选项打印成格式的选择列表... 55 =>克里斯 (uid =>用户名)

这是我的模块代码......

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'privatemsg_new') {
        global $user; 
        if (in_array('Venue Manager', $user->roles) || in_array('Couples Page Manager', $user->roles)) {
            $form['recipient']['#title'] = 'Choose which of your Couples you would like to message.';
            $form['recipient']['#type'] = 'select';
            $form['recipient']['#size'] = 1;
            $options = couples_page_messages_recipients();
            $form['recipient']['#options'] = $options;
            array_unshift($form['actions']['submit']['#validate'], 'couples_page_messages_validate');
        }
        if (in_array('Couples Page Manager', $user->roles)) {
            $form['recipient']['#title'] = 'Send a message to your venue.';
        }
    }   
}

function MYMODULE_recipients() {
    global $user;
    $options = array();
    if (in_array('Manager', $user->roles)) {
        $result = db_query('SELECT `entity_id` FROM `field_data_field_parent_user` WHERE `field_parent_user_target_id` = :uid', array(':uid' => $user->uid));
        if (is_array($result) || is_object($result)) {
            foreach ($result as $record) {
                $child = user_load($record->entity_id);
                $childlist[$child->name] = $child->name;
            }
        }
    }
    if (in_array('Page Manager', $user->roles)) {
        $result = db_query('SELECT `field_parent_user_target_id` FROM `field_data_field_parent_user` WHERE `entity_id` = :uid', array(':uid' => $user->uid));
        if (is_array($result) || is_object($result)) {
            foreach ($result as $record) {
                $child = user_load($record->field_parent_user_target_id);
            $childlist[$child->name] = $child->name;
            }
        }
    }
    return $childlist;
}

function MYMODULE_validate($form, &$form_state) {
  $recipients = $form_state['values']['recipient'];
  $usernames = array();
  foreach ($recipients as $recipient) {
    $user = user_load((int) $recipient);
    $usernames[] = $user->name;
  }
  $form_state['values']['recipient'] = implode(', ', $usernames);
}

表单加载正常,您从下拉列表中选择用户,填写消息并单击“发送”,然后显示以下错误...

  

警告:为foreach()提供的参数无效   MYMODULE_validate()(第60行)   /example.com/sites/all/modules/custom/MYMODULE/MYMODULE.module)。您   必须包括至少一个有效的收件人。

有人可以帮忙看看有什么不对吗?

如果我在验证函数中添加dsm($ form_state),我可以看到..

$form['values']['recipient'] = me@example.com

这意味着收件人成功通过,所以我不确定它会被卡住。

1 个答案:

答案 0 :(得分:0)

我决定放弃额外的验证功能。查看验证数组,已经执行了足够的验证,并且不需要重写。

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'privatemsg_new') {
        global $user; 
        if (in_array('Venue Manager', $user->roles) || in_array('Couples Page Manager', $user->roles)) {
            $form['recipient']['#title'] = 'Choose which of your Couples you would like to message.';
            $form['recipient']['#type'] = 'select';
            $form['recipient']['#size'] = 1;
            $options = couples_page_messages_recipients();
            $form['recipient']['#options'] = $options;
        }
        if (in_array('Couples Page Manager', $user->roles)) {
            $form['recipient']['#title'] = 'Send a message to your venue.';
        }
    }   
}

function MYMODULE_recipients() {
    global $user;
    $options = array();
    if (in_array('Manager', $user->roles)) {
        $result = db_query('SELECT `entity_id` FROM `field_data_field_parent_user` WHERE `field_parent_user_target_id` = :uid', array(':uid' => $user->uid));
        if (is_array($result) || is_object($result)) {
            foreach ($result as $record) {
                $child = user_load($record->entity_id);
                $childlist[$child->name] = $child->name;
            }
        }
    }
    if (in_array('Page Manager', $user->roles)) {
        $result = db_query('SELECT `field_parent_user_target_id` FROM `field_data_field_parent_user` WHERE `entity_id` = :uid', array(':uid' => $user->uid));
        if (is_array($result) || is_object($result)) {
            foreach ($result as $record) {
                $child = user_load($record->field_parent_user_target_id);
            $childlist[$child->name] = $child->name;
            }
        }
    }
    return $childlist;
}

一旦避免此验证,表单就会提交并发送消息。