You are here

protected function ProductAttributeTranslationFormTrait::buildValuesForm in Commerce Core 8.2

Builds the translation form for product attribute values.

Parameters

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

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

\Drupal\commerce_product\Entity\ProductAttributeInterface $attribute: The product attribute.

Return value

array The translation form.

2 calls to ProductAttributeTranslationFormTrait::buildValuesForm()
ProductAttributeTranslationAddForm::buildForm in modules/product/src/Form/ProductAttributeTranslationAddForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().
ProductAttributeTranslationEditForm::buildForm in modules/product/src/Form/ProductAttributeTranslationEditForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().

File

modules/product/src/Form/ProductAttributeTranslationFormTrait.php, line 42

Class

ProductAttributeTranslationFormTrait
Provides common functionality for the product attribute translation forms.

Namespace

Drupal\commerce_product\Form

Code

protected function buildValuesForm(array $form, FormStateInterface $form_state, ProductAttributeInterface $attribute) {
  $values = $attribute
    ->getValues();
  $has_translatable_values = FALSE;
  foreach ($values as $value) {
    if ($value
      ->isTranslatable()) {
      $has_translatable_values = TRUE;
      break;
    }
  }

  // Don't display the values if there's nothing to translate.
  if (!$has_translatable_values) {
    return $form;
  }
  $language = $form_state
    ->get('config_translation_language');
  $source_language = $form_state
    ->get('config_translation_source_language');

  // Set the keys expected by the inline form.
  $form_state
    ->set('langcode', $language
    ->getId());
  $form_state
    ->set('entity_default_langcode', $source_language
    ->getId());
  $form['values'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Value'),
      $this
        ->t('Value'),
    ],
    // #input defaults to TRUE, which breaks file fields on the value form.
    // This table is used for visual grouping only, the element itself
    // doesn't have any values of its own that need processing.
    '#input' => FALSE,
  ];
  foreach ($values as $index => $value) {
    $inline_form = $this->inlineFormManager
      ->createInstance('content_entity', [], $value);
    $original_value = $value;
    if ($value
      ->hasTranslation($source_language
      ->getId())) {
      $original_value = $value
        ->getTranslation($source_language
        ->getId());
    }
    $value_form =& $form['values'][$index];
    $value_form['source'] = [
      'value' => $this
        ->renderOriginalValue($original_value),
      '#wrapper_attributes' => [
        'style' => 'width: 50%',
      ],
    ];
    $value_form['translation'] = [
      '#parents' => [
        'values',
        $index,
        'translation',
      ],
      '#wrapper_attributes' => [
        'style' => 'width: 50%',
      ],
    ];
    $value_form['translation'] = $inline_form
      ->buildInlineForm($value_form['translation'], $form_state);
  }
  return $form;
}