Wednesday 13 July 2016

How to show content through Views in Drupal 7 from separate Database table?

/**
 * Implementation of hook_views_api
 */
function commportal_search_views_api() {
   return array(
      'api' => 3,
   );
}
/**
 * Implementation of hook_views_data
* custom is Module Name
custom_users is Table Name
* my_db is Database Name
 */
function  custom_views_data() {
    $data['custom_users']['table']['group'] = 'custom_users';
        $data['custom_users']['table']['base'] = array(
          // Use the first column's name as the primary field.
          'field' => 'id',
          'title' => 'custom_users',
          'database' => 'my_db',
          'weight' => -9001,
        );
/* Add Numeric Field Handlers */
    $numericFields = array('id', 'roll_number');
    foreach($numericFields as $fieldId){
        $data['custom_users'][$fieldId] = array(
            'title' => $fieldId,
            'help' => $fieldId,
            'field' => array(
            'handler' => 'views_handler_field_numeric',
                'click sortable' => TRUE,
            ),
            'sort' => array(
                'handler' => 'views_handler_sort',
            ),
            'filter' => array(
                'handler' => 'views_handler_filter_numeric',
            ),
            'argument' => array(
                'handler' => 'views_handler_argument_numeric',
            ),
        );
    }
/* Add String Field Handlers */
    $stringFields = array('email', 'first_name', 'last_name');
    foreach($stringFields as $fieldId){
        $data['custom_users'][$fieldId] = array(
            'title' => $fieldId,
            'help' => $fieldId,
            '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;
}

No comments:

Post a Comment

Your comment is so valuable as it would help me in my growth of knowledge.