You are here

public static function LocalizeFields::fieldAttachViewAlter in Localize Fields 7

Translates field view labels, and corrects decimal separator of decimals/floats.

Used by hook_field_attach_view_alter() implementation.

Parameters

array &$output:

array $context:

1 call to LocalizeFields::fieldAttachViewAlter()
localize_fields_field_attach_view_alter in ./localize_fields.module
Translates field view labels, and corrects decimal separator of decimals/floats.

File

./LocalizeFields.inc, line 924
Drupal Localize Fields module

Class

LocalizeFields
@file Drupal Localize Fields module

Code

public static function fieldAttachViewAlter(&$output, $context) {
  if (($localize = self::$localize) == -1) {

    // Save a method call if possible.
    $localize = self::localize();
  }
  if (!$localize) {
    return;
  }
  $cntxtDelim = self::CONTEXT_DELIMITER;

  // Find entity type, for field_info_instance() look-ups.
  if (!empty($context['entity_type'])) {
    $entity_type = $context['entity_type'];
  }
  elseif (!empty($output['#entity_type'])) {
    $entity_type = $output['#entity_type'];
  }
  else {

    // Abort if only partially implemented custom entity type which fails to
    // provide basic info.
    return;
  }

  // Find bundle, for translation context and field_info_instance() look-ups.
  $bundle = NULL;
  if (!empty($context['entity']) && is_object($context['entity'])) {

    // If entity type: field_collection_item.
    // Try field_name before type, because a misleading type could exist.
    if (!empty($context['entity']->field_name)) {
      $bundle = $context['entity']->field_name;
    }
    elseif (!empty($context['entity']->type)) {
      $bundle = $context['entity']->type;
    }
  }
  if (!$bundle) {
    if (!empty($output['#bundle'])) {
      $bundle = $output['#bundle'];
    }
    else {

      // Abort if only partially implemented custom entity type which fails to
      // provide basic info.
      return;
    }
  }

  // The bundle found may be array in a view.
  if (!is_string($bundle)) {
    return;
  }
  $context = 'field_instance' . $cntxtDelim . $bundle . self::CONTEXT_DELIMITER_BUNDLE;
  foreach ($output as $field_name => &$field) {
    if (is_array($field) && !empty($field['#field_type'])) {
      $context_instance = $context . $field_name . $cntxtDelim;
      if (!empty($field['#title'])) {

        // Not escaped with check_plain() due to double encoding.
        $field['#title'] = self::translateInternal($field['#title'], $context_instance . 'label', TRUE);
      }
      switch ($field['#field_type']) {
        case 'list_boolean':
        case 'list_text':
        case 'list_integer':
        case 'list_decimal':
        case 'list_float':

          // Translate option labels.
          // Is a field_base property.
          $fieldInfo = field_info_field($field_name);
          $translateOptions = empty($fieldInfo['settings']['allowed_values_no_localization']);
          unset($fieldInfo);
          if ($translateOptions) {
            $context_options = 'field' . $cntxtDelim . $field_name . $cntxtDelim . 'allowed_values';
            $limit = 1000;
            for ($i = 0; $i < $limit; ++$i) {
              if (array_key_exists($i, $field)) {

                // Options could easily be integers. Decimals on the other
                // hand may have to be translated because of the decimal
                // separator.
                if (!empty($field[$i]['#markup']) && !ctype_digit($markup = '' . $field[$i]['#markup'])) {

                  // Option labels are not encoded when markup.
                  // Not escaped with field_filter_xss() due to double encoding.
                  $field[$i]['#markup'] = self::translateInternal($markup, $context_options, FALSE);
                }
              }
              else {
                break;

                // Iter.
              }
            }
          }
          break;

        // Switch.
        case 'number_integer':

          // Translate prefix and/or suffix.
          $firstRow = TRUE;
          $prefixLength = $suffixLength = 0;
          $prefix = $suffix = '';
          $limit = 100;
          for ($i = 0; $i < $limit; ++$i) {
            if (array_key_exists($i, $field) && !empty($field[$i]['#markup'])) {
              if ($firstRow) {
                $firstRow = FALSE;
                if (!is_numeric($markup = $field[$i]['#markup'])) {
                  $instanceInfo = field_info_instance($entity_type, $field_name, $bundle);
                  if ($prefix = empty($instanceInfo['settings']['prefix']) ? '' : $instanceInfo['settings']['prefix']) {
                    $prefixLength = strlen($prefix);

                    // Not escaped with field_filter_xss() due to double encoding.
                    $prefix = self::translateInternal($prefix, $context_instance . 'prefix', FALSE);
                  }
                  if ($suffix = empty($instanceInfo['settings']['suffix']) ? '' : $instanceInfo['settings']['suffix']) {
                    $suffixLength = strlen($suffix);

                    // Not escaped with field_filter_xss() due to double encoding.
                    $suffix = self::translateInternal($suffix, $context_instance . 'suffix', FALSE);
                  }
                  unset($instanceInfo);
                  if ($prefixLength || $suffixLength) {
                    $field[$i]['#markup'] = $prefix . substr($markup, $prefixLength, strlen($markup) - ($prefixLength + $suffixLength)) . $suffix;
                  }
                  else {

                    // Don't work on any row. An error really, because the
                    // markup content should be numeric if no prefix or suffix.
                    break;

                    // Iter.
                  }
                }
                else {

                  // No prefix or suffix: nothing to do.
                  break;

                  // Iter.
                }
              }
              else {
                $markup = $field[$i]['#markup'];
                $field[$i]['#markup'] = $prefix . substr($markup, $prefixLength, strlen($markup) - ($prefixLength + $suffixLength)) . $suffix;
              }
            }
            else {

              // No more rows, or definitely no prefix/suffix.
              break;

              // Iter.
            }
          }
          break;

        // Switch.
        case 'number_decimal':
        case 'number_float':

          // Replace decimal separator if not dot.
          $fieldInfo = field_info_field($field_name);
          $decimal_separator = !empty($fieldInfo['settings']['decimal_separator']) ? $fieldInfo['settings']['decimal_separator'] : '.';
          unset($fieldInfo);

          // Translate prefix and/or suffix.
          $firstRow = TRUE;
          $prefixLength = $suffixLength = 0;
          $prefix = $suffix = '';
          $limit = 100;
          for ($i = 0; $i < $limit; ++$i) {
            if (array_key_exists($i, $field) && array_key_exists('#markup', $field[$i])) {

              // Do this only once.
              if ($firstRow) {
                $firstRow = FALSE;
                if (!is_numeric($markup = $field[$i]['#markup'])) {
                  $instanceInfo = field_info_instance($entity_type, $field_name, $bundle);
                  if ($prefix = empty($instanceInfo['settings']['prefix']) ? '' : $instanceInfo['settings']['prefix']) {
                    $prefixLength = strlen($prefix);

                    // Not escaped with field_filter_xss() due to double encoding.
                    $prefix = self::translateInternal($prefix, $context_instance . 'prefix', FALSE);
                  }
                  if ($suffix = empty($instanceInfo['settings']['suffix']) ? '' : $instanceInfo['settings']['suffix']) {
                    $suffixLength = strlen($suffix);

                    // Not escaped with field_filter_xss() due to double encoding.
                    $suffix = self::translateInternal($suffix, $context_instance . 'suffix', FALSE);
                  }
                  unset($instanceInfo);
                  if ($prefixLength || $suffixLength) {
                    $value = substr($markup, $prefixLength, strlen($markup) - ($prefixLength + $suffixLength));
                    if ($decimal_separator != '.') {
                      $value = str_replace('.', $decimal_separator, $value);
                    }
                    $field[$i]['#markup'] = $prefix . $value . $suffix;
                  }
                  elseif ($decimal_separator != '.') {
                    $field[$i]['#markup'] = str_replace('.', $decimal_separator, $markup);
                  }
                  else {

                    // Nothing to do for any row.
                    break;

                    // Iter.
                  }
                }
                elseif ($decimal_separator != '.') {
                  $field[$i]['#markup'] = str_replace('.', $decimal_separator, $markup);
                }
                else {

                  // Nothing to do for any row.
                  break;

                  // Iter.
                }
              }
              else {
                $markup = $field[$i]['#markup'];
                if ($prefixLength || $suffixLength) {
                  $value = substr($markup, $prefixLength, strlen($markup) - ($prefixLength + $suffixLength));
                  if ($decimal_separator != '.') {
                    $value = str_replace('.', $decimal_separator, $value);
                  }
                  $field[$i]['#markup'] = $prefix . $value . $suffix;
                }
                else {
                  $field[$i]['#markup'] = str_replace('.', $decimal_separator, $markup);
                }
              }
            }
            else {

              // No more rows, or a row misses #markup bucket (then an error).
              break;

              // Iter.
            }
          }
          break;

        // Switch.
        case 'field_collection':
        default:

          // Multi-value'd field_collection displays description.
          if (!empty($field['#suffix']) && strpos($field['#suffix'], 'field-collection-description')) {
            $matches = array();
            $pattern = '/(<div[^>]+field\\-collection\\-description[^>]+>)([^<]+)(<\\/div>)/';
            preg_match($pattern, $field['#suffix'], $matches);
            if ($matches && !empty($matches[3]) && ($instanceInfo = field_info_instance($entity_type, $field_name, $bundle)) && !empty($instanceInfo['description'])) {

              // Not escaped with field_filter_xss() due to double encoding.
              $translatedDescription = self::translateInternal($instanceInfo['description'], $context_instance . 'description', FALSE);
              if ($translatedDescription != $instanceInfo['description']) {
                $field['#suffix'] = preg_replace($pattern, '$1' . $translatedDescription . '$3', $field['#suffix']);
              }
            }
            unset($matches);
          }
          break;
      }
    }
  }
  unset($field);

  // Iteration ref
}