0

I have a custom table created using .install file in my custom module.

+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| first_name    | varchar(255)     | NO   |     | NULL    |                |
| last_name     | varchar(255)     | YES  |     | NULL    |                |
| email_address | varchar(255)     | NO   |     | NULL    |                |
| user_country  | varchar(50)      | NO   |     | NULL    |                |
| user_city     | varchar(50)      | NO   |     | NULL    |                |
| address       | varchar(600)     | NO   |     | NULL    |                |
| gender        | varchar(10)      | NO   |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

And in same module, created form for reading inputs from user and a submit handler for inserting those values inside this schema. It works fine. But now I need to fetch the data and display in Views. How can I do that? I have created a hook_views_api as:

   function MY_MODULE_views_api() {
  return array(
    ‘api’ => 3,
  );
}

In hook_view_data(),

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

 $data['myuser']['id'] = array(
    'title' => t('id'),
    'help' => t('Just a numeric field.'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

$data['myuser']['first_name'] = array(
    'title' => t('First Name'), 
    'help' => t('To get first Name.'), 
    '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['myuser']['last_name'] = array(
    'title' => t('last Name'), 
    'help' => t('To get last Name.'), 
    '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['myuser']['email_address'] = array(
    'title' => t('Email Address'), 
    'help' => t('To get Email Address.'), 
    '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['myuser']['user_country'] = array(
    'title' => t('User Country'), 
    'help' => t('To get User Country.'), 
    '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['myuser']['user_city'] = array(
    'title' => t('User City'), 
    'help' => t('To get User City.'), 
    '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['myuser']['address'] = array(
    'title' => t('Address'), 
    'help' => t('To get ADdress.'), 
    '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['myuser']['gender'] = array(
    'title' => t('Gender'), 
    'help' => t('To get Gender.'), 
    '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',
    ),
  );

}

but now this is not showing up in Add View. What else I have missed? I am searching this days and today its dead line for finishing this. Please need some help on this.

1 Answer 1

1

Just use the Data module that gives you a UI to create a new simple table with fields you have above. Or, you can "adopt" existing MySQL tables into data. Data will then let you easily make Views of the mysql table and setup the hook_views_data properly for you.

If you want to do all that yourself because you cant use Data or 'alpha' contrib modules I recommend looking at Data modules source code to see how they implement the hook functions you're having trouble with.

Also note that in your hook_views_data() you forgot to return $data as in the official API documentation. That could be your problem. Still, you would have custom code that basically is part of the Data module.

3
  • Thanks.. I tried with Data Module. but there was no way for including an existing table.. this table was created in a module.install file. Is it possible to do that with data module. And return $data i have included.. Commented Oct 27, 2015 at 16:17
  • Umm the Data module can adopt any table not owned by a module created by a schema function in drupal from the url /admin/structure/data/adopt. If you made it using hook_schema then ok. I recommend checking your hook_data_views function as a mentioned to include a return value.
    – tenken
    Commented Oct 27, 2015 at 16:21
  • Its done.. I had code for all functions in single mudule file. When moed to separate module, it worked Commented Oct 27, 2015 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.