We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6726
    • 7,075 Posts
    I have been meaning to dig into this for a while, but never had the opportunity so far... now I have a website with a LOT of different templates but I still have to make it manageable for editors. Put some order into this...

    The current way it’s done in MODx seems to be based on the template id (correct me if I am wrong), and handled in manager/actions/mutate_settings_dynamic.php from line 310 to 337 (Correction, see below it was in mutate_content.dynamic) :


       <tr>
                <td nowrap class="warning" valign="top"><b><?php echo $_lang["defaulttemplate_title"] ?></b></td>
                <td>
    			<?php
    				$sql = "select templatename, id from $dbase.".$table_prefix."site_templates";
    				$rs = mysql_query($sql);
    			?>
    			  <select name="default_template" class="inputBox" onchange='documentDirty=true;' style="width:150px">
    				<?php
    				while ($row = mysql_fetch_assoc($rs)) {
    					$selectedtext = $row['id']==$default_template ? "selected='selected'" : "" ;
    					if ($selectedtext) {
    						$oldTmpId = $row['id'];
    						$oldTmpName = $row['templatename'];
    					}
    				?>
    					<option value="<?php echo $row['id']; ?>" <?php echo $selectedtext; ?>><?php echo $row['templatename']; ?></option>
    				<?php
    				}
    				?>
     			 </select>
     			 	<br />
     			 	<br />
    				<input onchange="documentDirty=true;" type="radio" name="reset_template" value="1" /> <?php echo $_lang["template_reset_all"]; ?><br />
    				<input onchange="documentDirty=true;" type="radio" name="reset_template" value="2" /> <?php echo sprintf($_lang["template_reset_specific"],$oldTmpName); ?>
    				<input type="hidden" name="old_template" value="<?php echo $oldTmpId; ?>" />
    			</td>
              </tr>
    
    


    Now, I’d like to avoid editing the template’s id through phpMyAdmin, since it would also mean editing all template variables and I also have a lot of them...

    My question is, how do I modify the core code to have the template select sorted by alphabetical order ?

    Thanks !
      .: COO - Commerce Guys - Community Driven Innovation :.


      MODx est l&#39;outil id
      • 11975
      • 2,542 Posts
      Hi david,

      you should add  an order by clause to the sql statement

      $sql = "select templatename, id from $dbase.".$table_prefix."site_templates ORDER BY templatename ASC";

      should do the trick

      :-)
        Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
        • 6726
        • 7,075 Posts
        Nice thanks I’ll try that out smiley

        Edit : doesn’t work sad
        Maybe mutate_settings_dynamic.php is not the right file huh

        Edit 2 : Looking into it, there seems to be other code involved in mutate_content.dynamic.php, line 335 :

        function templateWarning(){
            var dropTemplate = document.getElementById('template');
            if (dropTemplate){ 
            	for (var i=0; i<dropTemplate.length; i++){
                	if (dropTemplate[i].selected){
                    	newTemplate = dropTemplate[i].value;
                    	break;
                    }
        


        Anyone from the core team has a clue there ?
          .: COO - Commerce Guys - Community Driven Innovation :.


          MODx est l&#39;outil id
          • 6726
          • 7,075 Posts
          Ok, I was wrong to begin with, it seems...

          Found the right part of the code to edit in mutate_content.dynamic.php from line 613 (MODx 0.9.5 RC3 rev 2017) :

          <?php
          
          $sql = "select templatename, id from $dbase." . $table_prefix . "site_templates";
                          $rs = mysql_query($sql);
                      ?>
                      <select id="template" name="template" class="inputBox" onchange='templateWarning();' style="width:300px">
                          <option value="0">(blank)</option>
                      <?php
          
                      while ($row = mysql_fetch_assoc($rs)) {
              if (isset ($_REQUEST['newtemplate'])) {
                  $selectedtext = $row['id'] == $_REQUEST['newtemplate'] ? "selected='selected'" : "";
              } else
                  if (isset ($content['template'])) {
                      $selectedtext = $row['id'] == $content['template'] ? "selected='selected'" : "";
                          } else {
                      $selectedtext = $row['id'] == $default_template ? "selected='selected'" : "";
                          }
                      ?>


          I edited the sql as you instructed and this time around, it works !
          :D

          I actually think I am going to submit this into FlySpray Done, it makes more sense to have templates sorted by name rather than ID.
          I never had to worry about this before but when you have lots of templates, it’s easier for editors to choose the right one upon editing or creating a document provided you choose a clear and logical naming convention for your templates.

          Another thing that I think we should consider : have permissions assigned to templates, that would allow not to display templates which shouldn’t be used by the editors).

          Anyway, I’ll soon (hopefully) be able to do this kind of stuff on my own, my PHP/MySQL training is starting december 11th grin
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l&#39;outil id
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I was letting my mind wander late last night, and had a thought. What if documents could be assigned to categories, and then templates and TVs could be assigned by category.

            For example, say I have a catalog. I need to have several TVs assigned to the category items. Now these items reside under a "Catalog" document, which displays these items using Ditto. I want the category items to have a different template than the rest of the site, mostly because I don’t want those TVs cluttering up the document editing pages of all of my documents, only the catalog item documents. As it is, this means I have to make sure to change the template of each item as I create it. Or I could put them in an unpublished document set to use the items template and use the "inherit template" plugin. In either case, it means taking extra steps to get the desired outcome. And even if you do want your items to use the same template, you have to make a different one anyway even if it’s just a duplicate of your main template just to deal with the TVs.

            Now if documents were assigned to categories, and templates and TVs were assigned by category, you could assign your items documents to the "catalog items" category, and assign your TVs to that category, as well as a different template if you so desired. But even if you wanted your items to use the same template, the TVs would only show up for editing in your "catalog item" documents.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 6726
              • 7,075 Posts
              That would be great Susan, real powerful !

              It really would solve many things indeed, but I am wondering if this will happen before 1.0, which will more probably than not have a solution for this... Maybe even earlier with intermediary step stones, who knows...
                .: COO - Commerce Guys - Community Driven Innovation :.


                MODx est l&#39;outil id