You are here

function views_features_export in Features 6

Implementation of hook_features_export().

File

includes/features.views.inc, line 106

Code

function views_features_export($data, &$export, $module_name = '') {

  // Build views & add to export array
  $map = features_get_default_map('views', 'name');
  $views = array();
  $conflicts = array();
  foreach ($data as $view_name) {
    if ($view = views_get_view($view_name, TRUE)) {

      // This view is provided by another module. Add it as a dependency or
      // display a conflict message if the View is overridden.
      if (isset($map[$view_name]) && $map[$view_name] !== $module_name) {
        if ($v = views_get_view($view_name)) {
          if ($v->type === 'Overridden') {
            $conflicts[$map[$view_name]] = $view_name;
          }
          elseif ($v->type === 'Default') {
            $export['dependencies'][$map[$view_name]] = $map[$view_name];
          }
        }
      }
      else {
        $export['features']['views'][$view_name] = $view_name;
        $views[$view_name] = $view;
      }
    }
  }
  if (!empty($conflicts)) {
    $message = 'The following overridden view(s) are provided by other modules: !views. To resolve this problem either revert each view or clone each view so that modified versions can be exported with your feature.';
    $tokens = array(
      '!views' => implode(', ', $conflicts),
    );
    features_log(t($message, $tokens), 'warning');
  }

  // Only add Views API hook if there are actually views to export.
  if (!empty($export['features']['views'])) {
    $export['features']['views_api']['api:' . views_api_version()] = 'api:' . views_api_version();
    $export['dependencies']['views'] = 'views';
  }

  // Discover module dependencies
  // We need to find dependencies based on:
  // 1. handlers
  // 2. plugins (style plugins, display plugins)
  // 3. other... (e.g. imagecache display option for CCK imagefields) : (
  // Handlers
  $handlers = array(
    'fields',
    'filters',
    'arguments',
    'sort',
    'relationships',
  );
  $handler_dependencies = views_handler_dependencies();

  // Plugins
  // For now we only support dependency detection for a subset of all views plugins
  $plugins = array(
    'display',
    'style',
    'row',
    'access',
  );
  $plugin_dependencies = views_plugin_dependencies();
  foreach ($views as $view) {
    foreach ($view->display as $display) {

      // Handlers
      foreach ($handlers as $handler) {
        if (isset($display->display_options[$handler])) {
          foreach ($display->display_options[$handler] as $item) {
            if ($item['table'] && isset($handler_dependencies[$item['table']])) {
              $module = $handler_dependencies[$item['table']];
              $export['dependencies'][$module] = $module;
            }
          }
        }
      }

      // Plugins
      foreach ($plugins as $plugin_type) {
        $plugin_name = '';
        switch ($plugin_type) {
          case 'display':
            if (isset($display->display_plugin)) {
              $plugin_name = $display->display_plugin;
            }
            break;
          case 'access':
            if (isset($display->display_options['access'], $display->display_options['access']['type']) && $display->display_options['access']['type'] != 'none') {
              $plugin_name = $display->display_options['access']['type'];
            }
            break;
          default:
            if (isset($display->display_options["{$plugin_type}_plugin"])) {
              $plugin_name = $display->display_options["{$plugin_type}_plugin"];
            }
            break;
        }
        if (!empty($plugin_name) && isset($plugin_dependencies[$plugin_type][$plugin_name])) {
          $module = $plugin_dependencies[$plugin_type][$plugin_name];
          $export['dependencies'][$module] = $module;
        }
      }
    }
  }
}