On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I would like to write a plugin or snippet to:
    1. create 3 tabs for the top hierarchy documents (which are language docs: en, de, ru)
    2. Add those as tabs to each document edit.
    3. Result should be:

    General | Settings | EN | DE | RU

    4. We use the same alias for each language, so we can link the langauges directly to each other. E.g. en/news and ru/news

    How to achieve this goal in Modx Evolution?

    This question has been answered by breezer. See the first response.

    • Yes it is possible, you'll use the OnDocFormRender and OnDocFormSave events with a plugin. I'm not at my computer with all my sample code till late tonight but when I get a chance I'll post some code to get you started.
        xforum
        http://frsbuilders.net (under construction) forum for evolution
      • discuss.answer
        Heres a plugin demo which should help you get started. I usually put the code in a file and link to it in the plugin content, easier to edit in my opinion. Name your plugin whatever you want, and either link to the file location, or add the code below to the plugin content.

        // if including a file
        include MODX_BASE_PATH.'assets/plugins/tabs/tab.plugin.php';


        <?php
        /*  tabs.plugin.php
        
        */
        
        /*
        Copy / paste the following into the SQL tab in phpMyAdmin
        to create the sample table
        Change the {PREFIX} part to match your setup
        
        CREATE TABLE IF NOT EXISTS `{PREFIX}tabs_demo` (
          `id` int(10) NOT NULL auto_increment,
          `name` varchar(50) NOT NULL default '',
          `docid` integer NOT NULL DEFAULT '0' COMMENT '',
          `content` mediumtext,
          PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM COMMENT='';
        */
        
        // name of the table which holds your data, without any prefix
        $table_name ='tabs_demo';
        
        
        $tab_tpl ='
        <!-- tab create -->
                <div class="tab-page" id="tab[+tab_title+]">
        <!-- tab title -->
                    <h2 class="tab">[+tab_title+]</h2>
                    <script type="text/javascript">tpSettings.addTabPage( document.getElementById( "tab[+tab_title+]" ) );</script>
        <!-- tab content -->
                    <div class="sectionHeader" id="[+tab_title+]_header">[+tab_heading+]</div>
                    <div class="sectionBody">
                        [+tab_content+]
                   </div>
        <!-- end tab content -->
                </div>
        <!-- close tab -->
        ';
        
        
        $e = &$modx->Event;
        $output ='';
        switch ($e->name) {
        
            case 'OnDocFormRender':
                
                /*====== copy this entire block to add a new tab  ======*/
                // modify the settings below for each tab
                $item_name ='EXAMPLE';
                $tab_title ='Tab Title';
                $tab_heading ='Tab Heading';
                $tab_content ='
                    <div style="width:100%">
                        <textarea id="[+item_name+]" name="[+item_name+]" style="width:100%; height:200px;" onchange="documentDirty=true;">[+field_content+]</textarea>
                        <input type="hidden" name="[+item_name+]_NAME" value="[+hidden_item_name+]">
                        <input type="hidden" name="[+item_name+]_ID" value="[+hidden_item_id+]">
                    </div>';
                // end modify settings
        
                // we are editing an existing document
                if( $_REQUEST['a'] == '27' ){
        
                    // $id is populated with the current modx document id
                    $tab_details = $modx->db->getRow( $modx->db->select( '*', $modx->getFullTablename( $table_name ), 'docid="'.$id.'" AND name="'.$item_name.'"'));
        
                }
        
                $replace = array(
                        '[+tab_title+]'         => $tab_title,
                        '[+tab_heading+]'       => $tab_heading,
                        '[+tab_content+]'       => $tab_content,
                        '[+hidden_item_name+]'  => $item_name,
                        '[+hidden_item_id+]'    => $_REQUEST['a'] == '27' ? $tab_details['id'] : '',
                        '[+item_name+]'         => $item_name,
                        '[+field_content+]'     => $_REQUEST['a'] == '27' ? $modx->htmlspecialchars($tab_details['content']) : ''
                        );
        
                $output .=str_replace( array_keys( $replace ), array_values( $replace ), $tab_tpl );
        
                /*======  end tab block ======*/
        
        
                // send the tabs output
                echo $output;
                
            break;
            
            case 'OnDocFormSave':
                
                /*====== copy this block and modify for each of the form fields ======*/
                if( isset ( $_POST['EXAMPLE'] ) ){
        
                    $post_value = $modx->db->escape( trim( $_POST['EXAMPLE'] ) );
                    $post_id =$_POST['EXAMPLE_ID'];
                    $post_name =$_POST['EXAMPLE_NAME'];
        
                    $status ='';
                    if( $post_id !='' ){
        
                        // we are editing an existing item
                        $fields = array(
                            'content' => $post_value
                        );
                        $update = $modx->db->update( $fields, $modx->getFullTablename( $table_name ), 'docid="'.$id.'" AND id="'.$post_id.'"' );
                        $status .= $update ? 'update success' : 'update failed';
                        
                    }else{
        
                        // creating a new item
                        $fields = array(
                            //'id'      => '',
                            'name'    => $post_name,
                            'docid'   => $id,
                            'content' => $post_value
                        );
                        $insert = $modx->db->insert( $fields, $modx->getFullTablename( $table_name ) );
                        $status .= $insert ? 'insert success' : 'insert failed';
                    }
        
                    // log this event - Reports / System Events
                    // comment this out after testing
                    $modx->logEvent( 0, 1, 'status: '.$status.' - name: '.$post_name.' - id: '.$post_id.'<br>'.$post_value , 'Tabs plugin' );
                }
                /*====== end of copy block ======*/
                
            break;
        
            default: '';
            
            return;
        }
        ?>


        On the System Events tab check the following events:
        OnDocFormRender
        OnDocFormSave
          xforum
          http://frsbuilders.net (under construction) forum for evolution