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

    Does anyone else experience this behaviour with Articles?

    publish an article, which gives is a permalink of /blog/alias
    edit and re-save the article and the permalink changes to /blog/year/month/day/alias

    edit: There are a number of ways to publish an article. The behaviour exhibits itself when you write a draft blog, then click one of the publish buttons. It doesn't seem to be a problem if you publish through the dropdown menu and save.


    This behaviour obviously breaks your notification tinyURLs and any other inbound links.
    I posted a bug http://tracker.modx.com/issues/7313 about this, but reading through this forum I was surprised no-one else had mentioned it. I'm now wondering if this behaviour is that common, or if I have some combination of system settings that may trigger this.

    This happens on both my local development setup, running MAMP and my hosting account setup.

    I'm going to try and debug it today, but this shouldn't happen - am I right?

    Thanks
    [ed. note: robinK last edited this post 12 years, 2 months ago.]
      • 25284
      • 141 Posts
      Ok, so debugging it:

      In the article.class.php ArticleUpdateProcessor class, beforeSave function

      public function beforeSave() {
              $afterSave = parent::beforeSave();
              if ($this->object->get('published')) {
                  if (!$this->setArchiveUri()) {
                      $this->modx->log(modX::LOG_LEVEL_ERROR,'Failed to set date URI.');
                  }
              }
      
             ..........
      
              return $afterSave;
          }
      


      There is a bit of code that resets the URI to the date format (setArchivedUri) if you are saving a published article.
      I don't get it. Why would you want to alter the URI of something that has been published already?

      Or more to the point, why isn't this URI being set when I publish the article in the first place?

      any ideas?
        • 25284
        • 141 Posts
        ok, so looking into the code further...

        I can (sort of) make my blog post have the correct archive uri when it is published by hacking around in the ArticlesPlugin
        case 'OnDocPublished':
                /** @var Article $resource */
                $resource =& $scriptProperties['resource'];
                if ($resource instanceof Article) {
        	    $resource->setArchiveUri();
                    $resource->save();
                    $resource->notifyUpdateServices();
                    $resource->sendNotifications();
                }
                break;
        


        This takes care of the case where you click 'publish' from the articles container or the top button bar on an actual article.

        To handle the case where you select the Publish option from the dropdown menu, I had to alter the both the beforeSave functions in ArticleCreateProcessor

        public function beforeSave() {
                $beforeSave = parent::beforeSave();
        
                if (!$this->parentResource) {
                    $this->parentResource = $this->object->getOne('Parent');
                    if ($this->parentResource) {
                        $this->object->set('articles_container',$this->parentResource->get('id'));
                    }
                }
                
        //        if ($this->object->get('published') || $this->isPublishing) {
        //            if (!$this->setArchiveUri()) {
        //                $this->modx->log(modX::LOG_LEVEL_ERROR,'Failed to set URI for new Article.');
        //            }
        //        }
        //        
                /** @var ArticlesContainer $container */
                $container = $this->modx->getObject('ArticlesContainer',$this->object->get('parent'));
                if ($container) {
                    $settings = $container->get('articles_container_settings');
                    $this->object->set('articles_container_settings',$settings);
                    $this->object->set('richtext',!isset($settings['articlesRichtext']) || !empty($settings['articlesRichtext']));
                }
        
                $this->isPublishing = $this->object->isDirty('published') && $this->object->get('published');
                if ($this->object->get('published') || $this->isPublishing) {
                    if (!$this->setArchiveUri()) {
                        $this->modx->log(modX::LOG_LEVEL_ERROR,'Failed to set URI for new Article.');
                    }
                }
                return $beforeSave;
            }
        


        and ArticleUpdateProcessor

            public function beforeSave() {
                $afterSave = parent::beforeSave();
        //        if ($this->object->get('published') || $this->isPublishing) {
        //            if (!$this->setArchiveUri()) {
        //                $this->modx->log(modX::LOG_LEVEL_ERROR,'Failed to set URI for new Article.');
        //            }
        //        }
        //                
        
                /** @var ArticlesContainer $container */
                $container = $this->modx->getObject('ArticlesContainer',$this->object->get('articles_container'));
                if ($container) {
                    $this->object->set('articles_container_settings',$container->get('articles_container_settings'));
                }
        
                $this->isPublishing = $this->object->isDirty('published') && $this->object->get('published');
                if ($this->object->get('published') || $this->isPublishing) {
                    if (!$this->setArchiveUri()) {
                        $this->modx->log(modX::LOG_LEVEL_ERROR,'Failed to set date URI.');
                    }
                }
                return $afterSave;
            }
        
        


        All I'm doing here is also checking to see if the isPublishing flag is true, in which case call the setArchiveUri method.

        Now, the sendNotifications & notifyUpdateServices both use the same code
        $url = $this->xpdo->makeUrl($this->get('id'),$this->get('context_key'),'','full');
        


        in my case, this will always return '' for an article created/published & saved in one go, or /blog/alias for a previously saved article. There doesn't seem to be anything in the code to take into account the archive Uri that has been forced on this blog post.

        Now, I'm very aware that I don't fully understand the Articles code yet, so the above is intended as a write up of my analysis.

        Please don't use it - it still doesn't work and I may be totally off on how I'm trying to fix it.