1

I am creating a fieldable custom entity in a module, and need to attach three taxonomy term reference fields to the entity. My entity is declared in my module's .module file as follows:

function dr_person_entity_info() { $return['person'] = array( 'label' => t('Person'), 'plural label' =>t('People'), 'description' => t('A person in the family tree'), 'entity class' => 'DRPersonEntity', 'controller class' => 'DRPersonAPIController', 'base table' => 'dr_person', 'revision table' => 'dr_person_revision', 'fieldable' => TRUE, //'label callback' => 'entity_class_label', 'uri callback' => 'entity_class_uri', 'module' => 'dr_person', 'access callback' => 'dr_person_access', 'entity keys' => array( 'id' => 'id', 'language' => 'language', 'revision' => 'vid', ), 'bundle keys' => array( 'bundle' => 'type', ), 'bundles' => array( 'person' => array( 'label' => t('Person'), 'admin' => array( 'path' => 'admin/structure/types/manage/person', 'access arguments' => array('administer person types'), ), ), ), 'load hook' => 'dr_person_load', 'view modes' => array( 'full' => array( 'label' => t('Person'), 'custom settings' => FALSE, ), ), ); return $return; }

I've got my table schemas declared in hook_schema() in the module's .install file as follows:

function dr_person_schema() { $schema['dr_person'] = array( 'description' => 'The base table for "person" entities.', 'fields' => array( 'id' => array( 'description' =>'The primary identifier for a person.', 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, ), 'vid' => array( 'description' => 'The current {dr_person_revision}.vid version identifier', 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), 'language' => array( 'description' => 'The {languages}.language of this record.', 'type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => '', ), //insert entity specific fields here 'given_name' => array( 'description' => 'A person\'s given (first) name', 'type' => 'varchar', 'length' => 100, 'not null' => FALSE, 'default' => '', ), 'middle_name' => array( 'description' => 'A person\'s middle name', 'type' => 'varchar', 'length' => 100, 'not null' => FALSE, 'default' => '', ), 'surname' => array( 'description' => 'A person\'s surname', 'type' => 'varchar', 'length' => 120, 'not null' => FALSE, 'default' => '', ), 'living' => array( 'description' => 'Boolean indicating whether the person is living.', 'type' => 'int', 'not null' => TRUE, 'default' => 1, ), //end entity specific fields 'uid' => array( 'description' => 'The {users}.uid that owns this person; initially, this is the user that created it.', 'type' => 'int', 'not null' => TRUE, 'default' => 0, ), 'status' => array( 'description' => 'Boolean indicating whether the person is published (visible to non-administrators).', 'type' => 'int', 'not null' => TRUE, 'default' => 1, ), 'created' => array( 'description' => 'The Unix timestamp when the person was created.', 'type' => 'int', 'not null' => TRUE, 'default' => 0, ), 'changed' => array( 'description' => 'The Unix timestamp when the person was most recently saved.', 'type' => 'int', 'not null' => TRUE, 'default' => 0, ), ), 'indexes' => array( 'person_changed' => array('changed'), 'person_created' => array('created'), 'person_status' => array('status', 'id'), 'uid' => array('uid'), 'language' => array('language'), ), 'unique keys' => array( 'person_id_vid' => array('id', 'vid'), //'person_vid' => array('vid'), ), 'foreign keys' => array( 'dr_person_revision' => array( 'table' => 'dr_person_revision', 'columns' => array('vid' => 'vid'), ), 'person_author' => array( 'table' => 'users', 'columns' => array('uid' => 'uid'), ), ), 'primary key' => array('id'), );

$schema['dr_person_revision'] = array(
    'description' => 'The revision table for "person" entities.',
    'fields' => array(
        'id' => array(
            'description' => 'The person this version belongs to.',
            'type' => 'int',
            'unsigned' => TRUE,
            'not null' => TRUE,
            'default' => 0,
        ),
  'vid' => array(
    'description' => 'The primary identifier for this version.',
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ),
  'uid' => array(
    'description' => 'The {users}.uid that created this version.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ),
        //insert entity specific fields here
        'given_name' => array(
            'description' => 'A person\'s given (first) name',
            'type' => 'varchar',
            'length' => 100,
            'not null' => FALSE,
            'default' => '<unknown>',
        ),
        'middle_name' => array(
            'description' => 'A person\'s middle name',
            'type' => 'varchar',
            'length' => 100,
            'not null' => FALSE,
            'default' => '<unknown>',
        ),
        'surname' => array(
            'description' => 'A person\'s surname',
            'type' => 'varchar',
            'length' => 120,
            'not null' => FALSE,
            'default' => '<unknown>',
        ),
        'living' => array(
    'description' => 'Boolean indicating whether the person is living.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
        ),
        //end entity specific fields
  'log' => array(
    'description' => 'The log entry explaining the changes in this version.',
    'type' => 'text',
    'not null' => FALSE,
    'size' => 'big',
  ),
  'timestamp' => array(
    'description' => 'A Unix timestamp indicating when this version was created.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ),
  'status' => array(
    'description' => 'Boolean indicating whether the person (at the time of this revision) is published (visible to non-administrators).',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
  ),
),
'indexes' => array(
  'id' => array('id'),
  'uid' => array('uid'),
),
'primary key' => array('vid'),
'foreign keys' => array(
  'versioned_person' => array(
    'table' => 'dr_person',
    'columns' => array('id' => 'id'),
  ),
  'version_author' => array(
    'table' => 'users',
    'columns' => array('uid' => 'uid'),
  ),
),
);
return $schema;

}

I am attaching my taxonomy term reference fields for taxonomy vocabularies created in a separate module (which is a dependency for this one), in hook_install() as follows:

function dr_person_install() { cache_clear_all(); //Name Prefix

$field = array(
    'field_name' => 'field_person_name_prefix',
    'type' => 'taxonomy_term_reference',
    'label' => t('Prefix'),
    'settings' => array(
        'allowed_values' => array(
            array(
                'vocabulary' => 'prefix',
                'parent' => 0,
            )
        ),
    ),
    'cardinality' => 1,
);
field_create_field($field);

$instance = array(
    'field_name' => 'field_person_name_prefix',
    'entity_type' => 'person',
    'bundle' => 'person',
    'label' => t('Prefix'),
    'description' => t('Name Prefix'),
    'required' => FALSE,
    'widget' => array(
        'type' => 'taxonomy_autocomplete',
        'weight' => -10,
    ),
);
field_create_instance($instance);
//Name Suffix
$field = array(
    'field_name' => 'field_person_name_suffix',
    'type' => 'taxonomy_term_reference',
    'label' => t('Suffix'),
    'settings' => array(
        'allowed_values' => array(
            array(
                'vocabulary' => 'suffix',
                'parent' => 0,
            )
        ),
    ),
    'cardinality' => 1,
);
field_create_field($field);

$instance = array(
    'field_name' => 'field_person_name_suffix',
    'entity_type' => 'person',
    'bundle' => 'person',
    'label' => t('Sufffix'),
    'description' => t('Name Suffix'),
    'required' => FALSE,
    'widget' => array(
        'type' => 'taxonomy_autocomplete',
        'weight' => 10,
    ),
);
field_create_instance($instance);

//Gender
    $field = array(
    'field_name' => 'field_person_gender',
    'type' => 'taxonomy_term_reference',
    'label' => t('Gender'),
    'settings' => array(
        'allowed_values' => array(
            array(
                'vocabulary' => 'gender',
                'parent' => 0,
            )
        ),
    ),
    'required' => TRUE,
    'cardinality' => 1,
);
field_create_field($field);

$instance = array(
    'field_name' => 'field_person_gender',
    'entity_type' => 'person',
    'bundle' => 'person',
    'label' => t('Gender'),
    'description' => t('Gender'),
    'required' => TRUE,
    'widget' => array(
        'type' => 'options_select',
        'weight' => 15,
    ),
);
field_create_instance($instance);

}

The entity gets created, the tables get inserted in the database, and I can attach the fields to the custom entity add/edit form with no problem, and instances of the entity can be created WITH the attached taxonomy term fields. However, when the module is installed, the following warning is thrown, twice:

array_keys() expects parameter 1 to be array, null given in drupal_schema_fields_sql() (line 7076 of /var/public/www.drupalroots.com/public_html/includes/common.inc).

The warning happens when field_create_field() is called the first time in the hook_install function (it doesn't happen for the 2nd or third taxonomy term fields). The issue appears to be that, when drupal_schema_fields_sql() is called from field_create_field(), it is passing the name of the base table and the revision table as defined in hook_entity_info(), but the call to drupal_get_schema() on those tables returns a FALSE, which would only happen if the tables do not exist. According to the documentation on hook_schema(), it should have already been called when hook_install() is run.

Any ideas on getting rid of the warnings when the module is installed? Everything else appears to be working properly (the fields can be attached to my entity add/edit forms during form creating, and values entered into the resulting entity add/edit forms are stored in the taxonomy term reference fields when an instance of the entity is created and saved).

Your Answer

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

Browse other questions tagged or ask your own question.