We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15001
    • 697 Posts
    Hi,

    I’m currently writing a plugin which should allow getting the content of a document from the file system and need some help.

    The idea comes from a discussion on the French section of the forum, "Vos trucs et astuces pour aller plus vite avec MODx ?", i.e. in English "Your tips and tricks to go faster with MODx ?"

    Helitrope posted some plugin allowing to load any template code from an external file. Very useful.
    http://modxcms.com/forums/index.php/topic,31819.msg197423.html#msg197423
    In the template source code field, type: @FILE:path_to_template (from assets/templates)
    Associated event : OnLoadWebDocument.

    Now, I’m trying to do the same for documents : create a plugin that allows to use a document content stored in an external file.
    There is already a snippet doing this :
    http://modxcms.com/forums/index.php/topic,32507.msg197851.html#msg197851

    The plugin has the advantage that more automation would be possible.

    I think that the OnWebPagePrerenderer is the associated event to use.

    Can someone help me finishing this plugin ?
    Here is the code :
    //Plugin to load a document from the file system.
    $e = & $modx->Event;
    
    switch($e->name) {
    
    	case 'OnWebPagePrerender':
    
    		// echo '<div style="background:yellow;z-index:100;">';
    		
    		$currentId = $modx->documentIdentifier;
    		$contenu=$modx->getTemplateVar('content', "name", $currentId );
    		$contenu = $contenu['value']; // extract value from returned table
    		
    		if( substr($contenu,0,4) == "@DOC") {
    			$docPath = substr($contenu,4);
    			$fileToLoad = $modx->config['base_path'] ."assets/site/".trim($docPath); 
    			$toInsert = file_get_contents($fileToLoad);
    			
    			echo $toInsert; // Just to see the content of the file.
    			
    			// Here, the content of the remote document is stored in the $toInsert variable.
    			// For the frontend, we need to replace the "@DOC the_document_to_load.html"
    			// instruction stored in the [*content*] field by the content of the $toInsert variable.
    			
    			// There are several variables like modx->documentContent and modx->documentGenerated.
    			// They seem containing the full page, including the template content.
    			// I also don't know if there are read-only or if they can be edited,
    			// and in this case which one to use.
    		}
    		
    		// echo '</div>';
    		
    }