You are here

function tmgmt_add_cart_form in Translation Management Tool 8

Adds add to cart form elements.

There are two use cases for this function:

1) Add the "Add to cart" submit action to the source overview form. In this case the $item_id should not be provided and only the action button will be displayed. The form is expected to submit job items ids as items[] which is being validated via tmgmt_source_add_to_cart_validate().

2) Add the "Add to cart" submit action to the translate tab. For this case the $item_id is required and with the add to cart button also the cart information is displayed. In this case there is no validation as the caller needs to provide valid $item_id value.

The "Add to cart" action submits the form by calling tmgmt_source_add_to_cart_submit() submit callback which processes either one job item or multiple.

Parameters

array $form: Form to which to add the add to cart form elements.

array $form_state: The current form state object.

string $plugin: Current plugin name.

string $item_type: Type of the source item.

mixed $item_id: (Optional) It is required in case a single source is being added into the cart.

Related topics

3 calls to tmgmt_add_cart_form()
ConfigTranslateForm::buildForm in sources/tmgmt_config/src/Form/ConfigTranslateForm.php
Form constructor.
ContentTranslateForm::buildForm in sources/content/src/Form/ContentTranslateForm.php
Form constructor.
SourceOverviewForm::buildForm in src/Form/SourceOverviewForm.php
Form constructor.

File

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

Code

function tmgmt_add_cart_form(&$form, FormStateInterface $form_state, $plugin, $item_type, $item_id = NULL) {
  $form_state
    ->set('tmgmt_cart', array(
    'plugin' => $plugin,
    'item_type' => $item_type,
    'item_id' => $item_id,
  ));
  $form['add_to_cart'] = array(
    '#type' => 'submit',
    '#value' => t('Add to cart'),
    '#submit' => array(
      'tmgmt_source_add_to_cart_submit',
    ),
    '#attributes' => array(
      'title' => t('Add marked items to the cart for later processing'),
    ),
  );
  if (empty($item_id)) {
    $form['add_to_cart']['#validate'] = array(
      'tmgmt_cart_source_overview_validate',
    );
  }
  else {

    //
    $form['add_to_cart']['#limit_validation_errors'] = array();

    // Compose the cart info message for the translate tab.
    $count = tmgmt_cart_get()
      ->count();
    if (tmgmt_cart_get()
      ->isSourceItemAdded($plugin, $item_type, $item_id)) {
      $form['add_to_cart']['#disabled'] = TRUE;
      $message = \Drupal::translation()
        ->formatPlural($count, 'There is @count item in the <a href="@url">translation cart</a> including the current item.', 'There are @count items in the <a href="@url">translation cart</a> including the current item.', array(
        '@url' => Url::fromRoute('tmgmt.cart')
          ->toString(),
      ));
    }
    else {
      $message = \Drupal::translation()
        ->formatPlural($count, 'There is @count item in the <a href="@url">translation cart</a>.', 'There are @count items in the <a href="@url">translation cart</a>.', array(
        '@url' => Url::fromRoute('tmgmt.cart')
          ->toString(),
      ));
    }
    $form['add_to_cart']['#suffix'] = '<span class="tmgmt-ui-cart-status-message">' . $message . '</span>';
  }
}