You are here

function node_convert_conversion_form in Node Convert 7

Same name and namespace in other branches
  1. 5 node_convert.module \node_convert_conversion_form()
  2. 6 node_convert.module \node_convert_conversion_form()

Node conversion form.

1 string reference to 'node_convert_conversion_form'
node_convert_menu in ./node_convert.module
Implements hook_menu().

File

./node_convert.forms.inc, line 11
Form page callbacks for the node_convert module.

Code

function node_convert_conversion_form($form, $form_state, $node) {
  $form = array();

  /* Setting the steps */
  if (!isset($form_state['values']['step'])) {
    $op = 'choose_destination_type';
  }
  elseif ($form_state['values']['step'] == 'choose_destination_type') {
    $op = 'choose_destination_fields';
  }
  $form['step'] = array(
    '#type' => 'value',
    '#value' => $op,
  );
  $form['node'] = array(
    '#type' => 'value',
    '#value' => $node,
  );

  /* Form steps */
  if ($op == 'choose_destination_type') {
    $type = node_type_get_name($node);

    // Remember current node type, used in theme_ function
    $form['current_type'] = array(
      '#markup' => $type,
    );

    // Get available content types
    $types = node_convert_return_access_node_types('to');
    if ($types != FALSE) {
      $key = array_search($form['current_type']['#markup'], $types);

      // Delete the current content type from the list
      if ($key !== FALSE) {
        unset($types[$key]);
      }
      $options = $types;

      // Populate the select with possible content types
      $form['destination_type'] = array(
        '#type' => 'select',
        '#options' => $options,
        '#title' => t("To what content type should this node be converted"),
      );
    }
    else {

      // Just used as a message, not sure if it's the best implementation
      $form['destination_type'] = array(
        '#markup' => t("You don't have access to any node types."),
      );
    }
  }
  elseif ($op == 'choose_destination_fields') {

    // $source_fields = content_types($node->type);
    // Get source type fields
    $source_fields = field_info_instances('node', $node->type);
    $fields_info = field_info_fields();

    // In case there are no fields, just convert the node type
    if (count($source_fields) == 0) {
      $no_fields = TRUE;
    }
    else {
      $no_fields = FALSE;

      // Get destination type fields
      $dest_fields = field_info_instances('node', $form_state['storage']['destination_type']);
      $i = 0;
      foreach ($source_fields as $source_field_name => $source_field) {
        ++$i;
        $options = array();
        $options['discard'] = 'discard';
        $options[APPEND_TO_BODY] = t('Append to body');
        $options[REPLACE_BODY] = t('Replace body');

        // Insert destination type fields into $options that are of the same type as the source.
        foreach ($dest_fields as $dest_field_name => $dest_value) {
          if ($fields_info[$source_field_name]['type'] == $fields_info[$dest_field_name]['type'] || $fields_info[$source_field_name]['type'] == 'text_with_summary' && $fields_info[$dest_field_name]['type'] == 'text_long' || $fields_info[$source_field_name]['type'] == 'text_long' && $fields_info[$dest_field_name]['type'] == 'text_with_summary') {
            $options[$dest_value['field_name']] = $dest_value['field_name'];
          }
        }

        // Remember the source fields to be converted
        $form['source_field_' . $i] = array(
          '#type' => 'value',
          '#value' => $source_field['field_name'],
        );
        $form['container_' . $i] = array(
          '#type' => 'container',
          '#suffix' => '<br />',
        );

        // The select populated with possible destination fields for each source field
        // If the destination node type has the same field as the source node type, the default value is set to it.
        $form['container_' . $i]['dest_field_' . $i] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => $source_field['field_name'],
          '#title' => check_plain($source_field['field_name']) . " " . t("should be inserted into"),
        );

        // Print the current value of the source field
        $temp_value = node_convert_format_field_value($node, $fields_info[$source_field_name]);
        $form['container_' . $i]['current_field_value_' . $i] = array(
          '#type' => 'item',
          '#markup' => '<div>' . t("Current value is:") . " <b>" . $temp_value . '</b></div>',
        );
      }
      $form['number_of_fields'] = array(
        '#type' => 'value',
        '#value' => $i,
      );
    }
    $form['no_fields'] = array(
      '#type' => 'value',
      '#value' => $no_fields,
    );
    $hook_options = node_convert_invoke_all('node_convert_change', array(
      'dest_node_type' => $form_state['storage']['destination_type'],
    ), 'options');
    if (!empty($hook_options)) {
      $form['hook_options'] = $hook_options;
      array_unshift($form['hook_options'], array(
        '#value' => '<br /><strong>' . t("Also the following parameters are available:") . '</strong>',
      ));
      $form['hook_options']['#tree'] = TRUE;
    }
  }
  if ($op != 'choose_destination_fields' && isset($types) && $types != FALSE) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t("Next"),
    );
  }
  elseif ($op == 'choose_destination_fields') {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t("Convert"),
      '#weight' => 100,
    );
  }
  return $form;
}