We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41255
    • 19 Posts
    How can I update the content of a specific Weblink? I tried several things with runProcessor but it has no effect.

    For example:
    The content of a Weblink (with id 64) is `http://www.domain.com/link123.html`.
    I need to update the content with a value that is specified in a TV (`http://www.domain.com/link456.html`).


    A new Weblink is created by
    $weblink = $modx->newObject('modWeblink', $fields);
    $weblink->save();


    I can't find any documentation to update an existing Weblink.

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      Try this:

      $webLink = $modx->getObject('modWebLink', 64);
      if ($webLink) {
         $webLink->setContent('http://some/url');
         $webLink->save(); 
          
      } else {
         /* failed to retrieve Weblink */
      }


      If that doesn't work (it should) try this:

      $webLink->set('content, 'http://some/url');


      Remember that the code doesn't clear the cache, so you probably won't see the change until the cache is cleared unless you do this after saving the Weblink:

      $cm = $modx->getCacheManager();
      $cm->refresh();


      I'm not sure whether Weblinks are cached, but I suspect that they are.

      Note that this clears the entire site cache. You can clear the cache for the individual Weblink (assuming that it's cached), but the official method is quite complicated.

      If you can find the cache file for the Weblink and you use the default MODX cache system, you can just unlink() it with something like this:

      unlink(MODX_CORE_PATH . /cache/path/to/weblink/cache/file);


      FYI, if the URL you're setting is local (i.e., on your site), you should do something like this to make the code more portable:

      $webLink->setContent(MODX_SITE_URL . 'remaining/url/');


      [ed. note: BobRay last edited this post 9 years, 10 months ago.]
        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
        • 41255
        • 19 Posts
        Quote from: BobRay at Jul 01, 2016, 12:59 AM
        If that doesn't work (it should) try this:

        $webLink->set('content, 'http://some/url');


        Thank you Bob. The first option didn't work for me. But the second does exactly what I meant. smiley

        Is it also possibly to update more fields at once - with values from an array? The same way as at creation of a new Weblink.

        $weblink = $modx->newObject('modWebLink', $fields);
        $weblink->save();


        I use the following array at creation.
        $fields = array(
            'pagetitle' =>$pagetitle,
            'content' =>$clickout,
            'parent' =>'90',
            'published' =>'1',
            'alias' =>$alias,
            'unpub_date' =>$unpub_date,
            'searchable' =>'0'
        );


        I will try the caching part soon. [ed. note: BobRay last edited this post 9 years, 10 months ago.]
          • 41255
          • 19 Posts
          I've found the from array-method.

          $weblink->fromArray($fields);
          $weblink->save();
            • 3749
            • 24,544 Posts
            I'm glad that worked, though I'm surprised that setContent($value) didn't work. That's the "official" method. It's a method of the modResource class, which modWebLink is derived from and all it does is this:

            public function setContent($content, array $options = array()) {
                return $this->set('content', $content);
            }


            FYI, the approach below sometimes works with various MODX objects, but it often fails. It will work with some fields but not others, so it's a better practice to use set() or fromArray().

            $weblink = $modx->newObject('modWebLink', $fields);


            fromArray() takes a bunch of arguments, but in your case the defaults are perfect. If you didn't have a date field (which needs conversion), the rawValues option would make it a little faster, and in extremely rare cases (e.g., code to migrate an entire site to a different server), you'd want to set the primary keys.

            void fromArray(
               array $fldarray,
               [string $keyPrefix = ''],
               [boolean $setPrimaryKeys = false],
               [boolean $rawValues = false],
               [boolean $adhocValues = false]
            )
            [ed. note: BobRay last edited this post 9 years, 10 months ago.]
              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