You are here

public function QueueExampleForm::buildForm in Examples for Developers 3.x

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/queue_example/src/Form/QueueExampleForm.php, line 89

Class

QueueExampleForm
Form with examples on how to use queue.

Namespace

Drupal\queue_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Simple counter that makes it possible to put auto-incrementing default
  // string into the string to insert.
  if (empty($form_state
    ->get('insert_counter'))) {
    $form_state
      ->set('insert_counter', 1);
  }
  $queue_name = $form_state
    ->getValue('queue_name') ?: 'queue_example_first_queue';
  $items = $this
    ->retrieveQueue($queue_name);
  $form['help'] = [
    '#type' => 'markup',
    '#markup' => '<div>' . $this
      ->t('This page is an interface on the Drupal 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['wrong_queue_warning'] = [
    '#type' => 'markup',
    '#markup' => '<div>' . $this
      ->t('Note: the example works only with the default queue implementation, which is not currently configured!!') . '</div>',
    '#access' => !$this
      ->doesQueueUseDB(),
  ];
  $queue_names = [
    'queue_example_first_queue',
    'queue_example_second_queue',
  ];
  $form['queue_name'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Choose queue to examine'),
    '#options' => array_combine($queue_names, $queue_names),
    '#default_value' => $queue_name,
  ];
  $form['queue_show'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Show queue'),
    '#submit' => [
      '::submitShowQueue',
    ],
  ];
  $form['status_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Queue status for @name', [
      '@name' => $queue_name,
    ]),
    '#collapsible' => TRUE,
  ];
  if (count($items) > 0) {
    $form['status_fieldset']['status'] = [
      '#theme' => 'table',
      '#header' => [
        $this
          ->t('Item ID'),
        $this
          ->t('Claimed/Expiration'),
        $this
          ->t('Created'),
        $this
          ->t('Content/Data'),
      ],
      '#rows' => array_map([
        $this,
        'processQueueItemForTable',
      ], $items),
    ];
  }
  else {
    $form['status_fieldset']['status'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('There are no items in the queue.'),
    ];
  }
  $form['insert_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Insert into @name', [
      '@name' => $queue_name,
    ]),
  ];
  $form['insert_fieldset']['string_to_add'] = [
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $this
      ->t('item @counter', [
      '@counter' => $form_state
        ->get('insert_counter'),
    ]),
  ];
  $form['insert_fieldset']['add_item'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Insert into queue'),
    '#submit' => [
      '::submitAddQueueItem',
    ],
  ];
  $form['claim_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Claim from queue'),
    '#collapsible' => TRUE,
  ];
  $form['claim_fieldset']['claim_time'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Claim time, in seconds'),
    '#options' => [
      0 => $this
        ->t('none'),
      5 => $this
        ->t('5 seconds'),
      60 => $this
        ->t('60 seconds'),
    ],
    '#description' => $this
      ->t('This time is only valid if cron runs during this time period. You can run cron manually below.'),
    '#default_value' => $form_state
      ->getValue('claim_time') ?: 5,
  ];
  $form['claim_fieldset']['claim_item'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Claim the next item from the queue'),
    '#submit' => [
      '::submitClaimItem',
    ],
  ];
  $form['claim_fieldset']['claim_and_delete_item'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Claim the next item and delete it'),
    '#submit' => [
      '::submitClaimDeleteItem',
    ],
  ];
  $form['claim_fieldset']['run_cron'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Run cron manually to expire claims'),
    '#submit' => [
      '::submitRunCron',
    ],
  ];
  $form['delete_queue'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Delete the queue and items in it'),
    '#submit' => [
      '::submitDeleteQueue',
    ],
  ];
  return $form;
}