-
-
Save mooror/e52d8b8cc2c6a7f72e1d90ec573ddea1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class StoreItemAdmin extends ModelAdmin { | |
//...Truncated for bravity | |
public function getEditForm($id = null, $fields = null) { | |
$form = parent::getEditForm($id, $fields); | |
$apiGridFieldName = 'StoreItem'; | |
$apiGridField = $form->Fields()->fieldByName($apiGridFieldName); | |
// Call to the packagist Api | |
$curl = curl_init(); | |
curl_setopt($curl,CURLOPT_URL,'https://packagist.org/search.json?q=[silverstripe]'); | |
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); | |
$packagistPackages = curl_exec($curl); | |
if ($packagistPackages=== false){ | |
Debug::message("The API request to packagist Failed. Error String:"); | |
Debug::show(curl_error($curl)); | |
} | |
curl_close($curl); | |
// If Api call was successful and did not return ''. | |
if($packagistPackages != ''){ | |
// Decode the json response and turn it into an array | |
$shopItems = json_decode($packagistPackages,true); | |
$itemsObject = $shopItems["results"]; | |
$shopTestItemsObject = (object) $shopTestItems; | |
$itemsArrayList = ArrayList::create($itemsObject); | |
$apiGrid = new GridField('ExampleGrid', 'Example grid',StoreItemType::get()); | |
if ($apiGridField) { | |
$apiGridConfig = GridFieldConfig_RecordViewer::create(); | |
//$config->removeComponentsByType('GridFieldDeleteAction'); | |
$apiGridField->setConfig($apiGridConfig); | |
$apiGridDetailForm = $apiGridField->getConfig()->getComponentByType('GridFieldDetailForm'); | |
$apiGridDetailForm->setFields(new FieldList( | |
new TextField('name','Item Name') | |
)); | |
$apiGridDataColumns = $apiGridField->getConfig()->getComponentByType('GridFieldDataColumns'); | |
$apiGridDataColumns->setDisplayFields(array( | |
'name' => 'Name', | |
'description' => 'Description', | |
'repository' => 'Repository', | |
'downloads' => 'Downloads' | |
)); | |
$apiGridField->setModelClass('StoreItemType'); | |
$apiGridField->setList($itemsArrayList); | |
} | |
}else{ | |
Debug::message("The API request to packagist Failed"); | |
} | |
return $form; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment