diff --git a/user_relationships.admin.inc b/user_relationships.admin.inc index 0486ad7..d852fd1 100644 --- a/user_relationships.admin.inc +++ b/user_relationships.admin.inc @@ -173,6 +173,17 @@ function user_relationships_admin_type_edit($form, &$form_state, $relationship_t '#required' => TRUE, '#weight' => -10, ); + + $form['machine_name'] = array( + '#type' => 'machine_name', + '#default_value' => isset($relationship_type) ? $relationship_type->machine_name : NULL, + '#maxlength' => 255, + '#machine_name' => array( + 'exists' => 'user_relationships_type_machine_name_load', + ), + '#weight' => -9, + ); + $form['requires_approval'] = array( '#type' => 'checkbox', '#title' => t('Requires Approval'), diff --git a/user_relationships.features.inc b/user_relationships.features.inc new file mode 100644 index 0000000..451189a --- /dev/null +++ b/user_relationships.features.inc @@ -0,0 +1,97 @@ + array( + 'name' => t('User Relationships'), + 'default_hook' => 'user_relationships_default_relationships', + 'default_file' => FEATURES_DEFAULTS_INCLUDED, + 'feature_source' => TRUE, + 'file' => drupal_get_path('module', 'user_relationships') . '/user_relationships.features.inc', + ) + ); +} + +/** + * Implements hook_features_export_options(). + */ +function user_relationship_features_export_options() { + $options = array(); + $relationships = user_relationships_types_load(); + foreach ($relationships as $relationship) { + if ($relationship->machine_name) { + $options[$relationship->machine_name] = $relationship->name; + } + } + return $options; +} + +/** + * Implements hook_features_export(). + */ +function user_relationship_features_export($data, &$export, $module_name = '') { + $pipe = array(); + $map = features_get_default_map('user_relationship'); + foreach ($data as $relationship) { + // If another module provides this style, add it as a dependency + if (isset($map[$relationship]) && $map[$relationship] != $module_name) { + $module = $map[$relationship]; + $export['dependencies'][$module] = $module; + } + // Otherwise, export the style + elseif (user_relationships_type_machine_name_load($relationship)) { + $export['dependencies']['user_relationships'] = 'user_relationships'; + $export['features']['user_relationship'][$relationship] = $relationship; + } + } + return $pipe; +} + + +/** + * Implements hook_features_export_render(). + */ +function user_relationship_features_export_render($module_name, $data, $export = NULL) { + $code = array(); + $code[] = ' $relationships = array();'; + $code[] = ''; + foreach ($data as $name) { + if ($relationship = user_relationships_type_machine_name_load($name)) { + unset($relationship->rtid); + $relationship_export = features_var_export($relationship, ' '); + $relationship_identifier = features_var_export($name); + $code[] = " // Exported user_relationship style: {$name}."; + $code[] = " \$relationships[{$relationship_identifier}] = {$relationship_export};"; + $code[] = ""; + } + } + $code[] = ' return $relationships;'; + $code = implode("\n", $code); + return array('user_relationships_default_relationships' => $code); +} + + +/** + * Implements hook_features_revert(). + */ +function user_relationship_features_revert($module) { + user_relationship_features_rebuild($module); +} + +/** + * Implements of hook_features_rebuild(). + */ +function user_relationship_features_rebuild($module) { + if ($user_relationships = features_get_default('user_relationship', $module)) { + foreach ($user_relationships as $machine_name => $user_relationship) { + $user_relationship = (object) $user_relationship; + if ($existing_relationship = user_relationships_type_machine_name_load($machine_name)) { + $user_relationship->rtid = $existing_relationship->rtid; + } + user_relationships_type_save($user_relationship); + } + } +} diff --git a/user_relationships.install b/user_relationships.install index 952bd84..12069e1 100644 --- a/user_relationships.install +++ b/user_relationships.install @@ -31,6 +31,7 @@ function user_relationships_schema() { $schema['user_relationship_types'] = array( 'fields' => array( 'rtid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), + 'machine_name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), 'name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), 'plural_name' => array('type' => 'varchar', 'length' => 255, 'default' => ''), 'is_oneway' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'), @@ -69,6 +70,7 @@ function user_relationships_schema() { ), ), 'unique keys' => array( + 'machine_name' => array('machine_name'), 'name' => array('name'), ), 'primary key' => array('rtid') @@ -200,3 +202,19 @@ function user_relationships_update_7005() { db_add_field('user_relationship_types', $field, $definition); } } + +/** + * Add machine name field. + */ +function user_relationships_update_7103() { + $field = array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ); + db_add_field('user_relationship_types', 'machine_name', $field); + + db_update('user_relationship_types') + ->expression('machine_name', 'name') + ->execute(); +} diff --git a/user_relationships.module b/user_relationships.module index 87fed87..e2be32c 100644 --- a/user_relationships.module +++ b/user_relationships.module @@ -423,6 +423,19 @@ function user_relationships_type_load($param = array(), $reset = NULL) { } } +/** + * Public API for retrieving a specific relationship by machine name. + * + * @param $machine_name + * The machine name identifying the relationship. + * @return + * object of the requested relationship type + * + */ +function user_relationships_type_machine_name_load($machine_name) { + return user_relationships_type_load(array('machine_name' => $machine_name)); +} + /** * Load all relationship types.