name == $term_name) { return $term->tid; } } return FALSE; } /** * Helper function: Create a taxonomy term and return the tid. * * @param $term_name Term name * @param $vocabulary_id ID of the vocabulary to search the term in * * @return Term id of the created term or FALSE */ function _create_taxonomy_term($term_name, $vocabulary_id) { $term = new stdClass(); $term->name = $term_name; $term->vid = $vocabulary_id;//$vocabulary->vid; taxonomy_term_save($term); return $term->tid; } /** * Helper function: Get a taxonomy term and return the tid, * Creates the taxonomy term if it does not exit yet * * @param $term_name Term name * @param $vocabulary_id ID of the vocabulary to search the term in * * @return Term id of the found or created term or FALSE */ function _create_get_taxonomy_term($term_name, $vocabulary_id) { if (empty($vocabulary_id )) return FALSE; $tid = _get_taxonomy_term($term_name,$vocabulary_id); if (empty($tid)) $tid = _create_taxonomy_term($term_name,$vocabulary_id); return $tid; } /** * Assigns the files uppercased folder name as taxonomy term * */ function file_taxonomy_file_presave($file) { $fieldname = variable_get('filetaxonomy_field',''); $field_info = empty($fieldname)?null:field_info_field($fieldname); $vocabulary_name = empty($field_info)?null:$field_info['settings']['allowed_values'][0]['vocabulary']; $vocabulary = empty($vocabulary_name)?null:taxonomy_vocabulary_machine_name_load($vocabulary_name); $vocabulary_id = empty($vocabulary)?null:$vocabulary->vid; if (!empty($vocabulary_id) && (empty($file->{$fieldname}))) { $path = drupal_ucfirst(parse_url($file->destination, PHP_URL_HOST)); $tid = _create_get_taxonomy_term($path,$vocabulary_id); if (!empty($tid)) { $file->{$fieldname} = array(); $file->{$fieldname}['und'][0]['tid'] = $tid; } } } /** * Reindex all files without taxonomy term * * */ function file_taxonomy_reindex() { /* TODO */ } /** * Module Settings * */ function file_taxonomy_admin() { $form = array(); $fields = field_info_field_map(); $fields_options = array(); foreach ($fields as $key => $item) { if (($item['type'] == 'taxonomy_term_reference') && (!empty($item['bundles']['file']))) { $fields_options[$key] = $key; } } $form['filetaxonomy_field'] = array( '#type' => 'select', '#title' => t('Field'), '#options' => $fields_options , '#default_value' => variable_get('filetaxonomy_field',''), '#description' => t('Select the field to which taxonomy terms should be added.'), '#required' => TRUE, ); $form['reindex'] = array( '#type' => 'submit', '#value' => 'Reindex all files', '#submit' => array('file_taxonomy_reindex') ); return system_settings_form($form); } function file_taxonomy_menu() { $items = array(); $items['admin/config/filetaxonomy'] = array( 'title' => 'File Taxonomy Module Settings', 'description' => 'File Taxonomy Module Settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('file_taxonomy_admin'), 'access arguments' => array('administer filetaxonomy settings'), 'type' => MENU_NORMAL_ITEM, ); return $items; }