If you need to define your own form for editing your entity, you can use the two non-generic operation handlers for 'edit' and 'add.

For these, you need the same as in Example 1, and also a form builder function called myentity_form(), along with the corresponding validate and submit handlers. Note that Entity API also allows you to have form builders specific to bundles: these will also work, as the form is obtained with entity_ui_get_form().

If you are using the Entity API admin UI system, and have your entity form in a .inc file, that will be loaded for you provided it is defined in your hook_entity_info's ['admin ui']['file'].

Define the table in hook_schema()

You first need to define a table schema for the entity with hook_schema(). This can contain anything you like.

Define the entity type in hook_entity_info()

The Entity Operations UI requires some properties to be defined in hook_entity_info(), as additional properties to what Drupal Core expects.

The following Entity API properties are also required:

  • 'module': This must specify the providing module for the entity type, i.e., the module that is implementing the hook.
  • 'entity class': This must be set to 'Entity' or a subclass of that.
  • 'access callback': This must be set to the Entity API access callback function, as the operations make use of entity_access().
  • 'admin ui': This is required if using the 'Add' or 'Edit' operation handlers that use Entity API's entity forms system.

The following is an example of defining operations in hook_entity_info():

<?php
function mymodule_entity_info() {
  $return = array(
    'myentity' => array(
      // Core properties.
      // ...
      // Entity API properties.
      'module' => 'mymodule',
      'entity class' => 'Entity',
      'access callback' => 'myentity_access',
      'admin ui' => array(
        'path' => 'admin/structure/myentity',
        'file' => 'myentity.admin.inc',
      ),
      // Entity Operations API
      'operations ui' => array(
        // The base path for your entities. This is the same as your entity's URI
        // but without the ID suffix. (In fact, you can set
        // entity_operations_entity_uri() as your URI callback, which will use the
        // value here).
        'path' => 'myentity',
      ),
    ),
  );
  return $return;
}
?>

Define the operations

The following is an example of defining operations in hook_entity_operation_info() with the entity form operation handlers:

<?php
function mymodule_entity_operation_info() {
  $info = array(
    'myentity' => array(
      // List of operations this entity type uses.
      'view' => array(
        // The handler class.
        'handler' => 'EntityOperationsOperationEntityView',
        // The places this operation is exposed.
        'provision' => array(
          // Show as an entity tab.
          'menu' => array(
            // Marks this as the default operation tab, that is, the one that
            // shows for the entity's URI.
            'default' => TRUE,
            // Properties for the menu item.
            'menu item' => array(
              // Override the title.
              'title' => 'Foobar',
            ),
          ),
          // A field handler for Views.
          // If there are no further settings, for easier DX it suffices to set
          // a provision item's value to TRUE.
          'views field' => TRUE,
        ),
      ),
      'add' => array(
        'handler' => 'EntityOperationsOperationAdd',
        'provision' => array(
          'menu' => TRUE,
        ),
      ),
      'edit' => array(
        'handler' => 'EntityOperationsOperationEdit',
        'provision' => array(
          'menu' => TRUE,
          'views field' => TRUE,
        ),
      ),
      'publish' => array(
        'handler' => 'EntityOperationsOperationEntityPublish',
        'provision' => array(
          // Show the operation form as an entity tab.
          'menu' => TRUE,
        ),
      ),
    ),
  );
  return $info;
}
?>