12

In modules/addressfield/addressfield.module I have:

/**
 * Implements hook_views_api().
 */
function addressfield_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'addressfield') . '/views',
  );
}

and

/**
*  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,
      ),
    );
  }
}

In modules/addressfield/views/addressfield.views.inc:

/**
 * Implementation of hook_views_handlers().
 */
function addressfield_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'addressfield') . '/views',
    ),
    'handlers' => array(
      'addressfield_views_handler_field_country' => array(
        'parent' => 'views_handler_field',
      ),
    ),
  );
}

This file doesn't get triggered in the debugger when I clear the cache. I don't understand this.

The handler is in modules/addressfield/views/addressfield_views_handler_field_country.inc which I took from the location module:

/**
 * @file
 * Country field handler.
 */

class addressfield_views_handler_field_country extends views_handler_field {

  function option_definition() {
    $options = parent::option_definition();
    $options['style'] = array('default' => 'name');
    return $options;
  }

  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['style'] = array(
      '#title' => t('Display style'),
      '#type' => 'select',
      '#options' => array('name' => t('Country name'), 'code' => t('Country code')),
      '#default_value' => $this->options['style'],
    );
  }

  function render($values) {
      return check_plain(strtoupper($values->{$this->field_alias}));
  }
}

Any advice on how to get this to work would be helpful. Thank you.

  • Just a general comment. The current api version of views itself is '3.0-alpha1', so you can't use 3. You should share your copy of views, because it's from the future ;) – Daniel Wehner Apr 26 '11 at 5:53
  • I had this problem it showed in the logs as "Notice: Uninitialized string offset: 1 in views_get_handler()" sticking here so if someone searches for that they will find this helpful page. – Jeremy French Jul 5 '12 at 15:48
  • Just a tip: execute "drush vd" once ... this will show you some debug messages if handlers are missing somehow. – Daniel Wehner Feb 10 '14 at 11:32
22

Which core version? In Drupal 7, hook_views_handlers() is gone, add your files to your .info file instead.

files[]=views/addressfield_views_handler_field_country.inc
|improve this answer|||||
  • It actually says so in the Views advanced help pages (page "Upgrading to Drupal 7 (API)") – Bojan Zivanovic Apr 9 '11 at 17:59
  • You'd think Drupal could emit a watchdog warning that it won't load the file because it's not whitelisted. I should invoice Dries for the hours I've spent chasing this down. – John Franklin Jun 19 '15 at 20:14
  • In addition to the .inc file I also had to include my MYMODULE.views.inc file in my .info file. Any reason why that might be? If I don't include it then my views plugin is not detected. – Keven Nov 10 '15 at 21:46

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.