public function DefaultFieldProcessor::extractTranslatableData

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 22

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(TRUE) as $property_key => $property) {
      $property_definition = $property
        ->getDataDefinition();
      $translate = $this
        ->shouldTranslateProperty($property);
      if ($property_definition instanceof DataReferenceDefinitionInterface && $property_definition
        ->getTargetDefinition()
        ->getDataType() == 'entity:file') {
        $target = $property
          ->getValue();
        if ($target instanceof FileInterface) {
          $config = \Drupal::config('tmgmt.settings');
          if (in_array($target
            ->getMimeType(), (array) $config
            ->get('file_mimetypes'))) {
            $translate = TRUE;
            $data[$delta][$property_key] = [
              '#label' => t('File'),
              '#file' => $target
                ->id(),
              '#translate' => $translate,
            ];
          }
        }
      }

      // Ignore values that are not primitives and other computed properties.
      if (!$property instanceof PrimitiveInterface || $property_definition
        ->isComputed()) {
        continue;
      }

      // 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;
}