You are here

public function DumperPluginManager::fixDumperFieldIncompatibility in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 src/DumperPluginManager.php \Drupal\geocoder\DumperPluginManager::fixDumperFieldIncompatibility()

Check|Fix some incompatibility between Dumper output and Field Config.

Parameters

string $dumper_result: The Dumper result string.

\Drupal\geocoder\DumperInterface|\Drupal\Component\Plugin\PluginInspectionInterface $dumper: The Dumper.

\Drupal\Core\Field\FieldConfigInterface $field_config: The Field Configuration.

File

src/DumperPluginManager.php, line 115

Class

DumperPluginManager
Provides a plugin manager for geocoder dumpers.

Namespace

Drupal\geocoder

Code

public function fixDumperFieldIncompatibility(&$dumper_result, $dumper, FieldConfigInterface $field_config) {

  // Fix not UTF-8 encoded result strings.
  // https://stackoverflow.com/questions/6723562/how-to-detect-malformed-utf-8-string-in-php
  if (is_string($dumper_result)) {
    if (!preg_match('//u', $dumper_result)) {
      $dumper_result = utf8_encode($dumper_result);
    }
  }

  // If the field is a string|text type check if the result length is
  // compatible with its max_length definition, otherwise truncate it and
  // set | log a warning message.
  if (in_array($field_config
    ->getType(), $this->maxLengthFieldTypes) && strlen($dumper_result) > $field_config
    ->getFieldStorageDefinition()
    ->getSetting('max_length')) {
    $incompatibility_warning_message = t("The '@field_name' field 'max length' property is not compatible with the chosen '@dumper' dumper.<br>Thus <b>be aware</b> <u>the dumper output result has been truncated to @max_length chars (max length)</u>.<br> You are advised to change the '@field_name' field definition or chose another compatible dumper.", [
      '@field_name' => $field_config
        ->getLabel(),
      '@dumper' => $dumper
        ->getPluginId(),
      '@max_length' => $field_config
        ->getFieldStorageDefinition()
        ->getSetting('max_length'),
    ]);
    $dumper_result = substr($dumper_result, 0, $field_config
      ->getFieldStorageDefinition()
      ->getSetting('max_length'));

    // Display a max-length incompatibility warning message.
    $this->messenger
      ->addMessage($incompatibility_warning_message, 'warning');

    // Log the max-length incompatibility.
    $this->logger
      ->get('geocoder')
      ->warning($incompatibility_warning_message);
  }
}