Polymorphism

From Achievo/ATK Wiki

Jump to: navigation, search

ATK Howto: Polymorphism

Complexity: expert
Author: Ivo Jansch <ivo@achievo.org>

List of other Howto's

An example of polymorphism is given in a question that was sent to the mailinglist:


"Can I override view/edit action to go to other node? Say I have a node called Main which display part of table Main. Edit and View button should go to certain nodes based on particular attribute's value in node Main."


The best way to accomplish this is to override the link of the edit icon for a record, and have it link to another node instead of the current one.

Try to add the following to your main node:

function recordActions($record, &$actions, &$mraactions)
{
  switch($record[["type"])
  {
    case "fruit": 
      $actions["edit"] = dispatch_url("yourmodule.fruit", "edit");
      $actions["view"] = dispatch_url("yourmodule.fruit", "view");
      break;
    case "car":
      $actions["edit"] = dispatch_url("yourmodule.car", "edit");
      $actions["view"] = dispatch_url("yourmodule.car", "view");
      break;
    default:
      // do nothing, this will result in the default edit links.
  }
}

If the 'fruit' and 'car' nodes are derived from the 'main' node (which only contains the fields that are both applicable to cars and fruit), this is a very nice way to achieve polymorphism.

Original Poster: Ivo

Personal tools
Navigation