You are here

protected function LingotekContentTranslationService::extractCohesionComponentValues in Lingotek Translation 3.7.x

Same name and namespace in other branches
  1. 4.0.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::extractCohesionComponentValues()
  2. 3.5.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::extractCohesionComponentValues()
  3. 3.6.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::extractCohesionComponentValues()
  4. 3.8.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::extractCohesionComponentValues()
1 call to LingotekContentTranslationService::extractCohesionComponentValues()
LingotekContentTranslationService::getSourceData in src/LingotekContentTranslationService.php
Returns the source data that will be uploaded to the Lingotek service.

File

src/LingotekContentTranslationService.php, line 914

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

protected function extractCohesionComponentValues(array $component_model, $values) {
  $field_values = [];
  foreach ($values as $key => $value) {

    // If the key does not match a UUID, then it's not a component field and we can skip it.
    if (!preg_match(ElementModel::MATCH_UUID, $key)) {
      continue;
    }
    $component = $component_model[$key] ?? NULL;

    // If we can't find a component with this uuid, we skip it.
    if (!$component) {
      continue;
    }
    $settings = $component
      ->getProperty('settings');

    // Skip this field if the component is not translatable.
    if (($settings->translate ?? NULL) === FALSE) {
      continue;
    }
    $skippedComponentTypes = [
      'cohTypeahead',
      'cohEntityBrowser',
      'cohFileBrowser',
    ];
    $component_type = $settings->type ?? NULL;
    if (in_array($component_type, $skippedComponentTypes)) {
      continue;
    }

    // Handle Field Repeaters before checking if the field is translatable,
    // since Field Repeater fields aren't but their contents are.
    if ($component_type === 'cohArray') {
      foreach ($value as $index => $item) {
        $field_values[$key][$index] = $this
          ->extractCohesionComponentValues($component_model, (array) $item);
      }
    }
    $form_field = \Drupal::keyValue('cohesion.assets.form_elements')
      ->get($component
      ->getElement()
      ->getProperty('uid'));
    if (($form_field['translate'] ?? NULL) !== TRUE) {

      // Skip if the form_field is not translatable.
      continue;
    }
    $schema_type = $settings->schema->type ?? NULL;
    switch ($schema_type) {
      case 'string':
        if (!empty($value)) {
          $field_values[$key] = $value;
        }
        break;
      case 'object':
        switch ($component_type) {
          case 'cohWysiwyg':
            if (!empty($value->text)) {
              $field_values[$key] = $value->text;
            }
            break;
          default:
            \Drupal::logger('lingotek')
              ->warning('Unhandled component type of \'%type\' (schema type: %schema) encountered when extracting cohesion component values.', [
              '%type' => $component_type,
              '%schema' => $schema_type,
            ]);
            break;
        }
        break;
      default:
        \Drupal::logger('lingotek')
          ->warning('Unhandled schema type of \'%type\' encountered when extracting cohesion component values.', [
          '%type' => $schema_type,
        ]);
        break;
    }
  }
  return $field_values;
}