I'm writing a module that is able to export relations through uuids (Relation UUID). I have written one component that is working about right. But knowing that all relations of each type can stack up pretty fast I would like to split up my component for each relation type. In the documentation of http://drupalcontrib.org/api/drupal/contributions!features!features.api.php/function/hook_features_api/7 there is a brief explanation for using the 'base' property.

Could someone please provide a little bit more information/documentation on how to implement such a feature?

Comments

alexverb’s picture

I figured it out. You can define the 'base' property as the component 'hook' name. Then in your hook you can add the components key as a parameter. That was kind of the missing piece in the documentation. Below is an example of how I used it in my module.

/**
 * Implements hook_features_api().
 */
function relation_uuid_features_api() {
  $components = array();

  $relation_types = relation_get_types();
  foreach ($relation_types as $key => $relation_type) {
    $components[$key] = array(
      'feature_source' => TRUE,
      'name' => t('Relation of type @name' , array('@name' => $key)),
      'base' => 'uuid_relation',
      'default_hook' => 'relation_uuid_default_content',
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
      'file' => drupal_get_path('module', 'relation_uuid') . '/includes/uuid_relation.features.inc',
    );
  }

  return $components;
}

/**
 * Implements hook_features_export_options().
 */
function uuid_relation_features_export_options($component) {
  $relation_type = $component;
  $options = array();
  // Get all possible relationships. This may become a heavy function when
  // user has a lot of relationships.
  // TODO:  Implement caching mechanism? Is that smart for this use case?
  $query = db_query('SELECT r.rid FROM {relation} r WHERE r.relation_type = :relation_type', array(':relation_type' => $relation_type));
  $rids = $query->fetchCol();
  $relations = relation_load_multiple($rids);
  foreach ($relations as $relation) {
    $endpoints = array();
    // Get the endpoints to represent relation later on.
    foreach ($relation->endpoints[LANGUAGE_NONE] as $endpoint) {
      $entity_type = $endpoint['entity_type'];
      $entity_info = entity_get_info($entity_type);
      $entity = entity_load($entity_type, array($endpoint['entity_id']));
      $entity = reset($entity);
      $endpoints[] = check_plain($entity->{$entity_info['entity keys']['label']});
    }
    // Build the option strings.
    $options[$relation->uuid] = t('!endpoints', array(
      '!endpoints' => implode(' —> ', $endpoints),
    ));
  }

  return $options;
}