Instead of hardcoding the handlers for Views, we should do something more intelligent. A few possibilities would be to allow client modules to specify the views handler in hook_schema, or to implement a hook themselves. Or, perhaps we just want to infer a few types based on the type of index column.

Comments

sidharth_k’s picture

The following has been accomplished:

  • You can create a relationship between a SObjStore field and an arbitrary column in another table
  • You can override the field, filter and sort handlers

You would do this in the following fashion. Please see in particular 'smoker' to override a field handler (views_handler_field_boolean) and 'nid' (we could have named it anything actually but called it 'nid') for creating a relationship to the node table.

  // Please not that you should NOT add a primary key
  // sobjstore will add a primary key itself called 'id__'
  $schema['test'] = array(
    'fields' => array(
      'first_name' => array(
        'description' => 'The first name of the person',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'last_name' => array(
        'description' => 'The last name of the person',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'Favorite Page',
        // This is how we create a linkage with other tables
        // Helps in views integration
        // Syntax exactly like the views relationship specification
        'relationship' => array(
          'base' => 'node',
          'base field' => 'nid',
          // Default. Does not need to be specified
          // 'handler' => 'views_handler_relationship',
          'label' => t('Node id of favorite page'),
        ),
      ),
      'smoker' => array(
        'description' => 'Smoker?',
        'type' => 'int',
        'not null' => FALSE,
        // You can specify field, filter and sort handlers
        // Here we specify only field and filter
        // The following are the defaults:
        //  For non-numeric fields:
        // array('field' => array(
        //    'handler' => 'views_handler_field',
        //  ),
        //  'filter' => array(
        //    'handler' => 'views_handler_filter',
        //  ),
        //  'sort' => array(
        //    'handler' => 'views_handler_sort',
        //  )
        // );
        //  For numeric fields:
        // array(
        //  'field' => array(
        //    'handler' => 'views_handler_field_numeric',
        //  ),
        //  'filter' => array(
        //    'handler' => 'views_handler_filter_numeric',
        //  ),
        //  'sort' => array(
        //    'handler' => 'views_handler_sort',
        //  )
        // );
        //
        // Please note the defaults are replaced with the values you provide
        // Note: replaced in the style of array_replace_recursive
        'field' => array(
          'handler' => 'views_handler_field_boolean',
        ),
      ),
      'age' => array(
        'description' => 'The age of the person',
        'type' => 'int',
        'size' => 'normal',
        'not null' => FALSE,
      ),
      // Disabled for now so we can play around with it later
      /*      'profession' => array(
        'description' => 'The profession of the person',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        ), */
      'city' => array(
        'description' => 'The current city of residence',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
    )
  );
sidharth_k’s picture

Assigned: Unassigned » sidharth_k
Status: Active » Fixed

Completed.

Commits:
http://drupalcode.org/sandbox/sidharth_k/1358954.git/commit/71d53a5
http://drupalcode.org/sandbox/sidharth_k/1358954.git/commit/5e5cc87

To see this in operation simply see the sobjstore_example. Make sure you look at the views example at path sobjstore_example/views_example.

You will see the relationship happening in column 'Favorite Page' and use of the boolean field handler in column 'Smoker'

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.