You are here

private function LingotekContentTranslationService::processCohesionComponentsValues in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 3.3.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::processCohesionComponentsValues()
1 call to LingotekContentTranslationService::processCohesionComponentsValues()
LingotekContentTranslationService::getSourceData in src/LingotekContentTranslationService.php
Returns the source data that will be uploaded to the Lingotek service.

File

src/LingotekContentTranslationService.php, line 816

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

private function processCohesionComponentsValues($values, $component_model, $model_key = []) {
  $data_layout = [];
  foreach ($values as $key => $value) {

    // If the key in the model matches a uuid then it a component field value
    // If the model contains (property) and is TRUE, the field is excluded from being expose as translatable
    if (preg_match(ElementModel::MATCH_UUID, $key)) {
      if (is_array($value)) {
        foreach ($value as $index => $inner_value) {
          $data_layout = array_merge($data_layout, $this
            ->processCohesionComponentsValues($inner_value, $component_model, array_merge($model_key, [
            $key,
            $index,
          ])));
        }
      }
      elseif (isset($component_model[$key]) && $component_model[$key]
        ->getProperty([
        'settings',
        'translate',
      ]) !== FALSE) {
        $form_elements = \Drupal::keyValue('cohesion.assets.form_elements');
        $field_uid = $component_model[$key]
          ->getElement()
          ->getProperty('uid');
        $form_field = $form_elements
          ->get($field_uid);
        $model = $component = $component_model[$key]
          ->getElement()
          ->getProperty('uuid');

        // We default to true if not set, so we can fallback to including it
        // if we cannot explicitly discard it.
        $componentType = isset($form_field['model']['settings']['type']) ? $form_field['model']['settings']['type'] : 'notDiscardedType';
        $protectedComponentTypes = [
          'cohTypeahead',
          'cohEntityBrowser',
          'cohFileBrowser',
        ];
        if (!in_array($componentType, $protectedComponentTypes)) {
          if (isset($form_field['translate']) && $form_field['translate'] === TRUE) {

            // Only expose value that is a string or a WYSIWYG
            if (is_string($value) && !empty($value)) {
              $data_layout[$key] = $value;
            }
            elseif (is_object($value) && property_exists($value, 'text') && property_exists($value, 'textFormat')) {
              $data_layout[$key] = Html::escape($value->text);
            }
          }
        }
      }
    }
  }
  return $data_layout;
}