1

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 (&$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 => $data) {
        if (in_array($column, $columns)){
          $result['field_data_field_address'][$column]['field'] = array(
            'field' => $column,
            'table' => 'field_data_field_address',
            'handler' => 'views_handler_field_field',
            'click sortable' => TRUE,
            'field_name' => $column,
            'additional fields' => $add_fields,

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

          $result['field_data_field_address'][$column]['group'] = 'Content';
        }
      }
    }
  }
}
0

Here is the solution.

/**
*  Implements hook_field_views_data_alter
* 
* @param mixed $result
* @param mixed $field
*/
function addressfield_field_views_data_alter (&$result, $field) {
  if (array_key_exists('field_data_field_address', $result)) {
    $result['field_data_field_address']['field_address_country'] += array(
      'title' => t('Country'),
      'help' => t('The Country name of the field'),
      'field' => array(
        'handler' => 'addressfield_views_handler_field_country',
        'click sortable' => 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 (&$result, $field) {
  if ($field['type'] == 'addressfield')) {
      $result['field_data_field_address']['country'] = array(
        'title' => t('Country'),
        'group' => 'Bad Ass',
        'help' => t('The Country name of the field'),
        'field' => array(
          'field' => 'field_address_country',
          'table' => 'field_data_field_address',
          'handler' => 'views_handler_field',
          'click sortable' => TRUE,
        ),
      );
   }
}

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.