Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

In this Discussion

Pre-populating recipients for Private Messages

For composing new messages, is it possible to pre-populate the "To" field with a couple of existing users?

Comments

  • After a little digging, I've found a solution for anyone who are facing the same requirement.

    Override the New Message view by copying the application/conversations/views/messages/add.php file to your theme.

    add a value property to the To textBox with the usernames (separated by commas, no spaces) that you want to pre-populate the recipients field with:
    echo wrap($this->Form->textBox('To', ['MultiLine' => true, 'class' => 'MultiComplete', 'value' => 'username1,username2']), 'div', ['class' => 'TextBoxWrapper']);

    That's it! Hope this helps someone out someday.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Always great to see that someone finds his solutions himself.

    I was curious if there is a way to do this without extra effort, but the code doesn't allow multiple recipients.

    If you already have a custom theme you might be interested to see a solution that doesn't require a view override. Though view overrides are quite comfortable at first sight, they need much more attention during updates.

    Moreover your solution isn't flexible: you always have to hardcode the list of users.

    If you want to, create a themehooks file and add this code:

        public function messagesController_render_before($sender) {
            // Test for GET parameters: ?to[]=user1&to[]=user2
            $recipients = $sender->Request->get('to', []);
            if (!is_array($recipients)) {
                $recipient = (array)$recipients;
            }
    
            // Add user from "conventional" method.
            array_unshift($recipients, $sender->Form->getValue('To', ''));
    
            // Loop through all recipients.
            foreach ($recipients as $key => $recipient) {
                if ($recipient == '') {
                    // Remove empty string from array.
                    unset($recipients[$key]);
                } else {
                    if (!Gdn::userModel()->getByUsername($recipient)) {
                        // If this recipient is no valid user name, add an error.
                        $sender->Form->setValidationResults(
                            [
                                'RecipientUserID' => [
                                    sprintf(
                                        '"%s" is an unknown username.',
                                        htmlspecialchars($recipient)
                                    )
                                ]
                            ]
                        );
                        // Remove invalid string from array.
                        unset($recipients[$key]);
                    }
                }
            }
    
            $sender->Form->setValue('To', implode(',', $recipients));
        }
    

    Then you will be able to do something like example.com/message/add/user1/subject?to[]=user2&to[]=user3&to[]=user4. You can certainly do example.com/message/add?to[]=user11&to[]=user12, too.

    whu606Shadowdare
  • ohbikoohbiko New
    edited July 7

    Thanks @R_J.

    I'm trying your method (which is a lot better for sure) and have created a themehooks file. However it doesn't seem to be working. I might have done it wrong (sorry this is my first vanillaforums implementation).

    I've created the file class.themehooks.php to the root of my theme folder (/themes/mytheme/class.themehooks.php). As instructed in the theme hooks docs, I've declared a MyThemeNameThemeHooks class using the documented example. The hook doesn't seem to be working as the Respond button that's supposed to be created in the Discussion pages isn't there (discussionController_afterDiscussionTitle_handler).

    Did I miss any step? Can you please help?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    If you add a themehooks file to an existing theme, I think you have to manually delete the theme-related files in the /cache folder.

    Vanilla caches the info about a theme. You can check this by looking at /cache/theme/name-of-your-theme.php. This files holds an info about possible themehook files and if there is no file/class referenced here,
    a) the cached file needs to be deleted so that the theme info gets updated
    b) or if a) wasn't successful, your file is malformed, due to the place where it is saved, the name you have chosen for that file or the PHP class name that is defined inside the file.

  • @R_J Thank you that worked.

    Do you know how I can set the placeholder attribute value of a field in the themehooks file? I looked in the forms class file and tried setData and setModel but no go.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Attributes are passed with an array when you create a new form element: $sender->Form->textBox('YourTextBox', ['placeholder' => t('Some text, made translatable by using the t() function')]);

    But it sounds as if you would like to add a placeholder to an existing form element. I do not know how that can be achieved.

Sign In or Register to comment.