Sometimes it returns bool and sometimes array full of errors. This is not very developer friendly. This needs to change to either just bool or array with information such as :

return array( 
'successful' => FALSE|TRUE,
'errors'=> array(
array(), array()...
),
);

Comments

litwol’s picture

this one is definitely not critical for 1.0, this is API improvement so it should go in no sooner than 2.0.

berdir’s picture

Status: Active » Needs review

This makes sense to include now imho. Always returning an array makes things really nicer. And as part of this, I also fixed the user blocking bug, see below.

Changes in that patch:
- Return array with success message, errors and warnings
- Slightly changed privatemsg_new_thread and privatemg_reply to work with updated validate function
- Some changes to pm_send_validate to output errors and warnings

- Pass $message by reference to _privatemsg_validate_message and remove $author and $show_warnings parameter. $author is already included in $message, $show_warnings is not needed anymore and the by reference change fixes the broken user blocking.
- change pm_send to display only usernames to which the message was sent, excluding blocked users..

Note:
- _privatemsg_send currently simply overwrites the 'success' key in privatemsg_new_thread and reply... if something would fail inside that function and we add checks to actually return false, this would mean that success would be FALSE but there aren't any errors...

berdir’s picture

StatusFileSize
new5.6 KB

Patch got lost...

berdir’s picture

Status: Needs review » Needs work

What about the following:

returned array is built up like the following:


array (
  'success' => TRUE|FALSE, // TRUE if no errors exist
  'messages' => array(
    'error' => array(),
    'warning' => array(),
    'notice' => array(),
    // ....
  )
);

The array is returned by _privatemsg_message_validate, which has a boolean flag if the current page is a form. The $messages array and the $flag are forwarded to the validate hook, too. If the flag is set, the validate function and hooks will/should set errors with form_set_error instead of adding them to $messages.

As $messages is passed around as a whole, validate hooks can add errors, warnings, notices, ... . It could for example be printed with the following code:

foreach ($messages as $messages_type) {
  foreach ($messages_type as $type => $message) {
    drupal_set_message($message, $type);
  }
}

Or a module invoking privatemsg_new_thread could decide to only display errors.

Related to that is the question if validate should be by reference or not, see #376023-1: #288183 followup: change hook_privatemsg_block_message to work on multiple recipients.

berdir’s picture

StatusFileSize
new5.64 KB

Here is a updated patch

- added a $form flag, if set to true, sub modules should use form_set_error to report errors
- $messages array, where all modules can store information according to the dsm types.

berdir’s picture

Status: Needs work » Needs review
berdir’s picture

StatusFileSize
new13.59 KB

Updated patch

- updated and added privatemgapi.test
- Changed parameters for new_thread and reply because body is now optional. It also allows to add options directly to the $message array.

berdir’s picture

StatusFileSize
new13.48 KB

Minor changes in the validate function (user permission check) and some coding style fixes.

Would be great if someone with simpletest 2.x-dev could run the tests..
- First as is
- If that works, change $recipient2 to $recipient1 on line 88 and/or 91 and check if still works.

I'm having permission issues and I can't figure out why. Also, it seems that others can't reproduce it: #385152: Clear user_access() cache

berdir’s picture

StatusFileSize
new13.82 KB

Found the reason for the failing tests, it was the static cache of user_access. See the link above.

Added same fixes for permission checking and a workaround for the mentioned bug. Should be ready now..

berdir’s picture

StatusFileSize
new13.82 KB

Re-roll with a fixed bug in privatemsg_reply() (thread_id was not used).

berdir’s picture

StatusFileSize
new13.85 KB

Re-roll.

naheemsays’s picture

Status: Needs review » Needs work

with this patch applied, when there are invalid users, there is a duplicated message as the invalid check and the error message is given twice:


  if (!empty($invalid)) {
    drupal_set_message(t('The following recipients are invalid: @invalid', array('@invalid' => implode(", ", $invalid))), 'error');
  }

  $validated = _privatemsg_validate_message($message, TRUE);
  foreach ($validated['messages'] as $type => $text) {
    drupal_set_message($text, $type);
  }
  $form_state['validate_built_message'] = $message;
  if (!empty($invalid)) {
    drupal_set_message(t('The following users will not receive this private message: !invalid', array('!invalid' => implode(", ", $invalid))), 'error');
  }
}

So from the patch just remove

+  if (!empty($invalid)) {
+    drupal_set_message(t('The following recipients are invalid: @invalid', array('@invalid' => implode(", ", $invalid))), 'error');

and it is ready to go.

naheemsays’s picture

StatusFileSize
new8.48 KB

Fixed bit in comment 12 and I am pretentious enough to set this to rtbc.

naheemsays’s picture

Status: Needs work » Reviewed & tested by the community
litwol’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +Needs documentation

Its in. thanks.

It needs API doc. for example its not very clear at first what $form = false/true does. also that function serves 2 purpses deppending on the context in which its called (also based on $form argument).

berdir’s picture

Status: Fixed » Needs review
StatusFileSize
new5.23 KB

Somehow the api tests got lost when nbz rerolled the patch :)

Re-uploading them.

litwol’s picture

Status: Needs review » Fixed

Considering that tests are only developer facing, this doesn't require must review. and in it goes ...

Status: Fixed » Closed (fixed)
Issue tags: -Needs documentation

Automatically closed -- issue fixed for 2 weeks with no activity.