You are here

function shorten_form_shorten in Shorten URLs 7

Same name and namespace in other branches
  1. 6 shorten.module \shorten_form_shorten()
  2. 7.2 shorten.module \shorten_form_shorten()

Builds a form which allows shortening of a URL via the UI.

2 string references to 'shorten_form_shorten'
shorten_form_shorten_form in ./shorten.module
Returns HTML representing the shorten form.
shorten_menu in ./shorten.module
Implements hook_menu().

File

./shorten.module, line 475
Shortens URLs via external services.

Code

function shorten_form_shorten($form, &$form_state) {
  drupal_add_js(drupal_get_path('module', 'shorten') . '/shorten.js');

  //Form elements between ['opendiv'] and ['closediv'] will be refreshed via AHAH on form submission.
  $form['opendiv'] = array(
    '#markup' => '<div id="shorten_replace">',
  );
  if (!isset($form_state['storage'])) {
    $form_state['storage'] = array(
      'step' => 0,
    );
  }
  if (isset($form_state['storage']['short_url'])) {

    // This whole "step" business keeps the form element from being cached.
    $form['shortened_url_' . $form_state['storage']['step']] = array(
      '#type' => 'textfield',
      '#title' => t('Shortened URL'),
      '#default_value' => $form_state['storage']['short_url'],
      '#size' => 25,
      '#attributes' => array(
        'class' => array(
          'shorten-shortened-url',
        ),
      ),
    );
  }
  $form['url_' . $form_state['storage']['step']] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#default_value' => '',
    '#required' => TRUE,
    '#size' => 25,
    '#maxlength' => 2048,
    '#attributes' => array(
      'class' => array(
        'shorten-long-url',
      ),
    ),
  );

  //Form elements between ['opendiv'] and ['closediv'] will be refreshed via AHAH on form submission.
  $form['closediv'] = array(
    '#markup' => '</div>',
  );
  $last_service = NULL;
  if (isset($form_state['storage']['service'])) {
    $last_service = $form_state['storage']['service'];
  }
  $service = _shorten_service_form($last_service);
  if (is_array($service)) {
    $form['service'] = $service;
  }
  $form['shorten'] = array(
    '#type' => 'submit',
    '#value' => t('Shorten'),
    '#ajax' => array(
      'callback' => 'shorten_save_js',
      'wrapper' => 'shorten_replace',
      'effect' => 'fade',
      'method' => 'replace',
    ),
  );
  return $form;
}