/** * Returns the requested elements as JSON * * @param callable|null $configFactory A function for generating the config * @param array|null $config The API endpoint configuration * * @throws Exception * @throws HttpException */ public function actionGetElements($configFactory = null, array $config = null) { if ($configFactory !== null) { $params = craft()->urlManager->getRouteParams(); $variables = isset($params['variables']) ? $params['variables'] : null; $config = $this->_callWithParams($configFactory, $variables); } // Merge in default config options $config = array_merge(['paginate' => true, 'pageParam' => 'page', 'elementsPerPage' => 100, 'first' => false, 'transformer' => 'Craft\\ElementApi_ElementTransformer'], craft()->config->get('defaults', 'elementapi'), $config); if ($config['pageParam'] == 'p') { throw new Exception('The pageParam setting cannot be set to "p" because that’s the parameter Craft uses to check the requested path.'); } if (!isset($config['elementType'])) { throw new Exception('Element API configs must specify the elementType.'); } /** @var ElementCriteriaModel $criteria */ $criteria = craft()->elements->getCriteria($config['elementType'], ['limit' => null]); if (!empty($config['criteria'])) { $criteria->setAttributes($config['criteria']); } // Load Fractal $pluginPath = craft()->path->getPluginsPath() . 'elementapi/'; require $pluginPath . 'vendor/autoload.php'; $fractal = new Manager(); $fractal->setSerializer(new ArraySerializer()); // Define the transformer if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract) { $transformer = $config['transformer']; } else { Craft::import('plugins.elementapi.ElementApi_ElementTransformer'); $transformer = Craft::createComponent($config['transformer']); } if ($config['first']) { $element = $criteria->first(); if (!$element) { throw new HttpException(404); } $resource = new Item($element, $transformer); } else { if ($config['paginate']) { // Create the paginator require $pluginPath . 'ElementApi_PaginatorAdapter.php'; $paginator = new ElementApi_PaginatorAdapter($config['elementsPerPage'], $criteria->total(), $config['pageParam']); // Fetch this page's elements $criteria->offset = $config['elementsPerPage'] * ($paginator->getCurrentPage() - 1); $criteria->limit = $config['elementsPerPage']; $elements = $criteria->find(); $paginator->setCount(count($elements)); $resource = new Collection($elements, $transformer); $resource->setPaginator($paginator); } else { $resource = new Collection($criteria, $transformer); } } JsonHelper::sendJsonHeaders(); echo $fractal->createData($resource)->toJson(); // End the request craft()->end(); }
/** * Return JSON * * @param array $var */ private function returnJson($var = array()) { JsonHelper::sendJsonHeaders(); // Output it into a buffer, in case TasksService wants to close the connection prematurely ob_start(); echo JsonHelper::encode($var); craft()->end(); }