1

I created a block to generate a private message link to send a message to all the users of a group. I use the php code as the text format on the block body.

<?php
$group = og_context();
$og_id = $group->gid;

$query = db_select("og_membership", "ogm");
$query->condition("ogm.gid", $og_id, "=");
$query->fields("ogm", array("entity_type", "etid"));
$result = $query->execute();

$result_tmp = $result->fetchAll();
$recipients = array();

foreach($result_tmp AS $tmem)
{
$recipients[] =  user_load($tmem->etid);
}

echo '<a href=/';
echo privatemsg_get_link($recipients,$account = array(),$subject = NULL);
echo '>Send message to ehood members</a>';
?>

When I use this, It created me a notice message saying -

Notice: Trying to get property of non-object in privatemsg_user_access() (line 335 of /sites/all/modules/privatemsg/privatemsg.module).
Notice: Trying to get property of non-object in privatemsg_user_access() (line 335 of /sites/all/modules/privatemsg/privatemsg.module).

Although it generated the link, It creates the error. I think its somehow related to how $account is mentioned ?

i tried to send global $user instead of $account, but still creates the same error.

3

In this case, the problem is that privatemsg_get_link() uses array() as default value for $account, but then uses the following code.

  if ($account == NULL) {
    global $user;
    $account = $user;
  }

The function should use NULL as default value for $account, in the same way user_access() does.

function user_access($string, $account = NULL) {
  global $user;

  if (!isset($account)) {
    $account = $user;
  }

  // ...
}

The error message you see is because the function tries to access $account->uid when $account is an empty array. An array doesn't have properties, and that is what the message is saying.
As the function is passing that value to user_access(), the same error would be raised from user_access(), which check the value of $account->uid, and for which an empty array is not the correct value to pass to its $account parameter, which requires an user object, or NULL.

It's fine to use the default values a function has, but you should check what the function does when it gets the default values for its parameters.

It is wrong to call a function as privatemsg_get_link($recipients, $account = array(), $subject = NULL) when you want to use the default values for its parameters. Just call the function as privatemsg_get_link($recipients). With your code, you are setting $account to an empty array, and passing $account as parameter.
PHP doesn't have named parameters, but only positional parameters. If it had named parameters, it would be possible to call a function indifferently as functioname(34, param3 = 4, param2 = 12), or functioname(34, param2 = 12, param3 = 4).

| improve this answer | |
0

privatemsg_get_link($recipients,$account = array(),$subject = NULL); is not the proper way to call a function. You should be passing in values for $account and $subject, not using what the function defines as default values. Consider this:

<?php
$group = og_context();
$og_id = $group->gid;

... more code ...

global $user
privatemsg_get_link($recipients, $user, 'Some subject line');
?>

When a function defines default values, like

function my_cool_function($array = array()) {
  // If the array passed to the function is empty, or if an array WASN'T passed...
  if ($array == NULL ) {
    // Create one.
    $array = array('some cool stuff');
  }
}

you shouldn't pass in the same default values, but rather real ones.

Also, I would verify that user_load() returned a valid user object before passing it along. What if it returned FALSE as it couldn't load a user? Something like:

if ($account = user_load($row->etid)) {
  $recipients[] = $account;
}

Protip! You don't have to run ->fetchAll() on a db_select object if you've just run ->execute() – you could just do

$result = db_select('table', 't')->fields('t', array('id'))->execute();
foreach ($result as $row) {
  print $row->id;
}

The fetchAll() method, among other fetch methods, is very useful if you're passing data around or using it in other ways, but if you're just iterating in a foreach() loop you don't need it.

Post back here if none of this solves the issue and I'll revise my answer. Also, do the community a favor and revise your question title to be more of a question, including the "pivatemsg" module (or whatever it's called) since this questions is explicitly regarding that module.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.