Search code examples
zend-frameworkpimcore

Inheritable (?) controller, action and template


When creating a document as another documents child, it is created with a default controller, action and template settings.

I want to set it up so that the controller, action and template fileds would get the values from their adjacent documents in a parent-child structure.

Is there an out of the box solution to this problem or maybe someone has done this in some other way?


Solution

  • "Out of the box" it's only possible to create named (predefined) document types with controller, action, template values. Those types are available on "add document" context menu in document tree.

    But I see two likely solutions:

    1. You can define custom Document_Page class and overload create() method.

      public static function create($parentId, $data = array())
      {
          $document = static::create($parentId, $data = array());
      
          $parent = Document::getById($parentId);
          $document->setModule($parent->getModule());
          $document->setController($parent->getController());
          $document->setAction($parent->getAction());
          $document->setTemplate($parent->getTemplate());
          $document->save();
      
          return $document;
      }
      

      Then you must declare your custom class in classmap.xml:

      <Document_Page>Website_Document_Page</Document_Page>
      
    2. Create a plugin and define postAddDocument(Document $document) hook:

      public static function postAddDocument(Document $document)
      {
          $parent = $document->getParent();
          $document->setModule($parent->getModule());
          $document->setController($parent->getController());
          $document->setAction($parent->getAction());
          $document->setTemplate($parent->getTemplate());
          $document->save();
      
          return $document;
      }