Writing custom Entity Operation handlers

Last updated on
30 April 2025

Rendered content handler

This is an example for a handler that returns rendered content (as opposed to a form). It is thus not available as an action.

If you are subclassing EntityOperationsOperationPage, there is relatively little that needs to be done. EntityOperationsOperationToken and EntityOperationsOperationDevel are good examples to look at.

The following methods are needed:

  • operationInfo(): returns basic information about this operation. This just needs to specify the 'label' and 'description', which are used administratively
  • operationStrings(): returns UI strings. This needs to specify the 'tab title'. Other things may be specified too: see the documentation for this method in the base class.
  • operationAccess(): returns logical access to the operation. For example, if your operation only makes sense in certain circumstances, or requires certain modules to be present.
  • userAccess(): checks access for the current user. If you want to use entity_access(), you can simply set the $access_verb class property instead of overriding this method.
  • build(): this should return the output for your operation

Action handler without a form

Some actions don't need any input from the user. EntityOperationsOperationPublish is a simple example to follow.

The base action handler class takes care of generating the form. The UI strings for this are set in operationStrings(), so that subclasses can easily change them. The various parts of the form are only output as needed, so for example within VBO, the submit button and confirmation text are not output, as VBO supplies its own.

The following methods are needed:

  • operationInfo(): as in the example above.
  • operationStrings(): returns UI strings. This needs to specify the 'tab title' as well as strings for the operation: see the documentation for this for details.
  • operationAccess(): as in the example above. In the case of EntityOperationsOperationPublish, this checks that the current entity is in a position where 'publishing' it makes sense. Thus, on an already published entity this denies access to the operation.
  • userAccess(): as in the example above.
  • execute(): this should performs the action and save the entity if needed

Action handler with a form

More complex actions may require user input. A good example is EntityOperationsOperationSetOwner, which allows the owner of an entity to be set, and shows a user autocomplete widget in its form.

Action operations can be used either in the UI, with a form, or programatically, by passing parameters. operationInfo() defines the parameters that are expected, for consumers of operations such as Services to make use of.

In addition to the methods in the example above, the operation handler should additionally implement:

  • operationInfo(): In addition to the admin labels, this should define the parameters
  • form(): this should return the form for the operation. It should not include a submit button: this is added by EntityOperationsOperationAction as appropriate.
  • formSubmitGetParameters(): this should extra from $form_state the values provided by the user, and use them to build a parameters array.

Help improve this page

Page status: Not set

You can: