We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Has anyone created a way to have the default media source change depending on the template of the document being edited? So for instance: An Articles blog post's content area would take on one directory as the media source while a static content page takes on another? Example:

    Blog Post Template: assets/content/posts/
    or
    Static Content Template: assets/content/pages/

    I know there is a way to dynamically generate resource dependent media sources from the MIGX tutorial on the Multi-File Uploader (doesn't seem to work anymore for me though), but I have no idea how to do the same based on a resource's template. It would be even better if it combined the functions of both, so not only would the base path change but it would create a folder for just that individual resource. Example:

    Blog Post Template: assets/content/posts/{id}/
    or
    Static Content Template: assets/content/pages/{id}/

    This way I could assign the same media source to the template variables AND as the default and have the TV's and content area draw their media from the same place for each. Really, this would save me tons of time training my family in using the company website...
      Ben Morrison
      Hacking templates to pieces since 2003
    • You can use MODX tags in the media source path and URL, so I would think something like
      /assets/images/[[*template]]/

      would do the trick.

      As far as automatically generating the directory, I would think a plugin using OnTemplateFormSave (or whatever that one is actually called) would do the job; check if /assets/images/templateID/ exists and is writable, and create it if not.

      Be careful with Static Resources, while you can use a Media Source to access the file manager and upload/select a file, the resource itself doesn't recognize media sources, so all you'll get in the path is the part within the media source path.

      I work around this by using a chunk with the Media Source path, and prepend this to the Static Resource's path. You can do this with another plugin if you don't want your users to have to remember to prepend the chunk.
        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
        • 4172
        • 5,888 Posts
        The magic happens within this snippet:
        https://github.com/Bruno17/MIGX/blob/master/core/components/migx/elements/snippets/migxresourcemediapath.snippet.php

        I think, you need just to add one line, or another logic, which switches between pages and posts:

        $path = str_replace('{template}', $resource->get('template'), $path);


        and use it like that:

        [[migxResourceMediaPath? &pathTpl=`assets/content/{template}/{id}/`]]



          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
        • That bit of php would return the template number correct? How would I return the name of the template rather than the number?
            Ben Morrison
            Hacking templates to pieces since 2003
          • Ok so I found this little bit of code from another post, but I don't know php and how to modify it for use in this context:

            <?php
            
            $templateName = $modx->db->getValue( $modx->db->select( 'templatename', $modx->getFullTableName('site_templates'), 'id='.$modx->documentObject['template'] ));
            
            ?>
              Ben Morrison
              Hacking templates to pieces since 2003
              • 4172
              • 5,888 Posts
              This is code for EVO

              you can try:

              if ($template = $resource->getOne('Template'){
                  $path = str_replace('{templatename}', $template->get('templatename'), $path);
              }

                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
              • I tried this but when I tried to use the media source the I get "The specified directory is not a directory..." it seems {templatename} is not being replaced at all.

                    if (strstr($path, '{breadcrumb}')) {
                        $breadcrumbpath = '';
                        for ($i = 1; $i <= $breadcrumbdepth; $i++) {
                            $breadcrumbpath .= $parentids[$i] . '/';
                        }
                        $path = str_replace('{breadcrumb}', $breadcrumbpath, $path);
                
                    } else {
                        $path = str_replace('{id}', $docid, $path);
                        $path = str_replace('{pagetitle}', $resource->get('pagetitle'), $path);
                        $path = str_replace('{alias}', $resource->get('alias'), $path);
                	$path = str_replace('{templatename}', $template->get('templatename'), $path);
                        $path = str_replace('{parent}', $resource->get('parent'), $path);
                        $path = str_replace('{ultimateparent}', $ultimateParent, $path);
                    }
                [ed. note: maroonlover last edited this post 10 years, 2 months ago.]
                  Ben Morrison
                  Hacking templates to pieces since 2003
                  • 4172
                  • 5,888 Posts
                  you didn't get the template-object with:

                  if ($template = $resource->getOne('Template'){
                      $path = str_replace('{templatename}', $template->get('templatename'), $path);
                  }
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                  • I tried with and without sad
                      Ben Morrison
                      Hacking templates to pieces since 2003
                    •     } else {
                              $path = str_replace('{id}', $docid, $path);
                              $path = str_replace('{pagetitle}', $resource->get('pagetitle'), $path);
                              $path = str_replace('{alias}', $resource->get('alias'), $path);
                      		if ($template = $resource->getOne('Template'){
                      		$path = str_replace('{templatename}', $template->get('templatename'), $path);
                      		}
                              $path = str_replace('{parent}', $resource->get('parent'), $path);
                              $path = str_replace('{ultimateparent}', $ultimateParent, $path);
                          }
                        Ben Morrison
                        Hacking templates to pieces since 2003