DefaultFieldProcessor.php in Translation Management Tool 8
File
sources/content/src/DefaultFieldProcessor.php
View source
<?php
namespace Drupal\tmgmt_content;
use Drupal\Core\Render\Element;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\TypedData\PrimitiveInterface;
use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\Core\TypedData\TypedDataInterface;
class DefaultFieldProcessor implements FieldProcessorInterface {
public function extractTranslatableData(FieldItemListInterface $field) {
$data = array();
$field_definition = $field
->getFieldDefinition();
foreach ($field as $delta => $field_item) {
$format = NULL;
$translatable_properties = 0;
foreach ($field_item
->getProperties() as $property_key => $property) {
if (!$property instanceof PrimitiveInterface) {
continue;
}
$property_definition = $property
->getDataDefinition();
$translate = $this
->shouldTranslateProperty($property);
if ($translate === TRUE) {
$data['#label'] = $field_definition
->getLabel();
if (count($field) > 1) {
$data[$delta]['#label'] = t('Delta #@delta', array(
'@delta' => $delta,
));
}
}
$data[$delta][$property_key] = array(
'#label' => $property_definition
->getLabel(),
'#text' => $property
->getValue(),
'#translate' => $translate,
);
$translatable_properties += (int) $translate;
if ($translate && $field_item
->getFieldDefinition()
->getFieldStorageDefinition()
->getSetting('max_length') != 0) {
$data[$delta][$property_key]['#max_length'] = $field_item
->getFieldDefinition()
->getFieldStorageDefinition()
->getSetting('max_length');
}
if ($property_definition
->getDataType() == 'filter_format') {
$format = $property
->getValue();
}
}
if (!empty($format)) {
$data = $this
->handleFormat($format, $data, $delta);
}
if ($translatable_properties <= 1 && !empty($data)) {
foreach (Element::children($data[$delta]) as $property_key) {
unset($data[$delta][$property_key]['#label']);
}
}
}
return $data;
}
public function setTranslations($field_data, FieldItemListInterface $field) {
foreach (Element::children($field_data) as $delta) {
$field_item = $field_data[$delta];
foreach (Element::children($field_item) as $property) {
$property_data = $field_item[$property];
if (isset($property_data['#translation']['#text']) && $property_data['#translate']) {
if (!$field
->offsetExists($delta)) {
$translation = $field
->getEntity();
$source = $translation
->getUntranslated();
$source_field = $source
->get($field
->getName());
$source_offset = $source_field
->offsetGet($delta);
$field
->offsetSet($delta, $source_offset);
}
$field
->offsetGet($delta)
->set($property, $property_data['#translation']['#text']);
}
}
}
}
protected function shouldTranslateProperty(TypedDataInterface $property) {
if ($property instanceof OptionsProviderInterface || !$property instanceof StringInterface) {
return FALSE;
}
return TRUE;
}
protected function handleFormat($format, $data, $delta) {
$allowed_formats = (array) \Drupal::config('tmgmt.settings')
->get('allowed_formats');
if ($allowed_formats && array_search($format, $allowed_formats) === FALSE) {
foreach ($data[$delta] as $name => $value) {
if (is_array($value) && isset($value['#translate'])) {
$data[$delta][$name]['#translate'] = FALSE;
}
}
}
else {
foreach ($data[$delta] as $name => $value) {
if (is_array($value) && isset($value['#translate']) && $value['#translate'] == TRUE) {
$data[$delta][$name]['#format'] = $format;
}
}
}
return $data;
}
}