MODX Community

Rest API and link tags

Hi,
a client want to use is current modx site as an headless CMS, front will be a Node/Express site.
I set up the Rest Api and have it working but I’m not sure how to deal with the internal links tags [[~ that are in almost every Modx documents content field.
How can I parse those tags before serving the data in the Json feed of the Rest Api?

Thanks for the help

1 Like

Try this:

$doc = $modx->getObject('modResource', array('pagetitle' => 'somePage'));

if ($doc) {
    $output = $doc->process();
}
1 Like

Thanks bob but that process the all page.

Just want the the content fields of the resource.

Tried $doc->content->process() but that brakes everything.

To make my problem clear here is my read function for my Rest:

 public function read($id) {
        if (empty($id)) {
            return $this->failure($this->modx->lexicon('rest.err_field_ns',array(
                'field' => $this->primaryKeyField,
            )));
        }
        /** @var xPDOObject $object */
        $c = $this->getPrimaryKeyCriteria($id);
        $this->object = $this->modx->getObject($this->classKey,$c);
        if (empty($this->object)) {
            return $this->failure($this->modx->lexicon('rest.err_obj_nf',array(
                'class_key' => $this->classKey,
            )));
        }
        $objectArray = $this->object->toArray();

        $afterRead = $this->afterRead($objectArray);
        if ($afterRead !== true && $afterRead !== null) {
            return $this->failure($afterRead === false ? $this->errorMessage : $afterRead);
        }
  

        $data['id'] = $this->object->id;
        $data['pagetitle'] = $this->object->pagetitle;
        $data['parent'] = $this->object->parent;
        $data['content'] = $this->object->content;
        $data['img'] = $this->object->getTVValue('blog_image_home');

        return $this->success('',$data);
	}

Is there a way to parse the link tags in $this->object->content;

1 Like
$parser = $this->modx->getParser();
$content = $this->object->content; // tho I personally prefer $this->object->get('content');
// parse all cacheable tags first 
$parser->processElementTags('', $content, true, false, '[[', ']]', array(), $maxIterations);
// parse all non-cacheable and remove unprocessed tags 
$parser->processElementTags('', $content, true, true, '[[', ']]', array(), $maxIterations);    
1 Like

Works great!

Thanks a lot for your help!

1 Like