You are here

function tmgmt_source_add_to_cart_submit in Translation Management Tool 8

Submit handler to add items into the cart.

Based on the submitted data it will create job items and add them into the cart. Use it in combination with tmgmt_add_cart_form() as that function sets all the necessary values needed to crate a job an add it into the cart.

See also

tmgmt_add_cart_form()

Related topics

1 string reference to 'tmgmt_source_add_to_cart_submit'
tmgmt_add_cart_form in ./tmgmt.module
Adds add to cart form elements.

File

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

Code

function tmgmt_source_add_to_cart_submit(array $form, FormStateInterface $form_state) {
  $cart_info = $form_state
    ->get('tmgmt_cart');
  if (!empty($cart_info['plugin']) && !empty($cart_info['item_type']) && $form_state
    ->getValue('items')) {
    $source_items = array_filter($form_state
      ->getValue('items'));
    $item_type = $cart_info['item_type'];
    $plugin = $cart_info['plugin'];
  }
  elseif (!empty($cart_info['plugin']) && !empty($cart_info['item_type']) && !empty($cart_info['item_id'])) {
    $source_items = array(
      $cart_info['item_id'],
    );
    $item_type = $cart_info['item_type'];
    $plugin = $cart_info['plugin'];
  }
  else {
    \Drupal::messenger()
      ->addError(t('Unable to add the content into the cart.'));
    return;
  }
  $i = 0;
  foreach ($source_items as $source_id) {
    if (tmgmt_cart_get()
      ->addJobItem($plugin, $item_type, $source_id)) {
      $i++;
    }
  }
  \Drupal::messenger()
    ->addStatus(\Drupal::translation()
    ->formatPlural($i, '@count content source was added into the <a href="@url">cart</a>.', '@count content sources were added into the <a href="@url">cart</a>.', array(
    '@url' => Url::fromRoute('tmgmt.cart')
      ->toString(),
  )));
}