We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10378
    • 375 Posts
    Ho can I retrieve and render values from the properties field of the site_content table (since MODx Revo 2.2.x) in frontend? I have a custom resource type which stores some values in the properties field and would like to read and render those values in frontend template.

    This is the content of the properties field:

    {"goodnews":{"mailingTemplate":"13","mailFrom":"[email protected]","mailFromName":"Company Name","editorGroups":"Administrator,Editor","unsubscribeResource":"[[~88]]","profileResource":"[[~194]]"}}
    


    For example I'd like to render mailFrom value in frontend template. But how?
    I thought it should is possible with a placeholder shortcut?

    This question has been answered by multiple community members. See the first response.

      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You'll need a snippet:
      <?php
      $page =  $modx->getObject('modResource', $modx->resource->get('id'));
      $view = 0;
      $view = $page->getProperty('mailingTemplate','mailFrom',$view);
      return $view;
      

      Not sure what the third argument is for here.
        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
        • 3749
        • 24,544 Posts
        The third argument is the default. The second argument, though, is 'namespace', It defaults to 'core', so sending 'mailFrom' there is going to confuse it.

        You can only get one property at a time with getProperty(), so I think this is the ticket:


        <?php
        $page =  $modx->getObject('modResource', $modx->resource->get('id'));
        $mailingTemplate = $page->getProperty('mailingTemplate');
        $mailFrom = $page->getProperty('mailFrom');
        


        or this:

        $page =  $modx->getObject('modResource', $modx->resource->get('id'));
        $props = $page->getProperties();
        $mailingTemplate = $props['mailingTemplate'];
        $mailFrom = $props->['mailFrom'];



        You could also do this to set them all as placeholders:

        $page =  $modx->getObject('modResource', $modx->resource->get('id'));
        $props = $page->getProperties();
        $modx->setPlacholders($props);



        [ed. note: BobRay last edited this post 13 years, 3 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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I took that code from the HitsPage snippet, names changed to fit the circumstance. I think I got the namespace/field reversed. The HitsPage snippet works great.
          {"hitspage":{"hitts":41}}

          $view = $page->getProperty('hitts','hitspage',$view);
          ...
          $page->setProperty('hitts',intval($view) + 1,'hitspage');

            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
          • discuss.answer
            • 3749
            • 24,544 Posts
            Yes, but hitspage creates those properties with their own namespace ('hitspage'). I don't think the method for getting them would work unless the properties were set with that namespace (as in your last example -- the third argument is the namespace).

            If you set them with setProperty('key', 'value'), they go in the core namespace and if you specify anything else in the second argument to getProperty(), I don't think they'll be found.

            My example should have used this, since it looks like Gadgetto has used the goodnews Namespace:


            $mailingTemplate = $page->getProperty('mailingTemplate', 'goodnews');
            $mailFrom = $page->getProperty('mailFrom', 'goodnews');


            $resource->getProperties('goodnews');


            I imagine that when you set element default properties in the Manager, they get the 'core' namespace, but I've never checked. [ed. note: BobRay last edited this post 13 years, 3 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
            • discuss.answer
              • 10378
              • 375 Posts
              Thank you all for jumping in!

              I solved this by overriding the process method of my custom resource type class! My solution sets all values from the properties field as custom placeholders so they can be accessed in frontend templates easily!

              public function process() {
                  $settings = $this->getSettings();
                  foreach ($settings as $key => $value) {
                      $this->xpdo->setPlaceholder($key, $value);
                  }
                  $this->_content = parent::process();
                  return $this->_content;
              }
              
              public function getSettings() {
                  $settings = $this->getProperties('goodnews');
                  if (!empty($settings)) {
                      $settings = is_array($settings) ? $settings : $this->xpdo->fromJSON($settings);
                  }
                  return !empty($settings) ? $settings : array();
              }
              
                Freelancer @bitego http://www.bitego.com
                ---
                GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                More infos here: http://www.bitego.com/extras/goodnews/