ProductAttributeTranslationFormTrait.php in Commerce Core 8.2
File
modules/product/src/Form/ProductAttributeTranslationFormTrait.php
View source
<?php
namespace Drupal\commerce_product\Form;
use Drupal\commerce_product\Entity\ProductAttributeInterface;
use Drupal\commerce_product\Entity\ProductAttributeValueInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
trait ProductAttributeTranslationFormTrait {
protected $entityTypeManager;
protected $inlineFormManager;
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;
}
}
if (!$has_translatable_values) {
return $form;
}
$language = $form_state
->get('config_translation_language');
$source_language = $form_state
->get('config_translation_source_language');
$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' => 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;
}
protected function renderOriginalValue(ProductAttributeValueInterface $value) {
$view_builder = $this->entityTypeManager
->getViewBuilder('commerce_product_variation');
$build = [];
foreach ($value
->getFieldDefinitions() as $field_name => $definition) {
if (!$definition
->isTranslatable()) {
continue;
}
if ($definition instanceof BaseFieldDefinition && $field_name != 'name') {
continue;
}
$build[$field_name] = $view_builder
->viewField($value
->get($field_name), [
'label' => 'above',
]);
}
return $build;
}
}