We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42399
    • 39 Posts
    Agel Nash created very useful plugin that allows to manage chunks and snippets with IDE only. The idea is to store all chunks and snippets in files. And MODX will load them automatically on start. If you create new file in chunks folder you can work with it immediately, there's no need to switch to manager and create new DB record there.
    Such way of development allows to save time and to use version control systems for chunks and snippets.
    <?php
    if (!defined('MODX_BASE_PATH')) { die('HACK???'); }
     
    /**
     * LoadElement
     *
     * Loading chunks and snippets from files
     *
     * @license     GNU General Public License (GPL), http://www.gnu.org/copyleft/gpl.html
     * @author      Agel_Nash <[email protected]>
     * @version     0.1
     * @internal    @events         OnWebPageInit, OnManagerPageInit
     * @internal    @properties     &extChunk=Chunks extensions (<i>comma separated</i>);input;txt,html &extSnippet=Snippets extensions (<i>comma separated</i>);input;php &pathElement=Elemets folder (<i>relative to site root</i>);input;assets/element/
     */
     
    class LoadElement{
        public static $pathElement = 'assets/element/';
        /**
         * Elements type validation
         *
         * @param string $element elements type
         * @return bool
         */
        protected static function validate($element){
            return is_dir(static::getPath($element));
        }
     
        /**
         * Elements folder path
         *
         * @param string $element elements type
         * @return string
         */
        protected static function getPath($element){
            return MODX_BASE_PATH . static::$pathElement . $element.'/';
        }
     
        /**
         * Get name of the method containing rules to load elements
         *
         * @param string $element тип элементов
         * @return string
         */
        protected static function getMethodName($element){
            return 'get'.ucfirst($element);
        }
     
        /**
         * Rules to load snippets
         *
         * @param DocumentParser $modx
         * @param SplFileInfo $item detected element
         * @return bool status of elements loading
         */
        protected static function getSnippet(DocumentParser $modx, SplFileInfo $item){
            $snippetName = $item->getBasename('.'.$item->getExtension());
            $modx->snippetCache[$snippetName] = "return require '".$item->getRealPath()."';";
            $modx->snippetCache[$snippetName . "Props"] = array();
            return true;
        }
     
        protected static function getChunk(DocumentParser $modx, SplFileInfo $item){
            $chunkName = $item->getBasename('.'.$item->getExtension());
            $modx->chunkCache[$chunkName] = file_get_contents($item->getRealPath());
            return true;
        }
        /**
         * Run elements creation
         *
         * @param DocumentParser $modx
         * @param string $element
         * @return bool
         */
        public static function run(DocumentParser $modx, $element, array $ext = array()){
            if( ! static::validate($element) ) return false;
     
            $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator(
                    static::getPath($element),
                    RecursiveDirectoryIterator::SKIP_DOTS
                ), RecursiveIteratorIterator::SELF_FIRST
            );
            foreach ($iterator as $item) {
                /**
                 * @var SplFileInfo $item
                 */
                if($item->isFile() && $item->isReadable() && (empty($ext) || in_array($item->getExtension(), $ext))){
                    $name = static::getMethodName($element);
                    static::$name($modx, $item);
                }
            }
            return true;
        }
    }
     
    LoadElement::$pathElement = (!empty($pathElement) && is_scalar($pathElement)) ? $pathElement : LoadElement::$pathElement;
     
    $extSnippet = (!empty($extSnippet) && is_scalar($extSnippet)) ? $extSnippet : 'txt,html';
    $extSnippet = array_map('trim', explode(",", $extSnippet));
    LoadElement::run($modx, 'snippet', $extSnippet);
     
    $extChunk = (!empty($extChunk) && is_scalar($extChunk)) ? $extChunk : 'txt,html';
    $extChunk = array_map('trim', explode(",", $extChunk));
    LoadElement::run($modx, 'chunk', $extChunk);
    


    Create folder for elements (default is assets/element/) and 2 subfolders there:

    • snippet - for snippets;
    • chunk - for chunks.

    After that try to create any file with .php extension in /assets/element/snippet/. The name of this file will be the name of the new snippet. The same with chunks (but file extension should be .txt or .html). It's worth to mention that elements are not created in manager - they are virtual, so you can disable them changing file extension or sort creating additional subfolders.

    Look at demo: http://www.youtube.com/watch?v=q_p2TDT0RKA&feature=player_embedded

    Original article in Russian: http://blog.agel-nash.ru/2014/2/ide-and-modx-evolution.html
      • 9995
      • 1,613 Posts
      Video stuck at 4.23 :-o
        Evolution user, I like the back-end speed and simplicity smiley
        • 13428 ☆ A M B ☆
        • 1,031 Posts
        Is the FileSource Plugin in Evo 1.0.12+ the same?
          • 612 ☆ A M B ☆
          • 9 Posts
          Quote from: Jako at Mar 06, 2014, 03:11 AM
          Is the FileSource Plugin in Evo 1.0.12+ the same?
          In FileSource need to create an entry in the database. And with this plugin, you need only file.
            Security Expert
            • 13428 ☆ A M B ☆
            • 1,031 Posts
            Nice idea, but it could be very frustating/confusing (if snippets/chunks are not there where they supposed to be – i.e. if someone different has to work on such an installation) and should be used only in own development.

            On my sites i call external chunks by @FILE binding (but the snippet has to support that).
              • 42399
              • 39 Posts
              Quote from: fourroses666 at Mar 06, 2014, 02:59 AM
              Video stuck at 4.23 :-o
              It's probably flash player issue, Chrome shows with html5 player till the end.