2

I have an entity with a textfield created using the core Fields feature that looks like this when I inspect the type of the field table in the database varchar(40).

I want to be able to determine max size of the string that can go into this field programmatically. I.e. I want do know if there is a function I can call with suitable parameters, and it would return 40.

A Drupal API function would be the preferred answer, but a could live with a PHP function if this is not available in the Drupal API.

I was reminded in a comment (now deleted) that I previously had answered a very similar question.

However, that answer relies on the field in question being part of a schema.

This new question is about fields that has been created using the core Fields feature. (I.e. using drupal_get_schema() will not fly.

  • Did you try using the field definition function(s)? – Kevin Jul 14 at 16:20
  • @Kevin, no. Hints about exact what function to try will be appreciated. In particualar if it is for Drupal 7. – Free Radical Jul 14 at 16:22
3

There's no single function that will extract the value for you, but field_info_field() will get you an array that contains the data:

$info = field_info_field('field_foo');
if (isset($info['columns']['value']['length'])) {
  // Do something with $info['columns']['value']['length']
}

It it's a core text field, value will always be the name of the column you need to check. For other field types it may differ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.