Skip to content

Commit b1f97ec

Browse files
authored
Merge pull request #1 from OS2web/rest_export_post_request
Rest export post request
2 parents 86961c5 + 77a0cb1 commit b1f97ec

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Drupal\os2web_rest_api\Plugin\views\display;
4+
5+
use Drupal\rest\Plugin\views\display\RestExport;
6+
use Drupal\views\Views;
7+
use Symfony\Component\Routing\RouteCollection;
8+
9+
/**
10+
* The plugin that handles Data response callbacks for REST resources.
11+
*
12+
* @ingroup views_display_plugins
13+
*
14+
* @ViewsDisplay(
15+
* id = "os2web_rest_export",
16+
* title = @Translation("OS2Web REST export, GET, POST"),
17+
* help = @Translation("Create a OS2Web REST export resource with ability GET, POST requests."),
18+
* uses_route = TRUE,
19+
* admin = @Translation("OS2Web REST export"),
20+
* returns_response = TRUE
21+
* )
22+
*/
23+
class Os2webRestExport extends RestExport {
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function collectRoutes(RouteCollection $collection) {
29+
parent::collectRoutes($collection);
30+
$view_id = $this->view->storage->id();
31+
$display_id = $this->display['id'];
32+
33+
if ($route = $collection->get("view.$view_id.$display_id")) {
34+
// OS2Web REST exports should only respond to GET and POST methods.
35+
$route->setMethods(['GET', 'POST']);
36+
}
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function displaysExposed() {
43+
return TRUE;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function usesExposed() {
50+
$this->has_exposed = TRUE;
51+
return $this->has_exposed;
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function defineOptions() {
58+
$options = parent::defineOptions();
59+
// Restore exposed form and blocks.
60+
$options['exposed_form'] = [
61+
'contains' => [
62+
'type' => ['default' => 'basic'],
63+
'options' => ['default' => []],
64+
],
65+
'merge_defaults' => [$this, 'mergePlugin'],
66+
];
67+
$options['exposed_block'] = ['default' => FALSE];
68+
return $options;
69+
}
70+
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function optionsSummary(&$categories, &$options) {
76+
parent::optionsSummary($categories, $options);
77+
78+
$categories['exposed'] = [
79+
'title' => $this->t('Exposed form'),
80+
'column' => 'third',
81+
'build' => [
82+
'#weight' => 1,
83+
],
84+
];
85+
if ($this->usesExposedFormInBlock()) {
86+
$options['exposed_block'] = [
87+
'category' => 'exposed',
88+
'title' => $this->t('Exposed form in block'),
89+
'value' => $this->getOption('exposed_block') ? $this->t('Yes') : $this->t('No'),
90+
'desc' => $this->t('Allow the exposed form to appear in a block instead of the view.'),
91+
];
92+
}
93+
94+
/** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form_plugin */
95+
$exposed_form_plugin = $this->getPlugin('exposed_form');
96+
if (!$exposed_form_plugin) {
97+
// Default to the no cache control plugin.
98+
$exposed_form_plugin = Views::pluginManager('exposed_form')->createInstance('basic');
99+
}
100+
101+
$exposed_form_str = $exposed_form_plugin->summaryTitle();
102+
103+
$options['exposed_form'] = [
104+
'category' => 'exposed',
105+
'title' => $this->t('Exposed form style'),
106+
'value' => $exposed_form_plugin->pluginTitle(),
107+
'setting' => $exposed_form_str,
108+
'desc' => $this->t('Select the kind of exposed filter to use.'),
109+
];
110+
111+
if ($exposed_form_plugin->usesOptions()) {
112+
$options['exposed_form']['links']['exposed_form_options'] = $this->t('Exposed form settings for this exposed form style.');
113+
}
114+
115+
}
116+
117+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Drupal\os2web_rest_api\Plugin\views\exposed_form;
4+
5+
use Drupal\views\Plugin\views\display\DisplayPluginBase;
6+
use Drupal\views\Plugin\views\exposed_form\Basic;
7+
use Drupal\views\ViewExecutable;
8+
9+
/**
10+
* Exposed form plugin that provides a basic exposed form.
11+
*
12+
* @ingroup views_exposed_form_plugins
13+
*
14+
* @ViewsExposedForm(
15+
* id = "os2web_basic",
16+
* title = @Translation("OS2Web Basic. GET, POST"),
17+
* help = @Translation("OS2Web Basic exposed form. GET, POST")
18+
* )
19+
*/
20+
class Os2webBasic extends Basic {
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
26+
parent::init($view, $display, $options);
27+
$exposed_input = array_merge(\Drupal::request()->request->all(), \Drupal::request()->query->all());
28+
$view->setExposedInput($exposed_input);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)