We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17815
    • 50 Posts
    Hi everyone, I have question,

    how can i create (via plugin) few subdocuments for created page?

    for example, there is folder "cars", when manager creates new document in this folder, i want automaticaly create few subdocs, for example "techspec, videos, photos etc"

    as i understand plugin must hook event OnDocFormSave and check either saved document is part of catalog, and is it new or old, and if new create subdocuments.

    but i do not found no examples for OnDocFormSave and about creating documents




    So, here is answer:

    For example, lets create sub docs "video" and "photo" for subdocuments of folder "products"
    So if i create document "/product/product-1", plugin will automaticaly create documents "/product/product-1/video" and "/product/product-1/photo"


    $e = &$modx->Event;
    switch($e->name) {
        case 'OnDocFormSave':
            
            global $id;
            global $parent;
            global $pagetitle;
            global $alias;
            global $template;
    
    $constProductsPageId = 2; //id of catalog page, where producs saved
    
    $IsProduct = ( $parent == $constProductsPageId );
    if($IsProduct) {
    
    //mark saved product as folder
    $modx->db->update("isfolder='1'",$modx->getFullTableName('site_content'), "id=".$id."");
    
    //check is there already video document
    //in my case, i check it via unused field description
    $IsThereVideo = ( 1 == $modx->db->getRecordCount( $modx->db->select( 'description', $modx->getFullTableName('site_content'), "description='video' AND parent = ".$id ) ));
    
    //and if there is no video - create new subdocument
    if(!$IsThereVideo) $modx->db->query("INSERT INTO ".$modx->getFullTableName('site_content')." VALUES (null, 'document', 'text/html', 'Video', 'Video', 'video', 'video', '', 1, 0, 0, ".$id.", 1, '', '', 1, 9, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', 0, 0, 0, 0, 0, 0, 0);");
    
    
    } 
            break;
            
        default:
            return;
            break;
    }