entity_get_controller() is giving me an error when I try to use a class. This class is defined in another file called ingredient.controller.inc

Fatal error: Class name must be a valid object or a string

The following code doesn't work:

 function ingredient_add() {  
   $ingredient = entity_get_controller('ingredient')->create();
   return drupal_get_form('ingredient_add_form',$ingredient);

The following one works;

function ingredient_add() {  
  $ingredient = (object) array('iid' => '','name' => '',);
  return drupal_get_form('ingredient_add_form',$ingredient);

The class code is the following one:

class IngredientController extends DrupalDefaultEntityController {

    public function create() {
      return (object) array(
        'iid' => '',
        'name' => '',

function nutrition_entity_info() {
  $return['ingredient'] = array (
    'label' => t('Ingredient'),
    'controller class' => 'IngredientController',
    'base table' => 'ingredient',
    'uri callback' => 'ingredient_uri',
    'fieldable' => TRUE,
    'entity keys' => array (
      'id' => 'iid',
      'label' => 'name',
    'static cache' => TRUE,
    'view modes' => array (
      'full' => array (
        'label' => t('Full content'),
        'custom settings' => FALSE,
      'teaser' => array(
        'label' => t('Teaser'),
        'custom settings' => FALSE,

  return $return;

Do you have any idea?

Should the implementation of hook_entity_info() be nutrition_entity_info() or ingredient_entity_info()?

Nutrition is the module name, ingredient the entity I am creating.

There was some kind of typo on the code, as it seems to work now.

1 Answer 1


I suspect that you haven't implemented hook_entity_info(), which is why entity_get_controller() returns nothing, and you can't call a method of nothing.

The Examples project has an issue in the queue for an entity example. Give it a read. :-)

Hooks have the module name, so if the module is called nutrition, you'd declare nutrition_entity_info().

  • Hi @paul-m. Yes I did declare the hook, but I forgot to paste it. I updated the question. Thanks! Jan 24, 2012 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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