You are here

function access_scheme_features_export_render in Access Control Kit 7

Implements hook_features_export_render().

File

./access.features.inc, line 61
Features integration for the access control kit module.

Code

function access_scheme_features_export_render($module_name, $data, $export = NULL) {
  $access_info = access_info();
  $handler_info = access_handler_info();
  $code = array();
  $code[] = '  $schemes = array();';
  foreach ($data as $scheme_machine_name) {
    $original = access_scheme_machine_name_load($scheme_machine_name, TRUE);
    if (!empty($original)) {

      // Clone the scheme before we manipulate it, so as not to alter the cache.
      $scheme = clone $original;

      // Remove properties that should not be exported.
      unset($scheme->sid);
      unset($scheme->info);
      unset($scheme->realm_field);
      unset($scheme->realms);

      // The scheme's machine name is used as the export key (to make it easier
      // to override a default scheme with an _alter() hook), so we don't need
      // it again in the exported properties.
      unset($scheme->machine_name);

      // Role IDs may differ between Drupal instances, so we need to export the
      // enabled roles by name only.
      $roles = array();
      foreach ($scheme->roles as $role_name) {
        $roles[$role_name] = $role_name;
      }
      ksort($roles);
      $scheme->roles = $roles;

      // Put the attached handlers into an exportable format.
      $handlers = array();
      foreach ($scheme->handlers as $object_type => $handler) {
        $handlers[$object_type] = array(
          'handler' => get_class($handler),
          'settings' => $handler
            ->getSettings(),
        );
      }

      // For the sake of readability in the exported code, remove the handlers
      // for now so we can add them later in separate assignment statements.
      unset($scheme->handlers);

      // Export the scheme.
      $code[] = '';
      $code[] = '  // Access scheme: ' . $scheme->name;
      $code[] = '  $schemes[\'' . $scheme_machine_name . '\'] = ' . features_var_export($scheme, '  ') . ';';

      // Export the handlers.
      if (!empty($handlers)) {
        $code[] = '  $schemes[\'' . $scheme_machine_name . '\'][\'handlers\'] = array();';
        foreach ($handlers as $object_type => $handler) {
          $code[] = '  // ' . $access_info[$object_type]['label'] . ' handler: ' . $handler_info[$handler['handler']]['label'];
          $code[] = '  $schemes[\'' . $scheme_machine_name . '\'][\'handlers\'][\'' . $object_type . '\'] = ' . features_var_export($handler, '  ') . ';';
        }
      }
    }
  }
  $code[] = '';
  $code[] = '  return $schemes;';
  $code = implode("\n", $code);
  return array(
    'access_default_schemes' => $code,
  );
}