We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36435
    • 40 Posts
    Hi guys.
    I'm developing a site with a blog using Articles, and the main image in each article is added through a TV.
    I've also got an image slider on the home page that currently has the images added manually. I'd like to set it up so that when a new article is added, if it has an image, then that image should show on the slider.
    I have no idea how to even start framing that piece of code... could someone please point me in the right direction? I haven't been able to find anything in the forums...
    Please help?

    Thanks

    Jo
      • 3749
      • 24,544 Posts
      There might be an easier way, but I think a plugin could detect an image in the article and write the image path to a TV on the Home page. I'm not sure what the event would be. There may be an event invoked when Articles saves an article. If not, you'd have to connect it to OnDocFormSave or OnSaveDocument and detect whether it's coming from Articles by looking at the parent IDs.


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36435
        • 40 Posts
        Thanks for your response BobRay.
        I guess I was being a bit too optimistic in hoping it would be just a couple of lines of code - my thinking was along the lines of getResources, or a combination using the Latest Posts functionality of Articles, but I don't know how to target individual elements of the post, like the tv.image and intro of each post. If this idea is plausible, any chance on some help with the syntax?

        Thanks
        Jo
          • 3749
          • 24,544 Posts
          I'm afraid I'm not familiar with Articles at all. Do you know how it handles the images? Are the images embedded in the content of the post or are they in a TV?


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 36435
            • 40 Posts
            They're in a TV. I read in a post somewhere (I can't find it now) that you can target the articles using a getResources call, since Articles is built on the same principle.
            That's the direction I've been trying for anyway.

            There will be images embedde in the content, however, the ones I'm targeting for this will be in the TV.

            Thanks

            Jo
              • 4172
              • 5,888 Posts
              you just need a getResources-call to get them.
              example:

              [[getResources?
              &parents=`XXX`
              &tpl=`slideChunk`
              &showHidden=`1`
              &includeTVs=`1`
              &tvPrefix=`` 
              ]]


              XXX is the ID of your articles-container

              you willl need &showHidden=`1` because articles normaly has hidemenu = 1 and would not be shown by default

              for detailed infos of getResources-properties see:
              http://rtfm.modx.com/display/ADDON/getResources [ed. note: Bruno17 last edited this post 14 years, 5 months ago.]
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 36435
                • 40 Posts
                Thanks Bruno. I'll give this a shot and do some reading as well.. I'll let you guys know how it goes.

                Thanks

                Jo
                  • 3749
                  • 24,544 Posts
                  OK, that should make it somewhat easier. If all articles had an image, you could use getResources, but it sounds like that's not the case so you'd have to roll your own. I'm making some guesses here (e.g., that the TV contains a path to the image) -- something like this (untested):

                  Put this tag in where the image path goes:

                  [[!getArticleImage]]



                  Then create a snippet called getArticleImage:

                  <?php
                  /* getArticleImage snippet */
                  
                  $c = $modx->newQuery('modResource');
                  $c->where(array(
                      'hidemenu' => '1', 
                      'published' => '1'
                  ));
                  $c->sortby('publishedon','DESC');
                  $c->limit(10);
                  
                  $resources = $modx->getCollection('modResource', $c);
                  
                  foreach($resources as $resource) {
                      $imagePath = $resource->getTVValue('name of image TV here');
                      if (!empty($imagePath)) {
                           return $imagePath;
                      }
                  
                  }
                  
                  return 'path_to_some_default_image';
                  


                  This code (if it works) will check the 10 most recent articles for an image and return the first one found. It will also pull non-article resources that are published and hidden from menus, but there shouldn't be very many of them and they won't have the image TV filled so it will skip them. You can change the limit() number to search more or fewer articles.


                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 4172
                    • 5,888 Posts
                    @Bob

                    I think you don't want to return here in the loop:

                    foreach($resources as $resource) {
                        $imagePath = $resource->getTVValue('name of image TV here');
                        if (!empty($imagePath)) {
                             return $imagePath;
                        }
                     
                    }
                    


                    it would also be possible to let Bobs - snippet return a commaseparated list of resourceids and give them to &resources - property of getResources or call getResources internally in the snippet.
                    [ed. note: Bruno17 last edited this post 14 years, 5 months ago.]
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 36435
                      • 40 Posts
                      Hi guys.
                      OK - I've been working on the getResources method (I've only just seen Bob's snippet method) and there's some light at the end of the tunnel.
                      Here's the call:
                      [[getResources?
                      	&parents=`2`
                      	&tpl=`slider-img-tpl`
                      	&showHidden=`1`
                      	&includeTVs=`1`
                      	&limit=`5`
                      	&sortdir=`ASC`  
                      	&tvPrefix=`` 
                      ]]


                      and here's the template chunk:

                      <!-- Carousel items -->
                        <div class="carousel-inner">
                          <div class="item">
                            <img src="[[+tv.artImage]]" alt="[[+pagetitle]]" />
                            <div class="carousel-caption">
                              <p class="hidden-phone">[[+introtext]]</p>
                            </div>
                          </div>
                        </div>


                      When i run it, it picks up everything except the image... It's driving me nuts. I've tried using [[*artImage]] as the TV call, but it still gives the same results:

                      Here's the output:

                      <!-- Carousel items -->
                        <div class="carousel-inner">
                          <div class="item">
                            <img src="" alt="Article 1" />
                            <div class="carousel-caption">
                              <p class="hidden-phone">this is an intro to an article
                      Quisque adipiscing, erat quis eleifend facilisis, nisi elit elementum turpis, non imperdiet libero augue non magna. Duis at diam ac lectus tempus pellentesque id vitae lorem.</p>
                            </div>
                          </div>
                        </div>
                      
                      
                      <!-- Carousel items -->
                        <div class="carousel-inner">
                          <div class="item">
                            <img src="" alt="Secont Test Article" />
                            <div class="carousel-caption">
                              <p class="hidden-phone">Secont Test Article</p>
                            </div>
                          </div>
                        </div>


                      What on Earth could I be missing??

                      Thanks guys.

                      Jo
                      BTW: here's the call to get the image in the Article post that works fine:
                      <div class="blog_entry_image">
                      	  <img src="[[*artImage:phpthumbof=`w=770&h=300&zc=1`]]" />
                      	</div>
                      [ed. note: justimaging last edited this post 14 years, 5 months ago.]