Show message inbox trouble with elgg_get_entities_from metadata

I' trying to create a web service to return the inbox of the requested user but I am not able to return the messages as a string. I would appreciate if someone can help me to understand how to get the user message inbox content as string

I created a function that receive the userid but when it is called for an iduser that has multiple messages from the webservice but I am not able to display any content.

$messages = elgg_get_entities_from_metadata(array(
        'type' => 'object',
        'subtype' => 'messages',
        'metadata_name' => 'toId',
        'metadata_value' => $iduser,
        'owner_guid' => $iduser,
        'limit' => $limit = 10,
        'offset' => $offset = 0,
));

}

To be able to display the string I was trying to return either the $messages or using another function to show the content

   $string ="test1:".$messages." test2: ".array_values($messages);
    return $string;
}

But when I call this function I just was able to get the followng result returned by the webservice

{"status":0,"result":"test1:Array test2: Array"}

In case that you could help me to display the values it will be appreciated

I am using elgg version 2.0.2

  • $messages is an array of objects, you can't output it as a string.

    I would create an export function and export it as an array.

    function export_message($message) {

        return [
            'guid' => $message->guid,
            'message' => $message->description,
            'toId' => $message->toId,
            'time_created' => $message->time_created,
            // whatever other info you might need
        ];

    }

     

    Then you can use that in your service

    $messages = elgg_get_entities_from_metadata(...);
    $exported = [];
    foreach ($messages as $m) {
        $exported[] = export_message($m);
    }

    return $exported;

  • Thank you very much Matt.

    Your reply was very helpful

    Kind Regards

    Alberto

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking