Sunday, January 4, 2009

Creating views for custom tables in Drupal 6



At a minimum, if your module wants to use Views, it needs to implement one hook:


function hook_views_data();


This function returns data that will describe how your module's tables relate to the Drupal node table, as well as what fields can be displayed and sorted, and how your tables may be filtered. This hook can return just one or many tables. This function should be placed in a file named MODULENAME.views.inc in your module's directory. Views will automatically find it and include it when necessary.

If you want to provide default views that your users can immediately use, implement hook_views_default_views(). You can use the views exporter tool to create this hook. This hook should be placed in MODULENAME.views_default.inc.

Implementation steps

1.Implement the hook_views_api() in your module.


function MODULENAME_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'MODULENAME') . '/modules',
);
}


2. Create a file named MODULENAME.views.inc and put it in your modules diretory.
/sites/all/modules/MODULENAME/modules/MODULENAME.views.inc

3. This .inc file must implement the hook_views_data() and the sample impelemntation is given below.

function custom_views_views_data() {

$data['custom_views']['table']['group'] = t('Custom View');

// Here 'custom_views' will be your table name and this will be treated as base table
$data['custom_views']['table']['base'] = array(
'field' => 'id',
'title' => t('Custom table'),
'help' => t('Stores information about custom table.'),
'weight' => 10,
);
// Following are the fields of the table 'custom_views'
// Field:custom_id
$data['custom_views']['custom_id'] = array(
'title' => t('ID'),
'help' => t('ID of the custom table.'),

'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);

// Field:custom_title
$data['custom_views']['custom_title'] = array(
'title' => t('Name'),
'help' => t('Name of the field.'),

'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
return $data;
}

Now when you create a new view, the custom fields will be included in the view.

For more information on views2 visit on http://drupal.org/node/235062

35 comments:

Saiprasad said...

Hey good blog .. cool posts... This one especially... Also do you follow some drupal book.. I would like to know some good book on drupal.. I dont want to learn on the cms part of it.. but would like to learn to use it as an api..

Unknown said...

Great tutorial, I have done as is here but when is show the content of the field that has HTML the view not render the html, what can I do?

Reynold Hugh said...

This is really a helpful post but I want to know that which type of template is the best for a shopping business website.
Online Drupal Templates

Naresh said...

Hi nice blog....
but i have some problem with this, how can create a node/cck using custome tables. what can i do.

Thanks in advance
Naresh

Best Shopping Sites in India said...

i tried this code.. But it is not working? Is it require to add handler functions in views.inc?

esther said...


Hi there, awesome site. I thought the topics you posted on were very interesting. I tried to add your RSS to my feed reader and it a few. take a look at it, hopefully I can add you and follow...


Web Development Company in Chennai

Unknown said...

Thanks a million! so helpful and clear.

Anonymous said...

Very interesting. I will visit this site again. Thanks! I totally agree with you. Great article. Keep up the good work SEO Company Bangalore | Web Design Company Bangalore

Anonymous said...

Great details about drupal web server drupal

Unknown said...

My cousin recommended this blog and she was totally right keep up the fantastic work!

Web Development Company in Chennai

Unknown said...

Hi,
Thanks and RI really love to read this post and I am glad to find your distinguished way of writing the post.egards
Web Design Firm Chennai

valiantsystems said...

Thanks for sharing this valuable information and it is useful for me. we also provides web design and web development company chennai

Unknown said...

Creating custom tables in the drupal website is such a difficult functionality.But experienced people could create that segment easily.
Web Designing Companies Bangalore | Website Design Bangalore

Aparajayah said...

Thanks for sharing the post. Its a useful information for every one. Please visit us Mobile Application Services.

Bangaloreweb guru said...

DruPal is one of the best Content Management System to develop the user friendly and seo friendly websites.

Web Designing Company Bangalore | Web Design Agency Bangalore

Web Services Bangalore said...

The really helpful post thanks for sharing this than another best web design and development Web Development Company | Web Designing Companies Bangalore|

Anonymous said...

Thanks for sharing valuable information .Keep posting..
PHP/MySQL Web Design |Real Estate Web Development Company

Unknown said...

Very create information about drupal..its used to my drupal website development team..i have a forward to our team....thank you for sharing the great information...

Web Design Company Bangalore | Web Development Company Bangalore

Bangaloreweb guru said...

Thanks for sharing excellent information. Your site is very cool . I am impressed by the details that you have on this site.Web Design Bangalore | Website Developers Bangalore

Unknown said...

thanks for sharing this article,your blog is one of the best blog in
internet keep post more article thank you....


Web Development Company Bangalore | Web Design Company Bangalore

Unknown said...

i tried it it is working for me thanks for sharing..hr consultancy | head hunting | HR Consultant | recruitment executive search | human resource management consultancy

Unknown said...

wow nice article really impressive with your article...
Web Designing companies Bangalore | Website Design services bangalore

Unknown said...

Web Design Bangalore Thank you for effective guide. Hope visit again..

Unknown said...

Ultimate post and very informative, you have explained well with detailed explanation. You can also share these type of content on sites like OS Business Opportunities

Ancy merina said...
This comment has been removed by the author.
Ancy merina said...
This comment has been removed by the author.
Unknown said...

Hi Sir,
This is very interesting article about Creating views for custom tables in Drupal 6. This is very useful information for online blog review readers. Keep it up such a nice posting like this.

From,
Maestro Infotech,
Web Design Company Bangalore

bhavani said...

Nice Blog. It is very interesting and more information.
website designing company in Chennai
Website design company in chennai
web designing company in chennai
web design company in chennai

Freya said...
This comment has been removed by the author.
LEON THOMAS said...

wonderful article buddy...!!
one of the best website development company i ever had.
they do good and very unique.do visit this page and know
more about this guys Web portal development company in chennai

one of their client site is now running succesfully,visit this site to know their working effort
Aluminium scaffolding hire

sharon said...

Thanks for sharing the excellent information.
professional web design company in chennai
top 10 web designing companies in chennai
web design company in chennai
seo company in chennai

Site Galleria: https://sitegalleria.com/ said...

Thanks for sharing the valuable and useful information.
web design company in bangalore
web development company in bangalore
digital marketing company in bangalore

Village Talkies said...

Great information!!! Thanks for your wonderful informative blog.
Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
As a best video production company in Bangalore, we produce quality and creative videos to our clients.

Bluelooktech said...

Your post is fantastic and thank you for sharing
Top Web and Mobile Development Company In Bangalore

Digital Orbis Creators said...

Wow! Nice Article.. Great author.. Keep posting
Digital Marketing Company in Coimbatore | SEO Company in Coimbatore | Best Digital Marketing Company in Coimbatore