You are here

function flag_export_flags in Flag 7.3

Same name and namespace in other branches
  1. 6.2 includes/flag.export.inc \flag_export_flags()
  2. 7.2 includes/flag.export.inc \flag_export_flags()

Export a flag to code.

Parameters

array $flags: An array of flag objects, or flag name.

string $module: Optional. The name of the module that will be created if exporting to use in hook_flag_default_flags().

2 calls to flag_export_flags()
flag_export_form in includes/flag.export.inc
Export a flag and display it in a form.
flag_features_export_render in includes/flag.features.inc
Implements hook_features_export_render().

File

includes/flag.export.inc, line 17
Import/Export functionality provided by Flag module.

Code

function flag_export_flags($flags = array(), $module = '', $indent = '') {

  // For features_var_export() (optional).
  module_load_include('inc', 'features', 'features.export');
  $output = $indent . '$flags = array();' . "\n";
  foreach ($flags as $item) {
    if (is_object($item)) {
      $flag = $item;
    }
    else {

      // We got just the flag name, for example from the features
      // implementation.
      if (!($flag = flag_load($item, TRUE))) {
        continue;
      }
    }
    if (!$flag
      ->is_compatible()) {
      drupal_set_message(t('Could not export flag %flag-name: Your flag was created by a different version of the Flag module than is now being used.', array(
        '%flag-name' => $flag->name,
      )), 'error');
      continue;
    }
    $flag->api_version = FLAG_API_VERSION;
    $new_flag = (array) $flag;
    if (!empty($module)) {

      // Even though Flag adds the module name itself later, we add the module
      // name here for reference by other modules (such as Features).
      $new_flag['module'] = $module;

      // Lock the flag name, as is normally desired by modules using
      // hook_flag_default_flags(), and needed by Features.
      $new_flag['locked'] = array(
        'name',
      );
    }

    // Allow other modules to change the exported flag.
    drupal_alter('flag_export', $new_flag);

    // Remove properties we don't export.
    $unset_properties = array(
      // Remove the flag ID.
      'fid',
      // The name is emitted as the key for the array.
      'name',
      // The entity info is just used as helper data.
      'entity_info',
      // Remove roles.
      'roles',
      // Remove errors.
      'errors',
    );
    foreach ($unset_properties as $property) {
      unset($new_flag[$property]);
    }
    $output .= $indent . '// Exported flag: "' . check_plain($flag
      ->get_title()) . '"' . ".\n";
    $output .= $indent . '$flags[\'' . $flag->name . '\'] = ' . (function_exists('features_var_export') ? features_var_export($new_flag, $indent) : var_export($new_flag, TRUE)) . ";\n";
  }
  $output .= $indent . 'return $flags;' . "\n";
  return $output;
}