[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/features/includes/ -> features.taxonomy.inc (source)

   1  <?php
   2  // $Id: features.taxonomy.inc,v 1.1.2.7 2010/08/12 18:04:09 jmiccolis Exp $
   3  
   4  /**
   5   * Implementation of hook_features_api().
   6   */
   7  function taxonomy_features_api() {
   8    return array(
   9      'taxonomy' => array(
  10        'name' => t('Taxonomy'),
  11        'feature_source' => TRUE,
  12        'default_hook' => 'taxonomy_default_vocabularies',
  13        'default_file' => FEATURES_DEFAULTS_INCLUDED,
  14      ),
  15    );
  16  }
  17  
  18  /**
  19   * Implementation of hook_features_export_options().
  20   */
  21  function taxonomy_features_export_options() {
  22    $vocabularies = array();
  23    foreach (_taxonomy_features_get_vocabularies() as $machine_name => $vocabulary) {
  24      $vocabularies[$machine_name] = $vocabulary->name;
  25    }
  26    return $vocabularies;
  27  }
  28  
  29  /**
  30   * Implementation of hook_features_export().
  31   *
  32   * @todo Test adding existing dependencies.
  33   */
  34  function taxonomy_features_export($data, &$export, $module_name = '') {
  35    // taxonomy_default_vocabularies integration is provided by Features.
  36    $export['dependencies']['features'] = 'features';
  37    $export['dependencies']['taxonomy'] = 'taxonomy';
  38  
  39    // Add dependencies for each vocabulary.
  40    $map = features_get_default_map('taxonomy');
  41    foreach ($data as $machine_name) {
  42      if (isset($map[$machine_name]) && $map[$machine_name] != $module_name) {
  43        $export['dependencies'][$map[$machine_name]] = $map[$machine_name];
  44      }
  45      else {
  46        $export['features']['taxonomy'][$machine_name] = $machine_name;
  47      }
  48    }
  49    return array();
  50  }
  51  
  52  /**
  53   * Implementation of hook_features_export_render().
  54   */
  55  function taxonomy_features_export_render($module, $data) {
  56    $vocabularies = _taxonomy_features_get_vocabularies();
  57    $code = array();
  58    foreach ($data as $machine_name) {
  59      foreach ($vocabularies as $k => $v) {
  60        if ($k == $machine_name) {
  61          unset($v->vid);
  62          $code[$machine_name] = $v;
  63        }
  64      }
  65    }
  66    $code = "  return ". features_var_export($code, '  ') .";";
  67    return array('taxonomy_default_vocabularies' => $code);
  68  }
  69  
  70  /**
  71   * Implementation of hook_features_revert().
  72   */
  73  function taxonomy_features_revert($module) {
  74    taxonomy_features_rebuild($module);
  75  }
  76  
  77  /**
  78   * Implementation of hook_features_rebuild().
  79   *
  80   * Rebuilds Taxonomy vocabularies from code defaults.
  81   */
  82  function taxonomy_features_rebuild($module) {
  83    if ($vocabularies = features_get_default('taxonomy', $module)) {
  84      foreach ($vocabularies as $machine_name => $vocabulary) {
  85        if ($vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'features_' . $machine_name))) {
  86          $vocabulary['vid'] = $vid;
  87        }
  88        taxonomy_save_vocabulary($vocabulary);
  89      }
  90    }
  91  }
  92  
  93  /**
  94   * Retrieves all vocabularies viable for export.
  95   *
  96   * This function omits the vocabularies whose 'module' property doesn't begin
  97   * with 'features_' so that only vocabularies with non-serial identifiers are returned.
  98   */
  99  function _taxonomy_features_get_vocabularies() {
 100    $vocabularies = array();
 101    foreach (taxonomy_get_vocabularies() as $k => $vocabulary) {
 102      if (strpos($vocabulary->module, 'features_') === 0) {
 103        // Don't bother site builder with 'features_'.
 104        $vocabularies[substr($vocabulary->module, 9)] = $vocabulary;
 105      }
 106    }
 107    return $vocabularies;
 108  }
 109  
 110  /**
 111   * @see features_form_taxonomy_form_vocabulary_alter().
 112   */
 113  function _features_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
 114    $elem = array(
 115      '#type' => 'textfield',
 116      '#title' => t('Machine name'),
 117      '#description' => t('A system name for this vocabulary, may not be changed once it has been set.'),
 118    );
 119  
 120    if (!isset($form['module']) || $form['module']['#value'] == 'taxonomy') {
 121      // This vocabulary is new or wasn't assigned a machine name.
 122      $elem['#element_validate'] = array('features_taxonomy_machine_name_validate');
 123      unset($form['module']);
 124    }
 125    elseif (strpos($form['module']['#value'], 'features_') === 0) {
 126      // Simply display the existing machine name if we have one.
 127      $elem['#disabled'] = true;
 128      $elem['#value'] = substr($form['module']['#value'], 9);
 129    }
 130    else {
 131      // Some other module is responsible here, do nothing.
 132      return;
 133    }
 134  
 135    // Position the machine name right after the vocab name without interfering
 136    // with any explicit weights.
 137    $new = array();
 138    foreach ($form['identification'] as $k => $v) {
 139      $new[$k] = $v;
 140      if ($k == 'name') {
 141        $new['module'] = $elem;
 142      }
 143    } 
 144    $form['identification'] = $new;
 145  }
 146  
 147  // Validation handler for the machine name field.
 148  function features_taxonomy_machine_name_validate($element, &$form_state) {
 149    // Validate format - lowecase + underscore only...
 150    if (!empty($element['#value']) && !preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
 151      form_error($element, t('The machine name can only consist of lowercase letters, underscores, and numbers.'));
 152    }
 153  
 154    // If provided check machine name for conflicts and prepend with 'features'.
 155    if (!empty($element['#value'])) {
 156      $name = 'features_' . $element['#value'];
 157      $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", $name));
 158      if ($vid) {
 159        form_error($element, t('This machine name is already taken. Please enter a unique machine name or this taxonomy.'));
 160      }
 161      else {
 162        $form_state['values']['module'] = $name;
 163      }
 164    }
 165    // Otherwise, default to the default module name 'taxonomy'.
 166    else {
 167      $form_state['values']['module'] = 'taxonomy';
 168    }
 169  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7