Community

TV in RESTFUL API

Hi there,

I would like to add a list of TVs as endpoints to the output of my simple API call, how would i go about doing this?

All i have at the moment is the code from the Modx example

<? class MyControllerItems extends modRestController {
    public $classKey = 'modResource';
    public $defaultSortField = 'id';
    public $defaultSortDirection = 'ASC';
} ?>

As an example one of my TVs is called: Ticker-text

Current output is: http://carillion.com/digitalsignage/items/1

Sorry if there is a simple solution to this, i just couldn’t get my head around it. :slight_smile:

Many thanks
Adam

Add this function

public function prepareListObject(xPDOObject $object){
    	$data = array();
    	$data['ticker-text'] = $object->getTVValue('Ticker-text');
    	return $data;
}

Also you may want to check this out:

1 Like

Thank you for your quick replay.

I have added the function, but still no luck on getting it to appear on the output.
Any ideas?

current code:

<? class MyControllerItems extends modRestController { public $classKey = 'modResource'; public $defaultSortField = 'id'; public $defaultSortDirection = 'ASC'; public function prepareListObject(xPDOObject $object){ $data = array(); $data['ticker-text'] = $object->getTVValue('Ticker-text'); return $data; } } ?>

current output: http://carillion.com/digitalsignage/items/1

Thanks again
Adam

Sorry looks like I’ve given you the wrong function method. The one I’ve given you is for the overview of all http://carillion.com/digitalsignage/items/. I can’t recall what the correct method is off hand and I’m away from my PC at the moment. The method will be in the modrestcontroller documentation though.

No problem at all, actually what you have gave me works perfect.

Although if you do have the other method that would be great.

Thank you.
Adam

I have managed to list the endpoints for specific IDs now.

However when trying to update using PUT the items don’t update?

Any ideas?

<? class MyControllerItems extends modRestController {

    public $classKey = 'modResource';
    public $defaultSortField = 'id';
    public $defaultSortDirection = 'ASC';



    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,
            )));
        }
		
	$tvID = 17;
	$tv = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvID, 'contentid' => $id));
		
		
    				
    $objectArray = Array();
    $objectArray ['id']  = $id;
    $objectArray ['pagetitle']  = $this->object->pagetitle;
    $objectArray ['alias']  = $this->object->alias;
    		
    			
    $objectArray['Background Image'] = $this->object->getTVValue(1);
    $objectArray['Case Study One'] = $this->object->getTVValue(2);
    $objectArray['Case Study two'] = $this->object->getTVValue(17);
    $objectArray['Case Study three'] = $this->object->getTVValue(18);
    $objectArray['Case Study four'] = $this->object->getTVValue(19);
    $objectArray['Whats happening Image'] = $this->object->getTVValue(3);
    $objectArray['Whats happening title'] = $this->object->getTVValue(5);
    $objectArray['Whats happening text'] = $this->object->getTVValue(4);
    $objectArray['Whats happening Image 2'] = $this->object->getTVValue(12);
    $objectArray['Whats happening title 2'] = $this->object->getTVValue(11);
    $objectArray['Whats happening text 2'] = $this->object->getTVValue(10);
    $objectArray['Whats happening Image 3'] = $this->object->getTVValue(15);
    $objectArray['Whats happening title 3'] = $this->object->getTVValue(13);
    $objectArray['Whats happening text 3'] = $this->object->getTVValue(14);
    $objectArray['Info screen Image'] = $this->object->getTVValue(16);
    $objectArray['Welcome Message'] = $this->object->getTVValue(20);
    		

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

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

       
    }

    ?>