You are here

function strip_utf8mb4_field_attach_presave in Strip 4-byte UTF8 7

Implements hook_field_attach_presave().

File

./strip_utf8mb4.module, line 54
Allow users to Strip 4-byte UTF8 characters. overly long 2 byte sequences, as well as characters above U+10000, and reject overly long 3 byte sequences and UTF-16

Code

function strip_utf8mb4_field_attach_presave($entity_type, $entity) {
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  foreach (field_info_instances($entity_type, $bundle) as $instance) {
    if (isset($instance['widget']['type']) && _strip_utf8mb4_for($instance['widget']['type'])) {

      // Grap the entity metadata wrapper for this field.
      $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);

      // Grap the filed name from the instance.
      $field_name = $instance['field_name'];

      // Get text field values.
      $text_field_data = $entity_wrapper->{$field_name}
        ->value();

      // If we do have data in the field.
      if (is_array($text_field_data) && count($text_field_data) > 0) {

        // Reject not utf8 strings for the field value.
        if (isset($text_field_data['value'])) {
          $text_field_data['value'] = _strip_utf8mb4_for_text_fields($text_field_data['value'], variable_get('strip_utf8mb4_replace_string', '--'));
        }
        else {
          foreach ($text_field_data as $text_field_data_item_key => $text_field_data_item) {
            $text_field_data[$text_field_data_item_key] = _strip_utf8mb4_for_text_fields($text_field_data_item, variable_get('strip_utf8mb4_replace_string', '--'));
          }
        }

        // Reject not utf8 strings for the field summary if we do have.
        if (isset($text_field_data['summary'])) {
          $text_field_data['summary'] = _strip_utf8mb4_for_text_fields($text_field_data['summary'], variable_get('strip_utf8mb4_replace_string', '--'));
        }

        // Save the filtered field data in the entity object.
        $entity_wrapper->{$field_name}
          ->set($text_field_data);
      }
      elseif (is_string($text_field_data) && $text_field_data != '') {
        $text_field_data = _strip_utf8mb4_for_text_fields($text_field_data, variable_get('strip_utf8mb4_replace_string', '--'));

        // Save the filtered field data in the entity object.
        $entity_wrapper->{$field_name} = $text_field_data;
      }
    }
  }

  // If we want to strip none utf8 from Drupal core node's title.
  if (isset($entity->title) && _strip_utf8mb4_for('core_title')) {
    $entity->title = _strip_utf8mb4_for_text_fields($entity->title, variable_get('strip_utf8mb4_replace_string', '--'));
  }
}