Hello, I use private message module on my site, but since it's not a social site, the purpose of these messages is only for users to write to site admin. My question is: can I set the field where you should put the name of the user you want to write to, so that this field results precompiled with "Admin" (or else)? Problem is the admnistrator should maintain the possibility to write to anyone, so I don't know if I really can do that!!! Hope I explained myself clearly. Tkx everybody!!

Comments

kong’s picture

This is written in Thai but the code should explain itself. The idea is to use hook_form_alter() to set the value of "recipient" field explicitly.

How to Limit Privatemsg Recipients to Specific Users

giozzz’s picture

Hello, and thanks for the tip! since I'm a noob, and I know nothing at all, I have a question: this code, should I post it inside some file already existing into privatemsg module directory, or should I create a new sheet, something like mymodule...?
Hope you'll answer, and thanks again

kong’s picture

You'll have to create a new module. This should be a good place to start: Module developer's guide

chrisshattuck’s picture

Hi there,

As kong mentioned, you'll need to create a new module. If you want a quick video demo, check out the "Build your first module" on Build a Module.com, and then follow it up with the "Using the Forms API" video. Here's what you'll need to do once you've created your module.

Let's say you named your module 'glue'. You'll create a function called 'glue_form_alter', like so:

  function glue_form_alter(&$form, $form_state, $form_id) {
  }

Inside of that function is where you can edit any form defined in Drupal. You'll need to get the $form_id of the form you're editing. I use a PHP debugger to get this information, but you can also use something like this to figure it out:

  function glue_form_alter(&$form, $form_state, $form_id) {
    echo $form_id;
  }

Once you know the $form_id you want, you'll need to set the '#default_value' parameter of the input you want to pre-populate. If you don't have a debugger, you can use the following to get the full array of the form variables:

  function glue_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
      case 'form_id_goes_here':
        die(var_dump($form));
    }
  }

After looking at that exploded array, you should be able to find the form item you want to change. Then use the following:

  function glue_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
      case 'form_id_goes_here':
        $form['the_element']['#default_value'] = 'Default value here';
        break;
    }
  }

Hope that helps!

Cheers,
Chris Shattuck

Speedy Drupal development training

Learn virtually any aspect of Drupal on BuildAModule, where I've recorded over 2200 video tutorials.

giozzz’s picture

it sure helps!!! thanks a lot for your commitment! don't know what I'd do if you all weren't there to explain!!!!! Now I try, and tks again!