0

I want to create a view using a table that is already present in the database, I used the hooks_views_data function to describe the tables and its fields. While creating a new view I can see my custom table listed in the view's selection option, but when I use the custom table I don't see the field name for my table instead I see " Array: Array (Array)" in the field selection option.

Database:

                                    Table "chado.biomaterial_value"
          Column        |  Type  |                               Modifiers                                
---------------------+--------+---------------------------------------------------               ---------------------
          name                | text   | 
          cultivar            | text   | 
          age                 | text   | 
          tissue              | text   | 
          treatment           | text   | 
          phenotype           | text   | 
          developmental_stage | text   | 
          source              | text   | 
          bio_val_id          | bigint | not null default nextval('biomaterial_value_bio_val_id_seq'::regclass)
     Indexes:
     "biomaterial_value_pkey" PRIMARY KEY, btree (bio_val_id)

Things that I have implemented: bioviews.info

name = Bio_Views
description = Custom module For learning views coding
core=7.x
version=VERSION
dependencies[] = views

bioviews.module

<?php
function bioviews_views_api() {
return array(
    'api' => '3.0-alpha1',
    'path' => drupal_get_path('module','bioviews'). '/views',
);
}

views/bioviews.views.inc:

 <?php

  /**
  * @file
  * Views integration for the mydons custom module
  */

 /**
  * Implements hook_views_data()
  *
  */
  function bioviews_views_data() {
     $data = array();

      // Table name is "biomaterial_value".
      $data['biomaterial_value']['table']['group'] = t('Biomaterial');

      $data['biomaterial_value']['table']['base'] = array(
           'field' => 'bio_val_id',  //Primary key
           'title' => 'biomaterial_value custom table',
           'help' => 'biomaterial_value custom database table'  
         );

   // Description of name field

    $data['biomaterial_value']['name'] = array(
         'title' => t('NAME'), 
         'help' => t('biomaterial_val name field'), 
         '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',
          ),
       );

  // Description of cultivar field

  $data['biomaterial_value']['cultivar'] = array(
      'title' => t('CULTIVAR'), 
      'help' => t('biomaterial_val cultivar field'), 
      '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',
  ),
);

 // Description of the age field

  $data['biomaterial_value']['age'] = array(
      'title' => t('AGE'),
      'help' => t('biomaterial_val age field'),
      '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',
         ),
      );

   //Description of the tissue field

   $data['biomaterial_value']['tissue'] = array(
       'title' => t('TISSUE'), 
       'help' => t('biomaterial_val tissue field.'), 
       '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',
        ),
     );

    // Description of the treatment field

    $data['biomaterial_value']['treatment'] = array(
        'title' => t('TREATMENT'), 
        'help' => t('biomaterial_val treatment field.'), 
        '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',
         ),
     );

    // Description of the phenotype field

    $data['biomaterial_value']['phenotype'] = array(
         'title' => t('PHENOTYPE'), 
         'help' => t('phenotype field.'), 
         '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',
            ),
       );

      // Description of the developmental_stage

       $data['biomaterial_value']['developmental_stage'] = array(
            'title' => t('DEVELOPMENT STAGE'),       
             'help' => t('phenotype field.'),        
             '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',
             ),
         );

     // Description of the source field

      $data['biomaterial_value']['source'] = array(
          'title' => t('SOURCE'),       
          'help' => t('phenotype field.'),        
          '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',
           ),
      );

      // Description of bio_val_id.
       $data['biomaterial_value']['bio_val_id'] = array(
         'title' => t('ID'),
         'help' => t('biomaterial_value ID field'),
         // Is field sortable TRUE.
         'field' => array(
             'click sortable' => TRUE,
          ),
         // Filter handler for filtering records.
        'filter' => array(
           'handler' => 'views_handler_filter_numeric'
        ),
        'sort' => array(
          'handler' => 'views_handler_sort'
        )
    );

     return $data;

 }
0

Your nesting isn't quite right in hook_views_data. You have 'base' incorrectly nested under 'group'

Try something like:

/**
 * Implements hook_views_data()
 *
 */
function bioviews_views_data() {

  $data = array();

  // Assumes table name is "biomaterial_value".
  $data['biomaterial_value']['table']['group'] = t('Biomaterial');

  $data['biomaterial_value']['table']['base'] = array(
    'field' => 'bio_val_id',  //Primary key
    'title' => 'biomaterial_value custom table',
    'help' => 'biomaterial_value custom database table'  
  );

  // Description of bio_val_id.
  $data['biomaterial_value']['bio_val_id'] = array(  
    'title' => t('ID'),
    'help' => t('biomaterial_value ID field'),
    // Is field sortable TRUE.
    'field' => array(
      'click sortable' => TRUE,  
    ),
    // Filter handler for filtering records.
    'filter' => array(
       'handler' => 'views_handler_filter_numeric'   
    ),  
    'sort' => array(
      'handler' => 'views_handler_sort'       
    )
  );

  /* ... Add other fields ... */
  // $data['biomaterial_value']['field_name'] = array();

  return $data;

}

Also, as an aside, remove

files[] = bioviews.module
files[] = views/bioviews.views.inc

From bioviews.info. files[] is autoloading for OO classes.

| improve this answer | |
  • :Thanks for the suggesting the changes.I updated the code as you suggested. I still have the following error: Fields Broken/missing handler (Broken handler biomaterial_value.name) – Hrishi Jun 26 '14 at 17:06
  • You're welcome. It looks like you're using PGSQL, "name" is a reserved keyword which is probably causing that error ( e.g: postgresql.org/docs/8.2/static/sql-keywords-appendix.html ). The easiest solution would be to rename that column. – David Thomas Jun 26 '14 at 21:45
  • David:Thanks again. Renaming fixed the broken handler error. Now when I select fields for my view I can only see ID and the biomaterial_name field (renamed name->biomaterial_name). There are selection options with ':Array' as the value, but not the actual field name. – Hrishi Jun 27 '14 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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