Reputation: 1149
I am pre-populating the recipients in the Privatemsg module, but cant figure out where I am going wrong.
I successfully print out the options into a select list in the format... 55 => Chris (uid => username)
Here is my module code...
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);
}
The form loads ok, you select the user from the drop down, fill out the message and click send, and then the following error shows...
Warning: Invalid argument supplied for foreach() in MYMODULE_validate() (line 60 of /example.com/sites/all/modules/custom/MYMODULE/MYMODULE.module). You must include at least one valid recipient.
Can someone help see whats wrong?
If I add a dsm($form_state) to the validation function, I can see that..
$form['values']['recipient'] = [email protected]
This means the recipient is being passed through successfully, so im not sure where its getting stuck.
Upvotes: 0
Views: 57
Reputation: 1149
I decided to ditch the extra validation function. Looking at the validate array, sufficient validation was already being performed and didn't need to be overridden.
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;
}
As soon as it avoided this validation, the form submitted and message sent.
Upvotes: 0