Skip to content

Commit 6d53c7c

Browse files
committed
Adding Search form
1 parent 0f4335c commit 6d53c7c

File tree

5 files changed

+95
-21
lines changed

5 files changed

+95
-21
lines changed

app/modules/custom/linux_package_viewer/linux_package_viewer.routing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ linux_package_viewer.search:
33
defaults:
44
_form: '\Drupal\linux_package_viewer\Form\SearchForm'
55
_title: 'Search'
6-
methods: ['GET']
6+
methods: ['GET', 'POST']
77
requirements:
88
_permission: 'view linux packages'
99
options:

app/modules/custom/linux_package_viewer/src/Form/SearchForm.php

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,81 @@ public function getFormId() {
4343
}
4444

4545
public function submitForm(array &$form, FormStateInterface $form_state) {
46-
46+
$form_state->setRebuild(false);
4747
}
4848

4949
public function buildForm(array $form, FormStateInterface $form_state) {
50-
$form['title'] = [
50+
51+
$definitions = $this->pluginManager->getDefinitions();
52+
$form['distribution'] = [
53+
'#type' => 'select',
54+
'#title' => $this->t('Distribution'),
55+
];
56+
57+
foreach($definitions as $id => $definition) {
58+
$form['distribution']['#options'][$id] = $this->t($definition['distribution']);
59+
}
60+
61+
$form['package'] = [
5162
'#type' => 'textfield',
52-
'#title' => $this->t('Search'),
63+
'#title' => $this->t('Package'),
5364
'#size' => 60,
5465
'#maxlength' => 128,
55-
'#required' => TRUE,
5666
];
5767

58-
$form['save'] = array(
68+
$form['save'] = [
5969
'#type' => 'submit',
60-
'#value' => $this->t('Save'),
61-
);
70+
'#value' => $this->t('Search'),
71+
'#ajax' => [
72+
'callback' => '::searchPackages',
73+
'wrapper' => 'linux-package-viewer-search-results-wrapper',
74+
'event' => 'click',
75+
'progress' => [
76+
'type' => 'throbber',
77+
'message' => NULL,
78+
]
79+
]
80+
];
81+
82+
$form['results'] = [
83+
'#type' => 'container',
84+
'#prefix' => '<div id="linux-package-viewer-search-results-wrapper">',
85+
'#suffix' => '</div>',
86+
];
6287

6388
return $form;
6489
}
6590

91+
public function searchPackages($form, FormStateInterface $formState) {
92+
$package = trim($formState->getValue('package'));
93+
if ($package === '') { return $form['results']; }
94+
95+
$distribution = $formState->getValue('distribution');
96+
$instance = $this->pluginManager->createInstance($distribution);
97+
$instance->setPackage($package);
98+
$results = $instance->execute();
99+
100+
$ele = [
101+
'#type' => 'table',
102+
'#header' => [
103+
$this->t('Package'),
104+
$this->t('View'),
105+
],
106+
];
107+
108+
foreach($results as $i => $result){
109+
$ele[$i]['#attributes'] = [
110+
'class' => ['linux-package-viewer-search-result']
111+
];
112+
113+
$ele[$i]['package'] = [
114+
'#plain_text' => $this->t($result),
115+
];
116+
}
117+
118+
$ele['#prefix'] = '<div id="linux-package-viewer-search-results-wrapper">';
119+
$ele['#suffix'] = '</div>';
120+
return $ele;
121+
}
122+
66123
}

app/modules/custom/linux_package_viewer/src/Plugin/LinuxPackageViewer/DebianViewer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ class DebianViewer extends LinuxPackageViewerPluginBase implements ContainerFact
2222
public function executeRaw() {
2323
$package = $this->getPackage();
2424
if ($package === "") { return []; }
25-
2625
$url = $this->getSearchUrl();
27-
$results = $this->httpClient->get("${url}/${package}");
26+
27+
try {
28+
$results = $this->httpClient->get("${url}/${package}");
29+
} catch (\Exception $e) {
30+
return [];
31+
}
32+
2833
$body = $results->getBody();
2934
$decodedBody = json_decode($body);
3035
return $decodedBody;

app/modules/custom/linux_package_viewer/src/Plugin/LinuxPackageViewer/FedoraViewer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ public function executeRaw() {
3232
"start_row" => 0,
3333
];
3434
$searchString = json_encode($search);
35-
$results = $this->httpClient->get("${url}/${searchString}");
35+
36+
try {
37+
$results = $this->httpClient->get("${url}/${searchString}");
38+
} catch (\Exception $e) {
39+
return [];
40+
}
41+
3642
$body = $results->getBody();
3743
$decodedBody = json_decode($body);
3844
return $decodedBody;

app/modules/custom/linux_package_viewer/src/Plugin/LinuxPackageViewer/UbuntuViewer.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@ class UbuntuViewer extends LinuxPackageViewerPluginBase implements ContainerFact
2222
public function executeRaw() {
2323
$package = $this->getPackage();
2424
if ($package === "") { return []; }
25-
2625
$url = $this->getSearchUrl();
27-
$results = $this->httpClient->get($url, [
28-
"query" => [
29-
"ws.op" => "getPublishedSources",
30-
"exact_match" => "false",
31-
"source_name" => $package,
32-
"ws.size" => "200",
33-
"ordered" => "false"
34-
]
35-
]);
26+
27+
try {
28+
$results = $this->httpClient->get($url, [
29+
"query" => [
30+
"ws.op" => "getPublishedSources",
31+
"exact_match" => "false",
32+
"source_name" => $package,
33+
"ws.size" => "200",
34+
"ordered" => "false",
35+
"memo" => "200"
36+
]
37+
]);
38+
} catch (\Exception $e) {
39+
return [];
40+
}
41+
3642
$body = $results->getBody();
3743
$decodedBody = json_decode($body);
3844
return $decodedBody;

0 commit comments

Comments
 (0)