You are here

function node_import_create_taxonomy in Node import 6

Create a new vocabulary or term by submitting the corresponding form.

1 string reference to 'node_import_create_taxonomy'
taxonomy_node_import_types in supported/taxonomy.inc
Implementation of hook_node_import_types().

File

supported/taxonomy.inc, line 40
Support file for the core taxonomy module.

Code

function node_import_create_taxonomy($type, $values, $preview) {
  $output = $preview ? '' : FALSE;
  if ($preview) {
    if ($type == 'vocabulary') {
      $node_types = array();
      foreach ($values['nodes'] as $node_type => $enabled) {
        if ($enabled) {
          $node_types[] = check_plain(node_get_types('name', $node_type));
        }
      }
      $output .= '<div class="preview">';
      $output .= '<h2>' . check_plain($values['name']) . '</h2>';
      $output .= '<div>' . filter_xss_admin($values['description']) . '</div>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Help text') . '</dt>';
      $output .= '<dd>' . check_plain($values['help']) . '</dd>';
      $output .= '<dt>' . t('Content types') . '</dt>';
      $output .= '<dd>' . implode('</dd><dd>', $node_types) . '</dd>';
      $output .= '<dt>' . t('Settings') . '</dt>';
      $settings = array();
      if ($values['tags']) {
        $settings[] = t('Tags');
      }
      if ($values['multiple']) {
        $settings[] = t('Multiple select');
      }
      if ($values['required']) {
        $settings[] = t('Required');
      }
      if ($values['hierarchy'] == 1) {
        $settings[] = t('Single hierarchy');
      }
      if ($values['hierarchy'] == 2) {
        $settings[] = t('Multiple hierarchy');
      }
      $output .= '<dd>' . implode('</dd><dd>', $settings) . '</dd>';
      $output .= '<dt>' . t('Weight') . '</dt>';
      $output .= '<dd>' . check_plain($values['weight']) . '</dd>';
      $output .= '</dl>';
      $output .= '</div>';
    }
    else {
      $output .= '<div class="preview">';
      $output .= '<h2>' . check_plain($values['name']) . '</h2>';
      $output .= '<div>' . filter_xss_admin($values['description']) . '</div>';
      if (!empty($values['parent'])) {
        $output .= '<p><strong>' . t('Parent:') . '</strong> ';
        $output .= node_import_display_taxonomy_term($values['parent']) . '</p>';
      }
      if (!empty($values['relations']) || !empty($values['synonyms'])) {
        $output .= '<dl>';
        if (!empty($values['relations'])) {
          $output .= '<dt>' . t('Related terms:') . '</dt> ';
          $output .= '<dd>' . implode('</dd><dd>', array_map('node_import_display_taxonomy_term', (array) $values['relations'])) . '</dd>';
        }
        if (!empty($values['synonyms'])) {
          $output .= '<dt>' . t('Synonyms:') . '</dt> ';
          $output .= '<dd>' . implode('</dd><dd>', array_map('check_plain', (array) $values['synonyms'])) . '</dd>';
        }
        $output .= '</dl>';
      }
      $output .= '</div>';

      // If a term is created and a later term refers to it (as relation
      // or parent), we get an error in preview. In order to avoid this
      // we manually add the (will-be-created) term to the checking
      // function.
      $types = node_import_types();
      $vocabulary = $types[$type]['vocabulary'];
      $field = array(
        'vocabulary' => $vocabulary,
      );
      node_import_check_taxonomy_term($values['name'], $field, array(), $preview, 'add');
    }
  }
  else {
    module_load_include('inc', 'taxonomy', 'taxonomy.admin');
    $values['op'] = t('Save');
    $form_state = array(
      'values' => $values,
    );
    if ($type == 'vocabulary') {
      node_import_drupal_execute('taxonomy_form_vocabulary', $form_state);
      $output = $form_state['vid'];
    }
    else {
      $types = node_import_types();
      $vocabulary = $types[$type]['vocabulary'];
      node_import_drupal_execute('taxonomy_form_term', $form_state, $vocabulary);
      $output = $form_state['tid'];

      // As a term can refer to other terms (parent, relation), we
      // can not continue if we have created a term.
      global $node_import_can_continue;
      $node_import_can_continue = FALSE;
    }
  }
  return $output;
}