We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37914
    • 89 Posts
    Where can I download the AliasID snippet, or is there an other way to retrieve an id from an alias?
    http://rtfm.modx.com/extras/revo/aliasid#download

    I need this so I can get the content form a resource.
    I can't use the ID because it wil change per context, but the alias will stay the same...

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      If I'm understanding you, it would take a custom snippet, but you shouldn't need the ID. If you know the alias and the context you want, you can to this:

      $alias='some alias';
      $context = 'some context';
      
      $resource = $modx->getObject('modResource', array('alias' => $alias, 'context_key' => $context));
      if ($resource) {
          return $resource->getContent();
      }


      If the alias is unique (i.e., not duplicated in other contexts), you can do this instead:

      $alias='some alias';
      
      $resource = $modx->getObject('modResource', array('alias' => $alias));
      if ($resource) {
          return $resource->getContent();
      }


      Note that you'll be getting the raw content field of the resource. Any MODX tags it contains won't be processed.


        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
        • 37914
        • 89 Posts
        Quote from: BobRay at May 15, 2015, 11:34 PM
        If I'm understanding you, it would take a custom snippet, but you shouldn't need the ID. If you know the alias and the context you want, you can to this:

        $alias='some alias';
        $context = 'some context';
        
        $resource = $modx->getObject('modResource', array('alias' => $alias, 'context_key' => $context));
        if ($resource) {
            return $resource->getContent();
        }


        If the alias is unique (i.e., not duplicated in other contexts), you can do this instead:

        $alias='some alias';
        
        $resource = $modx->getObject('modResource', array('alias' => $alias));
        if ($resource) {
            return $resource->getContent();
        }


        Note that you'll be getting the raw content field of the resource. Any MODX tags it contains won't be processed.



        Thanx again BobRay, this was exactly what I was looking for.

        I modified the code a bit ($modx->context->key;) so that it gets the alias from the current context, because I need this for a multi domain/context setup.

        <?php
        $alias='form-contact';
        $context = $modx->context->key;
        
        $resource = $modx->getObject('modResource', array('alias' => $alias, 'context_key' => $context));
        if ($resource) {
            return $resource->getContent();
        }

          • 13428 ☆ A M B ☆
          • 1,031 Posts
          Are context settings an option for this?

          Use [[++contact_id]] as placeholder and fill a setting 'contact_id' in each context with the resource id of the context contact page.

          And use CrossContextSettings to manage the settings in different contexts.