Modifying the Job Posting Module

Posted by Vincenzo Gambino on 20 Jan, 2010

In this blog, I will explain the steps taken to modify the job posting module to our specifications. Although I already mentioned “our specifications”, the modifications might be useful to other Drupal fellas too. Let’s get into action!

About the job posting module

The module itself is quite lightweight, but it was really useful for us in developing a job search site. It creates a new content type to which users are able to send applications. The application cover letters along with the file attachments are emailed to the author of the job posting. That’s all we need! Well… not entirely. The default application form has 4 displayed form fields, Title (item), Applicant (item), Cover letter (text area), Resume (file). In our case, we needed to get rid of the cover letter and resume fields, but we needed to add a contact number field. We use node profile module for the registration process and we wanted Drupal to pre-fill the contact field value on the job posting application form, so users only have to touch it if they have to. So we are about to start with a hook_form_alter()

Let’s have a look at the original form

'value', '#value' => $reference, );
$form['job_posting_title'] = array( '#type' => 'value', '#value' => $title, );
$form['job_posting_email'] = array( '#type' => 'value', '#value' => $email, ); // displayed fields
$form['job_posting_title_ref'] = array( '#type' => 'item', '#title' => t('Title'), '#value' => t('@title (@reference)', array('@title' => $title, '@reference' => $reference)), );
$form['job_posting_applicant'] = array( '#type' => 'item', '#title' => t('Applicant'), '#value' => $user->name .' <'. $user->mail .'>', '#maxlength' => 64, );
$form['job_posting_coverletter'] = array( '#type' => 'textarea', '#title' => t('Cover letter'), '#default_value' => t('@coverletter', array( '@coverletter' =>
'#required' => TRUE, '#description' => t('Enter your introductory covering letter.'), );
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['job_posting_resume'] = array( '#type' => 'file', '#title' => t('Attach resume'), '#description' => t('Choose a resume file to upload along with your covering letter. The attached file can only use the following extensions (txt doc pdf) and must be less than 1MB in size.'), );
$form['job_posting_submit'] = array('#type' => 'submit', '#value' => t('Send'));
return $form; }

There are some things here we do want to keep, basically all the internal fields, because they are passing through variables to the
function that comes in the
job_posting_application_form_submit($form, $form_values)
function so basically by not touching these internal elements we ensure that the receiver of the email will see a subject, a sender, etc. What we do want to change are the displayed fields. So let’s start with adding a simple phone text field:

'textfield', '#title' => 'Phone number', '#value' => $phonenumber, ); ?>

Yeah, but how do we know the actual value of the phone number field? Since we use a content type (node profile) for the registration, we can run a database query and easily get the ‘custom_field_value’ of the currently logged in user:

uid); while($number = db_fetch_array($phone)){ $phonenumber = $number['field_phone_number_value']; //NOTE: edit the field name and the content type name above to match yours } ?>

Let’s have a look at what we have done so far:

array($form,$form_values)); // Custom form handler - we will discuss this later // displayed fields
$phone = db_query("SELECT * FROM {node} n JOIN {content_type_candidate_reg} c ON n.nid = c.nid WHERE uid = '%s'", $user->uid);
while($number = db_fetch_array($phone)){ $phonenumber = $number['field_phone_number_value']; }
$form['job_posting_phone'] = array( '#type' => 'textfield', '#title' => 'Phone number', '#value' => $phonenumber, ); //Hide the Cover letter field
$form['job_posting_coverletter'] = array( '#type' => 'hidden', '#required' => FALSE, ); //Hide the Resume field
$form['job_posting_resume'] = array( '#type' => 'hidden', );
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['job_posting_submit'] = array('#type' => 'submit', '#value' => t('Send'), '#weight' => 10); //positioned the submit button to the bottom of the node so the newly added phone field won't appear where it shouldn't return $form; break;

So we’ve got the phone number of the currently logged in user. Great. Drupal will auto-fill the value based on the database query. Even better! But how will the job poster know what the applicant’s phone number is? Well, we need to let them know somehow, so we will include this value in the email notification that gets sent to the author of the job node. The function we need to look at is job_posting_application_form_submit($form, $form_values)

In order to add some extra functionality to the submission, we need to define our custom form handler that will do the magic for us. We add this line to our custom_form:

array($form,$form_values)); ?>

Ok, so we defined what handler drupal should use when submitting the form, now we create the handler function:

$apply_to = $form_values['job_posting_email'];
$confirm_from = $form_values['job_posting_email'];
$confirm_to = $user->mail; $subject = t('!subject', array('!subject' => $form_values['job_posting_title'] .' ('. $form_values['job_posting_reference']. ')'), $user->language );
$headers['Content-Type'] = 'text/plain; charset=UTF-8; format=flowed;';
$body .= 'Course name: ' .$form_values['job_posting_title']. "n"; //Include the title of the node the user has applied for
$body .= 'Applicant contact number: ' . $form_values['job_posting_phone'] . "n"; // Include the applicant's contact number
$body .= $content; // send application to employer
if (drupal_mail('application', $apply_to, $subject, $body, $apply_from, $headers)) { drupal_set_message(t('Your application has been submitted. Thank you.')); // send confirmation to applicant's profile address provided they're registered
if ($user->uid != 0) { if (drupal_mail('confirmation', $confirm_to, $subject, $body, $confirm_from, $headers)) { drupal_set_message(t('A confirmation message has been sent to ') . $user->mail); // on success, send user back to job listings page drupal_goto(); }

That’s about it, job done.