Provide Views 2 support: job_posting.views.inc

defconjuan - September 1, 2008 - 22:20
Project:Job Posting
Version:6.x-2.x-dev
Component:Miscellaneous
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I can't seem to use the expire date in any views. Is there a way to expose the Job Posting fields to Views?

#1

gmarus - September 2, 2008 - 00:51

The module does not currently implement hooks into the Views 2 API, mostly because when I had time to work on this the Views 2 module wasn't so stable and documentation was scarce. I'm hoping to get around to changing that in the next few weeks but in the interim you have to work with the templates and preprocess functions to get the look and structure you want.

#2

defconjuan - September 2, 2008 - 02:25

So, I'm no PHP head but I used some of the other module.views.inc files to see what the hooks look like. The following code exposes the deadline and employer fields of the job_posting table.

Simply put the code below in a file called job_posting.views.inc in the modules\job_posting folder.

Now when create or modify views I see a group for JobPostings and the two fields I exposed.

<?php

function job_posting_views_data() {
 
$data = array();
 
 
$data['job_posting']['table']['group']  = t('JobPostings');
 
 
$data['job_posting']['table']['join'] = array(
    
'node' => array (
    
'left_field' => 'nid',
    
'field' => 'nid',
    ),
  ); 

 
$data['job_posting']['deadline'] = array(
   
'title' => t('Deadline'),
   
'help' => t('This is the deadline for applications.'),
   
'field' => array(
     
'handler' => 'views_handler_field_date',
     
'click sortable' => TRUE,
    ),
   
'sort' => array(
     
'handler' => 'views_handler_sort',
    ),
   
'filter' => array(
     
'handler' => 'views_handler_filter_date',
    ),
   
'argument' => array(
     
'handler' => 'views_handler_argument',
    ),
  );


 
$data['job_posting']['employer'] = array(
   
'title' => t('Employer'),
   
'help' => t('This employer for the job posting.'),
   
'field' => array(
     
'handler' => 'views_handler_field',
     
'click sortable' => TRUE,
    ),
   
'sort' => array(
     
'handler' => 'views_handler_sort',
    ),
   
'filter' => array(
      
'handler' => 'views_handler_filter_string',
      
'label' => t('Value type'),
    ),
  );

 
 
  return
$data;
}


?>

The thing I couldn't get in there was the country name. I tried adding an extra item with an extra join but it kept falling over. If anyone can add that, that would be awesome.

#3

defconjuan - September 3, 2008 - 13:11

For all those using the job posting module, here's a views.inc file that will expose ALL of the data stored in the job_posting and job_posting country tables to Views.

Simply put the code below in a file called job_posting.views.inc in the modules\job_posting folder, upload it and empty your Drupal cache. Then, the next time you setup a view you will see a new section for JobPostings data when you try to add a display field, sort field, filter field, or arguments to a view. If you make any improvements on it, please post it!

<?php

function job_posting_views_data() {
   
$data = array();

   
$data['job_posting']['table']['group']  = t('JobPostings');
   
$data['job_posting']['table']['join'] = array(
       
'node' => array (
           
'left_field' => 'nid',
           
'field' => 'nid',
        ),
    );    

   
$data['job_posting']['deadline'] = array(
       
'title' => t('Deadline'),
       
'help' => t('This is the deadline for applications.'),
       
'field' => array(
           
'handler' => 'views_handler_field_date',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_date',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument',
        ),
    );

   
$data['job_posting']['employer'] = array(
       
'title' => t('Employer'),
       
'help' => t('This employer for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument',
        ),
    );
   
   
$data['job_posting']['city'] = array(
       
'title' => t('City'),
       
'help' => t('This is the city for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument',
        ),
    );
   
   
$data['job_posting']['state'] = array(
       
'title' => t('State'),
       
'help' => t('This is the state for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument',
        ),
    );
   
   
$data['job_posting']['website'] = array(
       
'title' => t('Website'),
       
'help' => t('This is the website of the employer.'),
       
'field' => array(
           
'handler' => 'views_handler_field_url',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );
   
   
$data['job_posting']['contact'] = array(
       
'title' => t('Contact'),
       
'help' => t('This is the contact person for this job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );
   
   
   
$data['job_posting']['email'] = array(
       
'title' => t('Email'),
       
'help' => t('This is the email that applications will be sent to.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );


   
$data['job_posting_country']['table']['group']  = t('JobPostings');
   
$data['job_posting_country']['table']['join']['node'] = array(
       
'left_table' => 'job_posting',
       
'left_field' => 'cid',
       
'field' => 'cid',
    );

   
$data['job_posting_country']['name'] = array(
       
'title' => t('Country'),
       
'help' => t('This job country for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );

return
$data;
}

?>

#4

gmarus - September 3, 2008 - 18:59

Thanks for the 'patch'. It seems like a good start towards Views integration.
The only issue I see with it is that some fields like 'deadline' and 'location' are either displayed differently or missing altogether because the preprocessor functions in job_posting.module aren't being called. I'm assuming that this is because Views is addressing the DB directly and is NOT generating a node listing the same way that the job_posting module is. Notice that deadline gets displayed in UTC (which is how it's actually stored) but in the included module listing template it's a countdown timer. Also, views created with this no longer have helpful links to the actual job posting nodes or any of the underlying logic (like job posting expiration) that's behind using the built-in job listing preprocessor and template.
Then again, maybe this isn't a problem for anyone but me *shrug*

#5

defconjuan - September 4, 2008 - 04:08

your right about location, it does not join it together into one string (city, state, country) but as those are stored as individual feels, one could theme the view using a .tpl.php file and that would handle display and output.

as for a countdown timer, i hear you. my main goal was just having access to the data. at least for now all fields can be referenced in views (and thus custom blocks, pages, and feeds). eventually, further development to address the issues you mention would have to happen.

i have a client who wants a lightweight drupal based job posting/application app and am using this as a starting point. my partner who's more of the php guy (i'm more on backend and ui) will be doing the coding so i'll make sure he posts our progress so that you can expand/add to the module if you choose.

thanks a lot for this module!

btw - expiration can be made to work with a view filter and the nice thing is that you can also use it in arguments (ex: /jobs/country/us or /jobs/expiring/2008/09, etc)

#6

leafish_paul - September 4, 2008 - 13:17
Title:Use Expires Date in Views» Provide Views 2 support: job_posting.views.inc

Awesome, cheers defconjuan! I had written a very simple snippet to just expose the deadline field (the only one we needed). I will try and test the full version from #3 on the same project.

Also marking this issue as a duplicate of this one, seeing as though we have code to test.

#7

leafish_paul - September 4, 2008 - 13:18
Category:support request» feature request

#8

gmarus - September 9, 2008 - 17:04

"btw - expiration can be made to work with a view filter and the nice thing is that you can also use it in arguments (ex: /jobs/country/us or /jobs/expiring/2008/09, etc)"

Very cool. Didn't know about that. Thanks.

#9

bryan.scott@ffa.int - September 14, 2008 - 22:27

I am having some trouble getting this to work with the views 2.0 rc2 for drupal 6, it have been working under rc1 fine. I was wondering if anyone had had any success.
[????@????? job_posting]$ cat job_posting.views.inc

<?php
/**
* Implementation of hook_views_data()
*/
function job_posting_views_data() {
   
$data = array();

   
$data['job_posting']['table']['group']  = t('JobPostings');
   
$data['job_posting']['table']['join']['node'] = array(
     
'left_field' => 'nid',
     
'field' => 'nid',
    );

   
$data['job_posting']['deadline'] = array(
       
'title' => t('Deadline'),
       
'help' => t('This is the deadline for applications.'),
       
'field' => array(
               
'handler' => 'views_handler_field_date',
               
'click sortable' => TRUE,
        ),
       
'sort' => array(
               
'handler' => 'views_handler_sort_date',
        ),
       
'filter' => array(
               
'handler' => 'views_handler_filter_date',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_date',
        ),
    );
    return
$data;
}
?>

#10

defconjuan - September 15, 2008 - 02:01

i'm having the same problems. if you read the views 2.0 rc2 release notes it says they've made some changes to the api that will make most modules that use views require at least some updating. unfortunately, i'm having trouble finding exactly what changes were made so that i can see how to update this code.

#11

defconjuan - September 15, 2008 - 05:00

Okay, you just need to add the following code to end of your job_posting.module file:

     function job_posting_views_api() {
          return array(
            'api' => 2,
            'path' => drupal_get_path('module', 'job_posting') .'/includes',
          );
     }

Then, if you already have a modules/job_posting/job_posting.views.inc file then just move that into modules/job_posting/includes. If you don't yet have a job_posting.views.inc file then just create one and enter the latest version of the code above.

Clear your caches and try again.

#12

bryan.scott@ffa.int - September 15, 2008 - 06:18

Yep, that worked

#13

webwriter - December 16, 2008 - 15:45

This is not working for me- have there been additional changes made since this patch was updated? Thanks for any help... I really need this to work!

#14

leafish_paul - December 16, 2008 - 16:30

Still using this for simple views sorting/filtering on a Drupal 6.8 site, working OK. Make sure you add the code in #3 to a file called job_posting.views.inc in your modules/job_posting folder and follow the additional instructions in #11.

Any more info on why its not working for you? Do you not see Job Posting related fields when building a view? Do you get any watchdog/PHP errors etc?

#15

webwriter - December 16, 2008 - 21:43

Thanks for your help!

I've put in #3 in an includes directory and #11 in the bottom of the module file... and views just looks the same. No jobs data to expose.

I don't see any errors in the watchdog table.

Yes, I've cleared my cache, and even if I hadn't, it's been about 5 hours since I uploaded changes.

Any thoughts appreciated- I had to create a custom view because staff wants jobs ordered alphabetically and a few other details, but now we can't expire the listings without manually unpublishing them.

#16

defconjuan - December 23, 2008 - 22:23

I just updated a site from Drupal 6.3 to 6.8, and updated job_posting from 6.x-1.9 to 6.x-1.10. When I did the upgrade, the view hacks described herein stopped working. To get it to start working again, I did #11 again (which depends on the include in #3), then dumped my cache. After that, it worked as before.

I'd just check exact punctuation and things like that, sounds like it might be a simple typo or path/include file location problem.

#17

samwarren - December 24, 2008 - 00:10

I had similar problem before. I even re-installed the apache/php/mysql server many times trying to solve this (by getting the newest php). Today, I ran into the solution. It is easy:
Previously, the module was listed in "/sites/defaults/modules/job_posting-6.x-1.10/job_posting".
Now, I moved "/job_posting" folder directly under modules, like "/sites/defaults/modules/job_posting". Then everything works.
The older folder structure works with no problem for Drupal recognizing the module, I also like it that way to show the version number.

#18

sciencegenome - March 20, 2009 - 19:53

I followed steps 3, 9, and 11, but am not sure what to look for or where to find the "job_posting" setting in views. Defconjuan, can you please be more specific with the instructions, I am not an expert yet.
Thanks,
DP

#19

sciencegenome - March 20, 2009 - 21:56
Priority:normal» critical

#20

defconjuan - June 7, 2009 - 14:36

Just an FYI, this is still working on Drupal 6.12 with Views 6.x-2.5. To restate the instructions for those having problems moving around the comments:

Step 1 - edit job_posting.module

Add the following code to the VERY end of your job_posting.module file:

     function job_posting_views_api() {
          return array(
            'api' => 2,
            'path' => drupal_get_path('module', 'job_posting') .'/includes',
          );
     }

Step 2 - create job_posting.views.inc

Create a text file named job_posting.views.inc in your modules/job_posting/includes directory. If the includes directory doesn't exist, create it and dump the following code to the empty job_posting.views.inc file:

<?php
   
function job_posting_views_data() {
   
$data = array();

   
$data['job_posting']['table']['group']  = t('JobPostings');
   
$data['job_posting']['table']['join'] = array(
       
'node' => array (
           
'left_field' => 'nid',
           
'field' => 'nid',
        ),
    );    

   
$data['job_posting']['deadline'] = array(
       
'title' => t('Deadline'),
       
'help' => t('This is the deadline for applications.'),
       
'field' => array(
           
'handler' => 'views_handler_field_date',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort_date',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_date',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );

   
$data['job_posting']['employer'] = array(
       
'title' => t('Employer'),
       
'help' => t('This employer for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );
   
   
$data['job_posting']['city'] = array(
       
'title' => t('City'),
       
'help' => t('This is the city for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );
   
   
$data['job_posting']['state'] = array(
       
'title' => t('State'),
       
'help' => t('This is the state for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );
   
   
$data['job_posting']['website'] = array(
       
'title' => t('Website'),
       
'help' => t('This is the website of the employer.'),
       
'field' => array(
           
'handler' => 'views_handler_field_url',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );
   
   
$data['job_posting']['contact'] = array(
       
'title' => t('Contact'),
       
'help' => t('This is the contact person for this job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );
   
   
   
$data['job_posting']['email'] = array(
       
'title' => t('Email'),
       
'help' => t('This is the email that applications will be sent to.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );


   
$data['job_posting_country']['table']['group']  = t('JobPostings');
   
$data['job_posting_country']['table']['join']['node'] = array(
       
'left_table' => 'job_posting',
       
'left_field' => 'cid',
       
'field' => 'cid',
    );

   
$data['job_posting_country']['name'] = array(
       
'title' => t('Country'),
       
'help' => t('This job country for the job posting.'),
       
'field' => array(
           
'handler' => 'views_handler_field',
           
'click sortable' => TRUE,
        ),
       
'sort' => array(
           
'handler' => 'views_handler_sort',
        ),
       
'filter' => array(
           
'handler' => 'views_handler_filter_string',
        ),
       
'argument' => array(
           
'handler' => 'views_handler_argument_string',
        ),
    );

return
$data;
}
   
   
?>

Step 3 - Empty Cache, Design Event View

Empty ALL of your caches (this is imperative, most people's problems are related to this). Then the next time you design a view, you'll notice a new group of fields under a new grouping titled JobPostings. If you don't know how to use views, that's another issue for which I have no tissue....

#21

gains - July 12, 2009 - 06:23

Thanks to defconjuan code, I was able to setup views support. This being my first patch I don't know how to add new files and folders to a patch.

So job_posting_302791.patch is for job_posting.module and views_2_includes_302791.zip is the includes folder and job_posting.views.inc file.

Any feedback would be great.;)

AttachmentSize
job_posting_302791.patch 675 bytes
views_2_includes_302791.zip 1.68 KB

#22

Summit - July 12, 2009 - 06:53

subscribing, greetings, Martijn

#23

gmarus - July 13, 2009 - 16:45

Gains: Thanks for the patch submission. It looks good and I'm rolling it into a new 6.x release today. Please test and report any issues you might discover. Also, any thoughts on 5.x? I notice the views module uses a different branch for 5.x and 6.x so I'm wondering whether this patch for 6.x will work with 5.x as well.

#24

gains - July 13, 2009 - 21:40

Thanks..

The API has changed in views 6.x. Let me have a look into it.

#25

gains - August 3, 2009 - 02:47

Created a new ticket for 5.x integration.

#537038: Views support for 5.x-1.x-dev

#26

gmarus - August 11, 2009 - 17:10
Priority:critical» normal
Status:active» needs review

changed status to 'needs review' to encourage wider testing

#27

gmarus - September 30, 2009 - 17:08
Status:needs review» fixed

#28

gmarus - September 30, 2009 - 17:10
Status:fixed» closed
 
 

Drupal is a registered trademark of Dries Buytaert.