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

    I am sure that this is documented somewhere, or at least that someone has it. I would like to have the ability to choose template when creating news (using modified newspublisher) . I would just like a simple single choice dropdown menu that sets the template variable .

    I use templates to change the menues according to which department the user is at the moment..

    however, is the template change possible?
      • 6221
      • 36 Posts
      noone?
      I really need this, and I don’t wanna reinvent the wheel again laugh
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You can specify the template to use in the NewsPublisher snippet, along with the folder for the new document, but that’s as far as it goes. You could hack the snippet to offer a dropdown.
          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
          • 10487 MODX Staff
          • 1,535 Posts
          And here is the hack grin Got bored over lunch so decided to have a look at this ... modded snippet is attached to this post.

          The main change is when retrieving the template values, I’ve used the code below to generate the template dropdown (if no value is selected, for whatever reason, then the site default template will be used):
          // get template
          $templateSQL = $modx->db->select('*',$modx->getFullTableName('site_templates'));
          if ($modx->db->getRecordCount($templateSQL) > 0 ) {
              $templateDropdown = '<select name="template">';
              while ($row = $modx->db->getRow($templateSQL)) {
                  $templateDropdown .= '<option value="'.$row['id'].'">'.$row['templatename'].'</option>';
              }
              $templateDropdown .= '</select>';
              $modx->setPlaceholder('templateDropdown',$templateDropdown);
          }
          
          if (isset($_POST['template'])) {
              $template = $_POST['template'];
          } else {
               $template = $modx->config['default_template'];
          }
          


          And, then I modified the form template to include the template field ... and voila!

          Hope that helps,
          Garry
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Excellent! Only one thing... why waste resources retrieving everything from the table when all you want are the ID and the name of the template? No need to load up the template code itself.

            $templateSQL = $modx->db->select('id, templatename',$modx->getFullTableName('site_templates'));
              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
              • 10487 MODX Staff
              • 1,535 Posts
              Well spotted, Susan! It’s what happens when I try and code and stuff my face at the same time laugh
                Garry Nutting
                Senior Developer
                MODX, LLC

                Email: [email protected]
                Twitter: @garryn
                Web: modx.com
                • 6221
                • 36 Posts
                You guys (and girls: ) are GREAT!!!!!! grin
                This is EXACTLY what I want!!

                Thanks alot for helping me!!!

                /Michael