Creating individual fields from a multi-column Field API widget This references the Addressfield and Views modules.

I would like to have extended Views support for the Addressfield module like the Location module does. Some of the extra fields that the Location module makes available are Country, Longitude, and Latitude by themselves and Proximity or Distance filters and sorts.

I've added longitude and latitude columns to the Addressfield module.

The database for the Location module is fairly straight forward in Drupal 7 but not built using the D7 Field API.

The Addressfield module is built using the Field API and with Views it has a Content: field available and also arguments, sorts and filters based on each of the columns in the Addressfield database table. Views only offers a choice of formatters on a single field.

This is fine because it's just as easy to add a formatter to a Fields API module as it is to theme a view . So I can make a formatter that only shows latitude or longitude. Perhaps, better yet, I can make a formatter that displays the latitude and longitude as a WKT point to be used for an OpenLayers map.

However, what I need is a proximity filter so I'm setting out how to extend the Field API's functionality in Views which isn't very straight forward.

Is there documentation available for the particular case or modules that extend the Fields API which can be used as examples?

I thought the logical thing to do was use hook_field_views_data_alter(). Here is the attempted code and I'm not sure why it doesn't work.

function addressfield_field_views_data_alter (&amp;$result, $field) {
  if (array_key_exists('field_data_field_address', $result)) {
    if ($result['field_data_field_address']) {

      $columns = array();
      $columns = array(
        'field_address_administrative_area',
        'field_address_country',
        'field_address_lat',
        'field_address_lng',
        'field_address_locality',
        'field_address_postal_code',
        'field_address_premise',
        'field_address_thoroughfare',
      );

      $add_fields = array('entity_type', 'delta', 'language', 'bundle');
      foreach ($result['field_data_field_address'] as $column =&gt; $data) {
        if (in_array($column, $columns)){
          $result['field_data_field_address'][$column]['field'] = array(
            'field' =&gt; $column,
            'table' =&gt; 'field_data_field_address',
            'handler' =&gt; 'views_handler_field_field',
            'click sortable' =&gt; TRUE,
            'field_name' =&gt; $column,
            'additional fields' =&gt; $add_fields,

            'entity_tables' =&gt; array(
              'node' =&gt; 'node',
              'node_revision' =&gt; 'node',
            ),
            'element type' =&gt; 'div',
            'is revision' =&gt; FALSE,
            'entity id' =&gt; 'nid',
            'base table' =&gt; 'node',
          );

          $result['field_data_field_address'][$column]['group'] = 'Content';
        }
      }
    }
  }
}
Here is the solution.

/**
*  Implements hook_field_views_data_alter
* 
* @param mixed $result
* @param mixed $field
*/
function addressfield_field_views_data_alter (&amp;$result, $field) {
  if (array_key_exists('field_data_field_address', $result)) {
    $result['field_data_field_address']['field_address_country'] += array(
      'title' =&gt; t('Country'),
      'help' =&gt; t('The Country name of the field'),
      'field' =&gt; array(
        'handler' =&gt; 'addressfield_views_handler_field_country',
        'click sortable' =&gt; TRUE,
      ),
    );
  }
}


Or if you want to not use a real field in the table such as field_address_country but to instead alias a field you can add a field and table attribute to the field array like this.

function addressfield_field_views_data_alter (&amp;$result, $field) {
  if ($field['type'] == 'addressfield')) {
      $result['field_data_field_address']['country'] = array(
        'title' =&gt; t('Country'),
        'group' =&gt; 'Bad Ass',
        'help' =&gt; t('The Country name of the field'),
        'field' =&gt; array(
          'field' =&gt; 'field_address_country',
          'table' =&gt; 'field_data_field_address',
          'handler' =&gt; 'views_handler_field',
          'click sortable' =&gt; TRUE,
        ),
      );
   }
}