You are here

function favorites_add_favorite_form in Favorites 6

Same name and namespace in other branches
  1. 7.2 favorites.module \favorites_add_favorite_form()
  2. 7 favorites.module \favorites_add_favorite_form()

Form callback for the "add favorite form"

See also

favorites_user_block()

1 string reference to 'favorites_add_favorite_form'
favorites_block in ./favorites.module
Implements hook_block().

File

./favorites.module, line 187
The favorites module allows users to bookmark any path within a site.

Code

function favorites_add_favorite_form() {
  global $user;

  // Try to get a default value for the title field.
  // drupal_get_title() has run through check_plain. This is useless for us
  // and needs to fixed, which only works easily with PHP >= 5.1.
  if (function_exists('version_compare') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
    $title = htmlspecialchars_decode(drupal_get_title());
  }
  if (!isset($title)) {
    $title = menu_get_active_title();
  }
  if ($title == '') {
    $title = variable_get('site_name', 'Home');
  }
  $title = strip_tags($title);
  $path = strip_tags($_GET['q']);
  $query = drupal_query_string_encode($_GET, array(
    'q',
  ));

  // Add a collapsible container.
  $form = array(
    'add' => array(
      '#type' => 'fieldset',
      '#collapsible' => true,
      '#collapsed' => true,
      '#title' => t('add this page'),
      'title' => array(
        '#type' => 'textfield',
        '#size' => 20,
        '#maxlength' => 255,
        '#default_value' => $title,
        '#attributes' => array(
          'style' => 'width: 90%',
          'class' => 'favorites-add-textfield',
        ),
      ),
      'submit' => array(
        '#type' => 'submit',
        '#value' => t("Add"),
        '#submit' => array(
          'favorites_add_favorite_form_submit',
        ),
      ),
      'path' => array(
        '#type' => 'value',
        '#value' => $path,
      ),
      'query' => array(
        '#type' => 'value',
        '#value' => $query,
      ),
    ),
  );

  //Preserve the current path with query string.
  $destination = drupal_get_destination();
  $form['#redirect'] = array(
    $_GET['q'],
    drupal_query_string_encode($_GET, array(
      'q',
    )),
  );

  // Additionally add path and query to the JS settings.
  drupal_add_js(array(
    'favorites' => array(
      'path' => $path,
      'query' => $query,
      'addCallbackPath' => url('favorites/js/add'),
    ),
  ), 'setting');
  return $form;
}