W3cubDocs

/Drupal 8

function drupal_schema_get_field_value

drupal_schema_get_field_value(array $info, $value)

Typecasts values to proper datatypes.

MySQL PDO silently casts, e.g. FALSE and '' to 0, when inserting the value into an integer column, but PostgreSQL PDO does not. Look up the schema information and use that to correctly typecast the value.

Parameters

array $info: An array describing the schema field info.

mixed $value: The value to be converted.

Return value

mixed The converted value.

Related topics

Schema API
API to handle database schemas.

File

core/includes/schema.inc, line 219
Schema API handling functions.

Code

function drupal_schema_get_field_value(array $info, $value) {
  // Preserve legal NULL values.
  if (isset($value) || !empty($info['not null'])) {
    if ($info['type'] == 'int' || $info['type'] == 'serial') {
      $value = (int) $value;
    }
    elseif ($info['type'] == 'float') {
      $value = (float) $value;
    }
    elseif (!is_array($value)) {
      $value = (string) $value;
    }
  }
  return $value;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!includes!schema.inc/function/drupal_schema_get_field_value/8.1.x