7

I have implemented hook_field_views_data() in mymodule.views.inc but the hook is not picked up.

In mymodule.module I have the following code to define where mymodule.views.inc resides.

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

Below is the code for views/mymodule.views.inc. And the problem is that if I try dumping out $field for example it seems like hook_field_views_data() is not picked up.

I tried to change mymodule_field_views_data() to mymodule_views_data, and that function is picked up. I tried this just to be sure that the file is included at all.

/**
 * Implements hook_field_views_data().
 */
function mymodule_field_views_data($field) {
  $data = field_views_field_default_views_data($field);

  // Add a handler for localities.
  foreach ($field['storage']['details']['sql'] as $type => $tables) {
    foreach ($tables as $table_name => $columns) {
      if (!isset($columns['locality'])) {
        continue;
      }

      $column_real_name = $columns['locality'];
      if (!isset($data[$table_name][$column_real_name]['filter'])) {
        continue;
      }

      $data[$table_name][$column_real_name]['filter']['handler'] = 'mymodule_views_handler_filter_locality';
    }
  }

  return $data;
}

mymodule.info

name = mymodule
description = Bla bla
core = 7.x

files[] = views/mymodule_views_handler_filter_locality.inc
3

That appears to be the correct behaviour.

If you look at the code for field_views_data():

function field_views_data() {
  $data = array();
  foreach (field_info_fields() as $field) {
    if ($field['storage']['type'] != 'field_sql_storage') {
      continue;
    }

    $module = $field['module'];
    $result = (array) module_invoke($module, 'field_views_data', $field);

    if (empty($result)) {
      $result = field_views_field_default_views_data($field);
    }
    drupal_alter('field_views_data', $result, $field, $module);

    if (is_array($result)) {
      $data = drupal_array_merge_deep($result, $data);
    }
  }

  return $data;
}

You can see it only invokes hook_field_views_data() for the module that defines the field. So in the case of (what looks like) an addressfield, the function Views will call has to be called addressfield_field_views_data().

It's more likely you're looking for hook_field_views_data_alter() which will allow you to alter existing definitions.

But if that doesn't do what you need, you might have to actually define the addressfield_field_views_data() function yourself on behalf of addressfield (if it's not already implemented somewhere). Should be a pretty quick test to find out.

1
  • 1
    Yes, you're right. I missed out that it has to be the module defining the field that can implement hook_field_views_data(). I will give hook_field_views_data_alter() a go. – Pontus Nilsson Aug 27 '13 at 10:40

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.