Skip to content
Snippets Groups Projects

Issue #3292906 by yahyaalhamad: Meta should include information about the view

1 file
+ 50
2
Compare changes
  • Side-by-side
  • Inline
@@ -10,6 +10,8 @@ use Drupal\jsonapi\JsonApiResource\Link;
use Drupal\jsonapi\JsonApiResource\LinkCollection;
use Drupal\jsonapi\ResourceResponse;
use Drupal\jsonapi_resources\Resource\EntityResourceBase;
use Drupal\views\Plugin\views\row\RowPluginBase;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
@@ -139,6 +141,51 @@ final class ViewsResource extends EntityResourceBase {
return $view->preview($display_id, $this->getViewArguments());
}
/**
* Gets the meta options.
*
* @param \Drupal\views\ViewExecutable $view
* The view.
*
* @return array
* The meta options.
*/
protected function getMetaOptions(ViewExecutable $view) {
$options = [];
// Get general options.
$options['title'] = $view->getTitle();
// Get pager options.
if($pager = $view->getPager()) {
$options['pager'] = [
'id' => $pager->getPluginId(),
'count' => $pager->getTotalItems(),
'configurations' => $pager->options,
];
}
// Get the view style.
$style = $view->getDisplay()->getPlugin('style');
if ($style instanceof StylePluginBase) {
$options['style'] = [
'id' => $style->getPluginId(),
'configurations' => $style->options,
];
}
// Get the view row.
$row = $view->getDisplay()->getPlugin('row');
if ($row instanceof RowPluginBase) {
$options['row'] = [
'id' => $row->getPluginId(),
'configurations' => $row->options,
];
}
return $options;
}
/**
* Process the resource request.
*
@@ -193,9 +240,10 @@ final class ViewsResource extends EntityResourceBase {
return $row->_entity;
}, $view->result);
$data = $this->createCollectionDataFromEntities($entities);
list($pagination_links, $total_count) = $this->getViewsPager($view);
[$pagination_links, $total_count] = $this->getViewsPager($view);
$meta_options = ['count' => $total_count] + $this->getMetaOptions($view);
$response = $this->createJsonapiResponse($data, $this->request, 200, [], $pagination_links, ['count' => $total_count]);
$response = $this->createJsonapiResponse($data, $this->request, 200, [], $pagination_links, $meta_options);
if (isset($bubbleable_metadata)) {
$bubbleable_metadata->addCacheContexts([
'url.query_args:page',
Loading