You are here

public function DefaultFieldProcessor::extractTranslatableData in Translation Management Tool 8

Extracts the translatatable data structure from the given field.

Parameters

\Drupal\Core\Field\FieldItemListInterface $field: The field object.

Return value

array $data An array of elements where each element has the following keys:

  • #text
  • #translate

Overrides FieldProcessorInterface::extractTranslatableData

See also

\Drupal\tmgmt_content\Plugin\tmgmt\Source\ContentEntitySource::extractTranslatableData()

2 calls to DefaultFieldProcessor::extractTranslatableData()
LinkFieldProcessor::extractTranslatableData in sources/content/src/LinkFieldProcessor.php
Extracts the translatatable data structure from the given field.
PathFieldProcessor::extractTranslatableData in sources/content/src/PathFieldProcessor.php
Extracts the translatatable data structure from the given field.
3 methods override DefaultFieldProcessor::extractTranslatableData()
LinkFieldProcessor::extractTranslatableData in sources/content/src/LinkFieldProcessor.php
Extracts the translatatable data structure from the given field.
MetatagsFieldProcessor::extractTranslatableData in sources/content/src/MetatagsFieldProcessor.php
Extracts the translatatable data structure from the given field.
PathFieldProcessor::extractTranslatableData in sources/content/src/PathFieldProcessor.php
Extracts the translatatable data structure from the given field.

File

sources/content/src/DefaultFieldProcessor.php, line 20

Class

DefaultFieldProcessor
Default field processor.

Namespace

Drupal\tmgmt_content

Code

public function extractTranslatableData(FieldItemListInterface $field) {
  $data = array();

  /* @var \Drupal\Core\Field\FieldItemInterface $field_item */
  $field_definition = $field
    ->getFieldDefinition();
  foreach ($field as $delta => $field_item) {
    $format = NULL;
    $translatable_properties = 0;
    foreach ($field_item
      ->getProperties() as $property_key => $property) {

      // Ignore values that are not primitives.
      if (!$property instanceof PrimitiveInterface) {
        continue;
      }
      $property_definition = $property
        ->getDataDefinition();
      $translate = $this
        ->shouldTranslateProperty($property);

      // All the labels are here, to make sure we don't have empty labels in
      // the UI because of no data.
      if ($translate === TRUE) {
        $data['#label'] = $field_definition
          ->getLabel();
        if (count($field) > 1) {

          // More than one item, add a label for the delta.
          $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 there is only one translatable property, remove the label for it.
    if ($translatable_properties <= 1 && !empty($data)) {
      foreach (Element::children($data[$delta]) as $property_key) {
        unset($data[$delta][$property_key]['#label']);
      }
    }
  }
  return $data;
}