I am using Drupal 7 Alpha 7.

My code:

function fieldTesting_field_schema($field)
{
    switch ($field['type']) {
        case 'latlng':
            $columns = array(
                'latitude' => array(
                    'type' => 'float',
                    'not null' => FALSE,
                ),
                'longitude' => array(
                    'type' => 'float',
                    'not null' => FALSE,
                ),
            );
            break;
    }
    return array('columns' => $columns);
}

Is there anything wrong with that?

I ask because of this error:

Notice: Undefined index: schema_fields_sql in C:\wamp\www\d7a7\includes\entity.inc on line 254 Catchable fatal error: Argument 2 passed to SelectQuery::fields() must be an array, null given, called in C:\wamp\www\d7a7\includes\entity.inc on line 273 and defined in C:\wamp\www\d7a7\includes\database\select.inc on line 1189

My goal is to create a new field for a latitude and longitude pair. You would enter the latitude in one text field, and the longitude in another.

Comments

LaurentAjdnik’s picture

Completely remove your switch structure and let me know:

function fieldTesting_field_schema($field)
{
  $columns = array(
      'latitude' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
      'longitude' => array(
        'type' => 'float',
        'not null' => FALSE,
      ),
  );

  return array('columns' => $columns);
}
jerrac’s picture

Thanks! That worked!

I pulled the initial schema code from this tutorial: http://www.figover.com/node/16

Why was there a switch statement in that tutorial?

Also, what clued you in to what was wrong with my code? I'd like to know, just in case I run into something similar in the future.

LaurentAjdnik’s picture

Hi David,

the switch() is necessary when your module creates different field types.

The number.module does that for obvious reasons:

function number_field_schema($field) {
  switch ($field['type']) {
    case 'number_integer' :
      $columns = array(
        'value' => array(
          'type' => 'int',
          'not null' => FALSE
        ),
      );
      break;

    case 'number_float' :
      $columns = array(
        'value' => array(
          'type' => 'float',
          'not null' => FALSE
        ),
      );
      break;

    case 'number_decimal' :
      $columns = array(
        'value' => array(
          'type' => 'numeric',
          'precision' => $field['settings']['precision'],
          'scale' => $field['settings']['scale'],
          'not null' => FALSE
        ),
      );
      break;
  }
  return array(
    'columns' => $columns,
  );
}

But if you handle only one, there is no need for it.

Here's what's declared in the famous taxonomy.module:

/**
 * Implements hook_field_schema().
 */
function taxonomy_field_schema($field) {
  return array(
    'columns' => array(
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'tid' => array('tid'),
    ),
  );

In your case, I guessed your hook_field_schema() returned NULL, and broke something in the calling function.

Which makes me wonder whether you did the right definiton in hook_field_info for 'latlng'... can you copy/paste it here ?

jerrac’s picture

the switch() is necessary when your module creates different field types.

Ah, I see. That's why it worked when I was creating separate fields for latitude and longitude.

Here's my hook_field_info().

function fieldTesting_field_info() {
  return array(
    'latlng' => array(
      'label' => t('Latitude and Longitude'),
      'description' => t('Store the decimal value of a latitude and longitude pair'),
      'default_widget' => 'latlng_text',
      'default_formatter' => 'latlng_simple_text',
    ),
  );
}
LaurentAjdnik’s picture

Seems OK to me. Check my comment at http://drupal.org/node/919620#comment-3480158.