Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm trying to follow this article to build my first entity using Entity API module. I'm unclear about the dependency between controller classes and include files. If it says:

'controller class' => 'ExampleTaskController',

does this mean the class has to be in a separate file named exactly as the class name (as it is in Views)?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

No.

'controller class' => 'ExampleTaskController',

controller class: The name of the class that is used to load the objects. The class has to implement the DrupalEntityControllerInterface interface. Leave blank to use the DrupalDefaultEntityController implementation.

Says nothing about where the class code actually lives in Drupal. The easiest place to put it is in the same file your declaring hook_entity_info. Because since it's in the same file it will be found by PHP because the class will have the same file scope as your hook_entity_info implementation, and likely the other hook functions needed for your entity.

In PHP there are only 3 scopes: file scope, function scope and global scope.

So how can it live outside of say YOURMODULE.module. You declare where this class can be found in your YOURMODULE.info file with the files[] directive.

Such as:

name = my custom module
package = other
files[]= EntityClassController.inc

Files defined by the files[] entries are scanned for class definitions and included automatically by Drupal in its bootstrap phase to be available as-needed by running code. So your class would live in the file location of DRUPAL_ROOT\sites\all\modules\mymodule\EntityConroller.inc as an example.

Alot of times to organize code better developers add subfolders, or submodules to their modules you could for instance declare the class to live in an includes folder in your modules folder with:

files[]= includes/ExampleTaskContoller.inc

Some hook functions such as hook_views_api and hook_menu allow you to define a file location where the page callback function can be found (in the case of hook_menu) or where your views implementation classes/handlers/etc may be found in the case of views.

For instance hook_menu allows for the following 2 keys to better organize where page callback functions live in your source code:

"file": A file that will be included before the page callback is called; this allows page callback functions to be in separate files. The file should be relative to the implementing module's directory unless otherwise specified by the "file path" option. Does not apply to other callbacks (only page callback).

"file path": The path to the directory containing the file specified in "file". This defaults to the path to the module implementing the hook.

Views allows 1 to define where views includes lives via hook_views_api:

path: (optional) If includes are stored somewhere other than within the root module directory, specify its path here.

template path: (optional) A path where the module has stored it's views template files. When you have specificed this key views automatically uses the template files for the views. You can use the same naming conventions like for normal views template files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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