1 queue_example.module queue_example_add_remove_form($form, &$form_state)

Form generator for managing the queue.

Provides an interface to add items to the queue, to retrieve (claim) an item from the head of the queue, and to claim and delete. Also allows the user to run cron manually, so that claimed items can be released.

Related topics

File

modules/examples/queue_example/queue_example.module, line 57
Hook implementations for the Queue Example module.

Code

function queue_example_add_remove_form($form, &$form_state) {
  // Simple counter that makes it possible to put auto-incrementing default
  // string into the string to insert.
  if (empty($form_state['storage']['insert_counter'])) {
    $form_state['storage']['insert_counter'] = 1;
  }

  $queue_name = !empty($form_state['values']['queue_name']) ? $form_state['values']['queue_name'] : 'queue_example_first_queue';
  $items = queue_example_retrieve_queue($queue_name);

  // Add CSS to make the form a bit denser.
  $form['#attached']['css'] = array(backdrop_get_path('module', 'queue_example') . '/queue_example.css');

  $form['help'] = array(
    '#type' => 'markup',
    '#markup' => '<div>' . t('This page is an interface on the Backdrop queue API. You can add new items to the queue, "claim" one (retrieve the next item and keep a lock on it), and delete one (remove it from the queue). Note that claims are not expired until cron runs, so there is a special button to run cron to perform any necessary expirations.') . '</div>',
  );

  $form['queue_name'] = array(
    '#type' => 'select',
    '#title' => t('Choose queue to examine'),
    '#options' => backdrop_map_assoc(array('queue_example_first_queue', 'queue_example_second_queue')),
    '#default_value' => $queue_name,
  );
  $form['queue_show'] = array(
    '#type' => 'submit',
    '#value' => t('Show queue'),
    '#submit' => array('queue_example_show_queue'),
  );
  $form['status_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Queue status for @name', array('@name' => $queue_name)),
    '#collapsible' => TRUE,
  );
  $form['status_fieldset']['status'] = array(
    '#type' => 'markup',
    '#markup' => theme('queue_items', array('items' => $items)),
  );
  $form['insert_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Insert into @name', array('@name' => $queue_name)),
  );
  $form['insert_fieldset']['string_to_add'] = array(
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => t('item @counter', array('@counter' => $form_state['storage']['insert_counter'])),
  );
  $form['insert_fieldset']['add_item'] = array(
    '#type' => 'submit',
    '#value' => t('Insert into queue'),
    '#submit' => array('queue_example_add_remove_form_insert'),
  );
  $form['claim_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Claim from queue'),
    '#collapsible' => TRUE,
  );

  $form['claim_fieldset']['claim_time'] = array(
    '#type' => 'radios',
    '#title' => t('Claim time, in seconds'),
    '#options' => array(
      0 => t('none'),
      5 => t('5 seconds'),
      60 => t('60 seconds'),
    ),
    '#description' => t('This time is only valid if cron runs during this time period. You can run cron manually below.'),
    '#default_value' => !empty($form_state['values']['claim_time']) ? $form_state['values']['claim_time'] : 5,
  );
  $form['claim_fieldset']['claim_item'] = array(
    '#type' => 'submit',
    '#value' => t('Claim the next item from the queue'),
    '#submit' => array('queue_example_add_remove_form_claim'),
  );
  $form['claim_fieldset']['claim_and_delete_item'] = array(
    '#type' => 'submit',
    '#value' => t('Claim the next item and delete it'),
    '#submit' => array('queue_example_add_remove_form_delete'),
  );
  $form['claim_fieldset']['run_cron'] = array(
    '#type' => 'submit',
    '#value' => t('Run cron manually to expire claims'),
    '#submit' => array('queue_example_add_remove_form_run_cron'),
  );
  $form['delete_queue'] = array(
    '#type' => 'submit',
    '#value' => t('Delete the queue and items in it'),
    '#submit' => array('queue_example_add_remove_form_clear_queue'),
  );
  return $form;
}