function tmgmt_job_checkout_and_redirect

Attempts to checkout a number of jobs and prepare the necessary redirects.

Parameters

array $form_state: Form state array, used to set the initial redirect.

array $jobs: Array of jobs to attempt checkout

See also

tmgmt_job_checkout_multiple()

Related topics

File

./tmgmt.module, line 1119
Main module file for the Translation Management module.

Code

function tmgmt_job_checkout_and_redirect(FormStateInterface $form_state, array $jobs) {
  $redirects = tmgmt_job_checkout_multiple($jobs);

  // If necessary, do a redirect.
  if ($redirects) {
    $request = \Drupal::request();
    if ($request->query
      ->has('destination')) {

      // Remove existing destination, as that will prevent us from being
      // redirect to the job checkout page. Set the destination as the final
      // redirect instead.
      tmgmt_redirect_queue_set($redirects, $request->query
        ->get('destination'));
      $request->query
        ->remove('destination');
    }
    else {
      tmgmt_redirect_queue_set($redirects, Url::fromRoute('<current>')
        ->getInternalPath());
    }
    $form_state
      ->setRedirectUrl(Url::fromUri('base:' . tmgmt_redirect_queue_dequeue()));

    // Count of the job messages is one less due to the final redirect.
    \Drupal::messenger()
      ->addStatus(\Drupal::translation()
      ->formatPlural(count($redirects), t('One job needs to be checked out.'), t('@count jobs need to be checked out.')));
  }
}