function node_convert_add_template in Node Convert 7
Same name and namespace in other branches
- 6 node_convert.module \node_convert_add_template()
Add template page callback.
1 string reference to 'node_convert_add_template'
- node_convert_menu in ./
node_convert.module - Implements hook_menu().
File
- ./
node_convert.admin.inc, line 119 - Administration page callbacks for the node_convert module.
Code
function node_convert_add_template($form, &$form_state, $template = NULL) {
$form = array();
// @TODO Figure out how to let a user edit templates that are stored in code via features.
$is_editing_mode = FALSE;
if (!empty($template)) {
$is_editing_mode = TRUE;
$form_state['storage']['is_editing_mode'] = $is_editing_mode;
}
/* 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,
);
if ($op == 'choose_destination_type') {
// Get available content types
$to_types = node_convert_return_access_node_types('to');
$from_types = node_convert_return_access_node_types('from');
if ($to_types != FALSE && $from_types != FALSE) {
$form['template_name'] = array(
'#type' => 'textfield',
'#title' => t("Template name"),
'#required' => TRUE,
);
$form['machine_name'] = array(
'#type' => 'machine_name',
'#title' => t('Machine name'),
'#machine_name' => array(
'exists' => 'node_convert_machine_name_check',
'source' => array(
'template_name',
),
),
'#required' => TRUE,
);
$form['source_type'] = array(
'#type' => 'select',
'#title' => t("Source type"),
'#options' => $from_types,
);
$form['dest_type'] = array(
'#type' => 'select',
'#title' => t("Destination type"),
'#options' => $to_types,
);
$form['create_action'] = array(
'#type' => 'checkbox',
'#title' => t("Create action?"),
'#description' => t("If the option is checked, an action named Convert *Template name* will be created."),
);
if ($is_editing_mode) {
$form['template_name']['#default_value'] = $template['name'];
$form['machine_name']['#default_value'] = $template['machine_name'];
$form['machine_name']['#disabled'] = TRUE;
$form['source_type']['#default_value'] = $template['source_type'];
$form['dest_type']['#default_value'] = $template['destination_type'];
// @TODO Fix action when editing.
$form['create_action']['#access'] = FALSE;
}
}
else {
$form['no_types'] = array(
'#type' => 'markup',
'#value' => t("You don't have access to any node types."),
);
}
}
elseif ($op == 'choose_destination_fields') {
// Get the fields of the source type
$source_fields = field_info_instances('node', $form_state['storage']['source_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 the destination type fields
$dest_fields = field_info_instances('node', $form_state['storage']['dest_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_field) {
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_field['field_name']] = $dest_field['field_name'];
}
}
// Remember the source fields to be converted
$form['source_field_' . $i] = array(
'#type' => 'value',
'#value' => $source_field['field_name'],
);
// The select populated with possible destination fields for each source field
$form['dest_field_' . $i] = array(
'#type' => 'select',
'#options' => $options,
'#title' => (isset($source_field['label']) ? $source_field['label'] . ' (' . $source_field['field_name'] . ')' : $source_field['field_name']) . ' ' . t("should be inserted into"),
);
if ($is_editing_mode) {
// Populate the previous fields, only if the selected node types haven't changed from the original ones.
$source_type = $form_state['values']['source_type'];
$destination_type = $form_state['values']['dest_type'];
if ($source_type == $template['source_type'] && $destination_type == $template['destination_type']) {
$form['dest_field_' . $i]['#default_value'] = $template['data']['fields']['destination'][$i - 1];
}
}
}
$form['number_of_fields'] = array(
'#type' => 'value',
'#value' => $i,
);
}
$form['no_fields'] = array(
'#type' => 'value',
'#value' => $no_fields,
);
// All node specific form options needed for types like book, forum, etc. are done here
$hook_options = node_convert_invoke_all('node_convert_change', array(
'dest_node_type' => $form_state['storage']['dest_type'],
), 'options');
if (!empty($hook_options)) {
$form['hook_options'] = $hook_options;
array_unshift($form['hook_options'], array(
'#value' => '<strong>' . t("Also the following parameters are available:") . '</strong>',
));
$form['hook_options']['#tree'] = TRUE;
}
}
if ($op == 'choose_destination_type' && $to_types != FALSE && $from_types != FALSE) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Next"),
);
}
elseif ($op == "choose_destination_fields") {
$submit_label = $is_editing_mode ? t('Update') : t('Create');
$form['submit'] = array(
'#type' => 'submit',
'#value' => $submit_label,
'#weight' => 100,
);
}
return $form;
}