├── jsonexpand ├── twigextensions │ └── JsonExpandTwigExtension.php ├── controllers │ └── JsonExpand_SectionsController.php ├── JsonExpandPlugin.php └── services │ └── JsonExpandService.php └── Readme.md /jsonexpand/twigextensions/JsonExpandTwigExtension.php: -------------------------------------------------------------------------------- 1 | new \Twig_Filter_Method($this, 'jsonExpandFilter') 13 | ); 14 | } 15 | 16 | public function jsonExpandFilter($content) { 17 | 18 | $expandedContent = craft()->jsonExpand->getJson($content); 19 | 20 | return JsonHelper::encode($expandedContent); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # JSON Expand Plugin for CraftCMS 2 | 3 | A simple Craft CMS twig extension that returns your object or array of objects as JSON 4 | 5 | ## Usage 6 | 7 | install the plugin and it will create a new twig filter and an action url 8 | 9 | ### Twig Filter 10 | 11 | you can use this in any template like so 12 | 13 | {% set events = craft.entries.section('events').find() %} 14 | {{ events | json_expand | raw }} 15 | 16 | ### Action Url 17 | 18 | you can also call up all the elements in a section directly by using the action url 19 | 20 | /actions/jsonExpand/sections/query?sectionId=4 21 | 22 | where the sectionId the id of the section whose entries you want to grab. 23 | 24 | Hope to add more filtering options to the action urls shortly. 25 | 26 | ## TODO 27 | 28 | * Add more filtering by action url 29 | * Add support for more fieldtypes 30 | -------------------------------------------------------------------------------- /jsonexpand/controllers/JsonExpand_SectionsController.php: -------------------------------------------------------------------------------- 1 | request->getSegments(5); 10 | 11 | $startDate = craft()->request->getParam('startDate', null); 12 | 13 | $criteria = craft()->elements->getCriteria(ElementType::Entry); 14 | $criteria->sectionId = $sectionId; 15 | 16 | if ($startDate != null) { 17 | $criteria->startDate > date("U"); 18 | } 19 | 20 | $criteria->place = 'Partnership for After School Education'; 21 | 22 | $entries = $criteria->find(); 23 | 24 | //$var = 'this is how it works: '.$section; 25 | 26 | $json = craft()->jsonExpand->getJson($entries); 27 | 28 | //return var; 29 | $this->returnJson($json); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jsonexpand/JsonExpandPlugin.php: -------------------------------------------------------------------------------- 1 | log->removeRoute('WebLogRoute'); 47 | craft()->log->removeRoute('ProfileLogRoute'); 48 | parent::init(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /jsonexpand/services/JsonExpandService.php: -------------------------------------------------------------------------------- 1 | getEntryJson($entry); 14 | } 15 | 16 | } elseif (is_object($content)) { 17 | 18 | $json = $this->getEntryJson($content); 19 | 20 | } else { 21 | 22 | $json = null; 23 | 24 | } 25 | 26 | return $json; 27 | } 28 | 29 | 30 | private function getEntryJson($entry) { 31 | 32 | $entryData = array(); 33 | 34 | 35 | $entryData = $entry->getAttributes(); 36 | 37 | $entryData['title'] = $entry->title; 38 | 39 | 40 | 41 | 42 | foreach ($entry->getType()->getFieldLayout()->getFields() as $field) { 43 | 44 | $field = $field->getField(); 45 | $handle = $field->handle; 46 | $value = $entry->$handle; 47 | 48 | if ($value instanceof \ArrayObject) { 49 | $value = array_merge((array)$value); 50 | } 51 | 52 | 53 | 54 | // gets all the default attributes from the element 55 | 56 | // check if its a relational field type 57 | if ( $field['type'] == 'Categories' || $field['type'] == 'Users' || $field['type'] == 'Assets' || $field['type'] == 'Entries' || $field['type'] == 'Matrix' ) { 58 | 59 | // for each related element 60 | foreach ($value as $relatedElement) { 61 | 62 | $relatedArray = array(); 63 | 64 | // gets all the default attributes from the element 65 | $relatedArray = $relatedElement->getAttributes(); 66 | 67 | $relatedArray['title'] = $relatedElement->title; 68 | 69 | // setup switch for fieldtype specific further customizations 70 | switch ($field['type']) { 71 | case 'Assets': 72 | $relatedArray['url'] = $relatedElement->url; 73 | 74 | $thumbTransform = array('mode'=>'fit', 'width'=>'100'); 75 | $relatedArray['thumbnail'] = $relatedElement->setTransform($thumbTransform)->url; 76 | break; 77 | 78 | case 'Matrix': 79 | $relatedArray['type'] = $relatedElement->type; 80 | break; 81 | 82 | case 'Matrix': 83 | $relatedArray['type'] = $relatedElement->type; 84 | break; 85 | 86 | 87 | 88 | } 89 | 90 | // get all the custom fields 91 | foreach ($relatedElement->getFieldLayout()->getFields() as $subField) { 92 | 93 | $subField = $subField->getField(); 94 | $subHandle = $subField->handle; 95 | $subValue = $relatedElement->$subHandle; 96 | 97 | if ($subValue instanceof \ArrayObject) { 98 | $subValue = array_merge((array)$subValue); 99 | } 100 | 101 | $relatedArray[$subHandle] = $subValue; 102 | 103 | } 104 | // add the item to the fields array 105 | $entryData[$handle][] = $relatedArray; 106 | } 107 | 108 | 109 | 110 | 111 | } else { 112 | // just set the field value to the field 113 | 114 | 115 | $entryData[$handle] = $value; 116 | 117 | 118 | 119 | //echo $field['type']; 120 | } 121 | } 122 | 123 | return $entryData; 124 | 125 | } 126 | 127 | 128 | } 129 | --------------------------------------------------------------------------------