You are here

function wysiwyg_template_template_form in Wysiwyg API template plugin 7.2

Wysiwyg template create/modify form

1 string reference to 'wysiwyg_template_template_form'
wysiwyg_template_menu in ./wysiwyg_template.module
Implementation of hook_menu().

File

./wysiwyg_template.admin.inc, line 114
Administrative page callbacks for the Wysiwyg Template module.

Code

function wysiwyg_template_template_form($form, $form_state, $template = NULL) {
  if (isset($form_state['confirm_delete'])) {

    // Rebuild the form to confirm term deletion.
    $form['name'] = array(
      '#type' => 'value',
      '#value' => $form_state['values']['name'],
    );
    $form['delete'] = array(
      '#type' => 'value',
      '#value' => true,
    );
    return confirm_form($form, t('Are you sure you want to delete the template %title?', array(
      '%title' => $form_state['values']['title'],
    )), 'admin/content/wysiwyg-template', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
  }
  if (!empty($template)) {

    // Add the current values as defaults to the form, if editing an existing item.
    $form_state['values'] = $template;
  }
  $content_types = array();
  foreach (node_type_get_types() as $content_type) {
    $content_types[$content_type->type] = $content_type->name;
  }
  $form = array();
  $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Template Name'),
    '#default_value' => isset($form_state['values']['title']) ? $form_state['values']['title'] : '',
    '#description' => t('Select a name for this template.'),
    '#maxlength' => 80,
    '#required' => true,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'wysiwyg_template_name_exists',
      'source' => array(
        'title',
      ),
    ),
    '#description' => t('A unique machine-readable name for this template. It must only contain lowercase letters, numbers, and underscores.'),
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Template Description'),
    '#default_value' => isset($form_state['values']['description']) ? $form_state['values']['description'] : '',
    '#description' => t('A description to be shown with the template.'),
  );
  $form['weight'] = array(
    '#type' => 'textfield',
    '#title' => t('Weight'),
    '#default_value' => isset($form_state['values']['weight']) ? $form_state['values']['weight'] : 0,
    '#description' => t('The weight of this template for the sort order in lists.'),
  );
  $form['content_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Available for content types'),
    '#options' => $content_types,
    '#default_value' => isset($form_state['values']['content_types']) ? array_keys($form_state['values']['content_types']) : array(),
    '#description' => t('If you select no content type, this template will be available for all content types.'),
    '#access' => count($content_types) > 1,
  );

  // load image if it has one
  $image = '';
  if (isset($form_state['values']['fid']) && $form_state['values']['fid']) {
    $image_uri = file_load($form_state['values']['fid'])->uri;
    if ($image_uri) {
      $image = theme('image_style', array(
        'style_name' => 'wysiwyg_template_thumbnail',
        'path' => $image_uri,
      ));
    }
  }
  $form['template_image'] = array(
    '#type' => 'file',
    '#title' => t('Choose a file'),
    '#size' => 22,
    '#description' => t('A image to be shown with the template.'),
    '#prefix' => $image,
  );
  $form['template_image_fid'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($form_state['values']['fid']) ? $form_state['values']['fid'] : 0,
  );

  // add delete button if it has an image
  if (isset($form_state['values']['fid']) && $form_state['values']['fid']) {
    $form['template_image_delete'] = array(
      '#type' => 'checkbox',
      '#title' => t('Delete the Template image.'),
    );
  }
  $form['body'] = array(
    '#type' => 'text_format',
    '#title' => t('HTML Template'),
    '#rows' => 10,
    '#format' => isset($form_state['values']['format']) ? $form_state['values']['format'] : filter_default_format(),
    '#default_value' => isset($form_state['values']['body']) ? $form_state['values']['body'] : '',
    '#required' => true,
  );
  if (module_exists('token')) {
    $form['token_help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'node',
        'custom',
      ),
      '#global_types' => true,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  if (!empty($template)) {
    if (!empty($form_state['values']['name'])) {
      $form['name']['#default_value'] = $form_state['values']['name'];
      $form['name']['#disabled'] = TRUE;
      $form['name']['#value'] = $form_state['values']['name'];
    }

    // If it's an existing template, offer a delete button.
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  return $form;
}