Thursday, October 8, 2009

Adding validation to views

This is in continuation to my previous post. I had the view ready. the path of the view was of the form dev/%/upload.

This would mean the page view could be generated with URLs of the form -
http:///dev/Sony/upload

assuming that Sony is the company for which you want to find the uploads.

Now, we need to validate that the user who is generating this URL actually belongs to Sony.

I added those validations using PHP code snippet by clicking on arguments link as shown -


Next, go to the validation section, select the validator as PHP and enter -

global $user;
profile_load_profile($user);
return $argument == $user->profile_company;

Refer image -
You can also choose the behavior you want in case of default arguments, illegal arguments, etc

Save the view. Check the preview by giving some valid and invalid arguments.


We are all set with a foolproof view definition.



Thursday, October 1, 2009

Building views in Drupal with Profiles and Filefield cck

I have a requirement where a user has some profile fields like company name. I have some content which are papers uploaded by every user.
I need to build a view so that a user can see technical papers of all the people who work for the same company.


This means implementing views to filter on the dynamic value of a profile attribute. There are filters on Current user, user status etc. I wanted to filter on the
user attribute of company.

What I have with me -
I have implemented the following -
1. Enabled profile module
2. Added 2 fields to the user profile -

* profile_job_title - which is an autocomplete textfield
* profile_company - a select list pre populated by site admin which contains the names of the company eg - Microsoft, Google, Yahoo, etc

3. Users are created by the site admin and he assigns them the property of the company to which they belong.
4. Created a new content type 'file' which is a filefiled cck field on a node
5. When these users log in to the site they can upload their files/ technical papers.

Requirement -
I wanted a view which said Uploaded papers of your company. This page would allow the logged in user to see all papers that are submitted by users
belonging to the same company



How I achieved this -

1. I decided to pass the company name dynamically as a parameter to the view.

2. I created a new view of the type node from Views > Add -


















3. I selected formatting options like table type, fields to be displayed, etc





4. I defined a path dev/%/upload where % would be the logged in user's company name which I will be passed as an argument to the view


5. Passed the argument as dev/%/upload Hence declare that profile company would be passed as an argument

6. I defined the filter for 'Profle Company field' and selected the Override option




7. Also selected other filter on 'node type' to be file


8. Click the Override option so that the value of this filter is picked from the argument and overrides any default.


9. While doing this, you can continue watching the Live preview on the same page towards the bottom of the configuration form





10. Finally select 'Page' as Display and enter an argument for the Company field. In my eg, I have added EA Sports as an argument. Click Preview. What you see is all files uploaded by users whose Company name is EA Sports. The company field shows the company name as well.



Hope this helps ! In the next post I will show how to ensure security and add validations to the view.

Back to Drupal 6

After a short stint with Ruby on Rails, I have moved to working on Drupal again. I am working for a telecom company who have different intranet applications and a fw external marketing sites.

We are evaluating whether Drupal would be a good option to use instead of some very expensive CMS tools that they are using. So I am creating some cool Web 2.0 feature rich interactive sites.
I see that Drupal is the right answer for all their woes with these CMS.

We are also evaluating some open source search alternatives for enterprise search - Solr, etc.

Keep going Team Drupal !