hook_views_api makes it possible to define a template path for your module where you can store views templates. When you have specified this key views automatically uses the template files for the views. Features as it stands does not include this in the version of hook_views_api it adds to the features.inc file in the features module. It would be a nice feature to include this either if the folder 'views-templates' exists in the module directory or maybe if a variable were specified in the info file e.g. features[views_template_path] = "theme/views-templates"

An example hook_views_api implementation is:

/**
 * Implements hook_views_api().
 */
function mymodule_views_api() {
  list($module, $api) = func_get_args();
  if ($module == "views" && $api == "views_default") {
    return array(
      "version" => "3.0",
      "template path" => drupal_get_path('module', 'mymodule') . "/views-templates",
    );
  }
}

Comments

njcheng’s picture

This would be incredibly useful.

hefox’s picture

An alternate solution to this is when adding suggestion for these, check if the suggestion is already added to this feature, and if it's not, see if the hook is already implemented. If it is, don't autocreate this. This would allow people to do the hook in the .module and construct it any way they want.

I've done that before with features; it's like one or two line change, if I recall correctly.

njcheng’s picture

@hefox, thanks so much for the comment. Can you be a bit more specific or include an example of how to do this?

hefox’s picture

The views_api being included is suggested in the hook_features_export of views (which is made via ctools in d7). That hook is passed the module name. Using that module name, can look up and see if that views_api component is currently part of export (features component map function may have that info)? If it's not included, but function_exists for $module_name _views_api, then don't suggest it.

jantimon’s picture

For now I am using an ugly workaround for this:

features.ctools.inc line 68

    if ($info = _ctools_features_get_info($component)) {
      $code[] = '  $info = array();';
      $code[] = '  if ($module == "'. $info['module'] .'" && $api == "'. $info['api'] .'") {';
      $code[] = '    $info = array("version" => "' . $info['current_version'] . '");';
      $code[] = '  }';
      $code[] = '  if (function_exists("' . $module . '_plugin_api_overwrite")) {';
      $code[] = '    ' . $module . '_plugin_api_overwrite($info, $module, $api);';
      $code[] = '  }';
      $code[] = '  if (!empty($info)) {';
      $code[] = '    return $info;';
      $code[] = '  }';
    }

This enables me to implement a plugin_api_overwrite method and implement
custom views handlers.

function mymodule_views_api_overwrite() {
  list($module, $api) = func_get_args();
  return array(
    "version" => "3.0",
    "template path" => drupal_get_path('module', 'mymodule') . "/views-templates",
  );
}
johnennew’s picture

Here is another workaround which just involves hooks to tell features not to include the hook_views_api function in a features module. You can then specify the function yourself in the features module .module file.

The features hook is:

/**
 * Implements hook_features_export_alter().
 *
 * Alter the final export array just prior to the rendering of
 * defaults.
 * FORBID the hook_views_api, we are going to add that ourself.
 *
 * @param array &$export
 *   By reference. An array of all components to be exported with a given
 *   feature.
 * @param array $module_name
 *   The name of the feature module to be generated.
 */
function mymodule_features_export_alter(&$export, $module_name) {
  if ($module_name == 'mymodule' && !empty($export['features']['ctools']['views:views_default:3.0'])) {
    unset($export['features']['ctools']['views:views_default:3.0']);
  }
}

Some more detailed instructions can be found here:
http://deeson-online.co.uk/labs/views-templates-features-module

njcheng’s picture

@ceng, thanks so much for this work-around. Totally worked.

milesw’s picture

For those arriving at this thread, a commenter on the blog post in #6 pointed out there is now a hook_views_api_alter() available.

This issue is supposedly fixed in Features already.

mpotter’s picture

Status: Active » Closed (fixed)
ice5nake’s picture

Porting hook_views_api_alter() to 6.x?

kenorb’s picture

Example:

/**
 * Alter the list of modules/themes which implement a views api.
 *
 * @param array $list
 *   An array of informations about the implementors of a views api.
 *   The key of this array are the module names/theme names.
 */
function foo_views_api_alter(&$list) {
  // Alter the path of the views implementation.
  $path = drupal_get_path('module', 'foo');
  $list['foo']['path'] = $path;
  $list['foo']['template path'] = $path . '/templates';
}