You are here

function context_features_export in Features 6

Same name and namespace in other branches
  1. 7.2 includes/features.context.inc \context_features_export()
  2. 7 includes/features.context.inc \context_features_export()

Implementation of hook_features_export().

File

includes/features.context.inc, line 57

Code

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

  // 3.x
  if (context_features_get_version() === 3) {
    $pipe = ctools_component_features_export('context', $data, $export, $module_name);
    $contexts = context_load();
    foreach ($data as $identifier) {
      if (isset($contexts[$identifier])) {
        $context = $contexts[$identifier];

        // Conditions.
        // Currently only node and views conditions are supported.
        // @TODO: Should this be delegated to a method on the plugin?
        foreach (array(
          'node',
          'views',
        ) as $key) {
          if (!empty($context->conditions[$key]['values'])) {
            foreach ($context->conditions[$key]['values'] as $item) {

              // Special pipe for views
              if ($key === 'views') {
                $split = explode(':', $item);
                $view_name = array_shift($split);
                $pipe[$key][$view_name] = $view_name;
              }
              else {
                $pipe[$key][$item] = $item;
              }
            }
          }
        }

        // Reactions.
        if (!empty($context->reactions['block']['blocks'])) {
          foreach ($context->reactions['block']['blocks'] as $block) {
            $block = (array) $block;
            $bid = "{$block['module']}-{$block['delta']}";
            $pipe['block'][$bid] = $bid;
          }
        }
      }
    }
    return $pipe;
  }

  // 2.x
  $export['dependencies']['context'] = 'context';

  // Collect a context to module map
  $map = features_get_default_map('context', NULL, 'context_features_identifier_2');
  $pipe = array();
  $contexts = context_enabled_contexts();
  foreach ($data as $identifier) {

    // If this context is already provided by another module, add it
    // as a dependency and prevent it from becoming a duplicate export.
    if (isset($map[$identifier]) && $map[$identifier] != $module_name) {
      if (isset($export['features']['context'][$identifier])) {
        unset($export['features']['context'][$identifier]);
      }
      $module = $map[$identifier];
      $export['dependencies'][$module] = $module;
    }
    else {
      if (!empty($contexts[$identifier])) {
        $export['features']['context'][$identifier] = $identifier;
        $context = $contexts[$identifier];
        foreach (array(
          'node',
          'menu',
        ) as $key) {
          if (!empty($context->{$key})) {
            if (is_array($context->{$key})) {
              foreach ($context->{$key} as $item) {
                $pipe[$key][$item] = $item;
              }
            }
            else {
              $item = $context->{$key};
              $pipe[$key][$item] = $item;
            }
          }
        }

        // Special pipe for views
        if (!empty($context->views) && is_array($context->views)) {
          foreach ($context->views as $view_name) {
            $split = explode(':', $view_name);
            $view_name = array_shift($split);
            $pipe['views'][$view_name] = $view_name;
          }
        }

        // Special pipe for blocks
        if (!empty($context->block)) {
          foreach ($context->block as $block) {
            $block = (array) $block;
            $bid = "{$block['module']}-{$block['delta']}";
            $pipe['block'][$bid] = $bid;
          }
        }
      }
    }
  }
  return $pipe;
}