Pages

Monday, August 20, 2012

Create a user account in Drupal 7 programmatically


 
  //This will generate a random password, you could set your own here
  $password = user_password(8);
 
  //set up the user fields
  $fields = array(
    'name' => 'user_name',
    'mail' => 'user_name@example.com',
    'pass' => $password,
    'status' => 1,
    'init' => 'email address',
    'roles' => array(
      DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    ),
  );
 
  //the first parameter is left blank so a new user is created
  $account = user_save('', $fields);
 
  // If you want to send the welcome email, use the following code
 
  // Manually set the password so it appears in the e-mail.
  $account->password = $fields['pass'];
 
  // Send the e-mail through the user module.
  drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', 'noreply@example..com'));
Note: You can add additional roles inside the role array using the role id as the array index and the role name as the value.
Questions? Let me know in the comments below.

Theme File Link in Drupal 7


For consistency's sake it is always a good idea to output any file download links the same way throughout any website. If you are using file uploads attached to nodes on your Drupal 7 website, this is generally taken care of for you. You will notice that all file links are displayed with a consistent icon set (which can be overridden), as well as a consistent link. This applies to all node pages and any views that contain the downloadable files.
What happens if you want to display your own download link to a file outside of views or on the node page. In my case I had to create a complex listing of nodes. In this (very rare) case, views could not handle what was needed so I had to build my own display.
I needed to output the files in a similar way to views. It turns out this is very simple. Just call the file_link theme function.
theme('file_link', array('file' => $file));
The $file variable needs to be a file object. This can be retrieved in a few different ways. If you have a fully loaded node object (as I did), you can output the field directly (thanks to Greg in the comments andhttp://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way for showing me this better approach). Simply replace the 'field_my_file_upload' with the name of your file field:
field_view_field('node', $node, 'field_my_file_upload');
If you know the file id of the file, you can use file_load function from the core file module. Note: replace $fid with the id of the file you want to display for download.
theme('file_link', array('file' => file_load($fid)));
Until next time...

Sending emails from your Ubuntu Drupal development environment in 2 minutes


I use an Ubuntu system when developing Drupal websites. When setting up a development environment in Ubuntu you may need to configure your local development environment to send emails. Here is how I configured my system in less than 2 minutes.
First thing I did was install SSMTP. This is incredibly easy in Ubuntu, just run the following from the terminal.

sudo apt-get install ssmtp
SSMTP will only allow you to send emails through another email system. You could use Gmail to send the email, however I prefer an email delivery service such as Sendgrid (used on all my production sites... and now my development sites). It makes setting up a server to send emails as painless as possible. You can sign up for a free account to try it out at https://sendgrid.com/user/signup
The next thing is to edit the SSMTP config file.

sudo vim /etc/ssmtp/ssmtp.conf
Now add the following configuration options at the bottom of the file (filling in the user name and password accordingly):

mailhub=smtp.sendgrid.net
FromLineOverride=YES
AuthUser=[SENDGRID-EMAIL]
AuthPass=[SENDGRID-PASSWORD]
AuthMethod=LOGIN

Disable Personal Contact Form in Drupal 7 Contact module


In the comments of a past post discussing best Drupal modules, it was recommended not to use Webform for contact forms. So for a project I am currently working on I decided to just use the Drupal 7 contact module that comes with core Drupal.
I have used this is the past and it works just fine. My requirements were simple, "provide a site-wide contact form that authenticated users can use to contact the site admins". On the surface it seems simple enough.
Getting this set up was extremely simple, just turn the module on, do a bit of configuration, and you easily have a simple working site wide contact form. The only problem is now every authenticated user has a checkbox that allows them to enable a "Personal contact form". While this is certainly a useful feature on some sites, this would only confuse the users of the site I was building and is definitely not in the requirements.
So the search began... I figured there must be a permission or a setting that can hide this option from users. The site I am building does not even allow users to see other user's profiles, so this option would not even be usable. However, despite my searching attempts I could find no such option.

Solution to hiding the Personal Contact Form Checkbox

There are a few options to default the personal contact form checkbox to checked/unchecked, but no option to hide this from users from the administrative interface of Drupal. In the days where usability of web applications is extremely important, having this unusable option available to users was not acceptable for me so I found the following two solutions:
1. Add this code to your template.php file of your theme (http://drupal.org/node/1233150#comment-5704336):
function yourtheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    unset($form['contact']);
  }
}
function yourmodule_form_user_profile_form_alter(&$form, &$form_state) {
  unset ($form['contact']);
}
Here is what I find wrong with these approaches:
  • If I add the first code to my theme, and decide to try out a different theme or build a new one later, I need to make sure to always add this code back or all the users on the site will begin seeing that personal contact form checkbox.
  • If I add the second set of code to a custom module, I have to manage extra code for the simple purpose of hiding unnecessary functionality. This is the better of the two options for me because I have multiple custom modules that this functionality would easily fit into. However, this seems like a lot of hassle if you need to create a module for only this one purpose.
All in all, these solutions are incredibly easy. I already have multiple custom modules and a custom theme for this website so it is really an easy fix for me. However, if you are building a simple site, and are not comfortable with PHP (which is true for a lot of new Drupal users), you might be better in all honesty to stick with using the Webform module for your site-wide contact form. If you don't plan on allowing authenticated users to login to your site, or don't mind the Personal Contact Form checkbox for your authenticated users, then the Drupal Core Contact module might be a good fit.
What do you think? Let me know in the comments below.