Skip to content

Commit

Permalink
feat(SLB-480): added the coffee module as a POC for how a UI could lo…
Browse files Browse the repository at this point in the history
…ok like
  • Loading branch information
chindris committed Nov 4, 2024
1 parent 77036aa commit 70e0bb9
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 4 deletions.
4 changes: 4 additions & 0 deletions apps/cms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"amazeelabs/silverback_translations": "^1.0.4",
"composer/installers": "^2.2",
"drupal/admin_toolbar": "^3.4.1",
"drupal/coffee": "^1.4",
"drupal/config_filter": "*",
"drupal/config_ignore": "^3.2",
"drupal/config_notify": "^1.10",
Expand Down Expand Up @@ -111,6 +112,9 @@
},
"drupal/graphql": {
"Check if translation exists when loading an entity by its uuid": "./patches/graphql_load_by_uuid_translation_check.patch"
},
"drupal/coffee": {
"On the fly ajax search POC": "./patches/contrib/coffee/on_the_fly_ajax_search_poc.patch"
}
},
"patchLevel": {
Expand Down
72 changes: 71 additions & 1 deletion apps/cms/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions apps/cms/config/sync/coffee.configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_core:
default_config_hash: cOqVnz_7pbb_0R31nQwih4Gh8XkmIbyFagFp6uUPaOc
coffee_menus:
admin: admin
max_results: 7
1 change: 1 addition & 0 deletions apps/cms/config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module:
block: 0
block_content: 0
breakpoint: 0
coffee: 0
config: 0
config_ignore: 0
config_notify: 0
Expand Down
9 changes: 6 additions & 3 deletions apps/cms/config/sync/views.view.global_search.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ display:
multi_type: separator
multi_separator: ', '
pager:
type: mini
type: full
options:
offset: 0
items_per_page: 10
Expand All @@ -135,6 +135,8 @@ display:
tags:
next: ››
previous: ‹‹
first: '« First'
last: 'Last »'
expose:
items_per_page: false
items_per_page_label: 'Items per page'
Expand All @@ -143,6 +145,7 @@ display:
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
quantity: 9
exposed_form:
type: basic
options:
Expand Down Expand Up @@ -241,8 +244,8 @@ display:
tags:
- 'config:search_api.index.global_search'
- 'search_api_list:global_search'
page_1:
id: page_1
search:
id: search
display_title: Page
display_plugin: page
position: 1
Expand Down
39 changes: 39 additions & 0 deletions apps/cms/patches/contrib/coffee/on_the_fly_ajax_search_poc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
diff --git a/js/coffee.js b/js/coffee.js
index 23c4e09..f4ee07a 100644
--- a/js/coffee.js
+++ b/js/coffee.js
@@ -13,10 +13,31 @@
var initSource = proto._initSource;

function filter(array, term) {
- var matcher = new RegExp($.ui.autocomplete.escapeRegex(term), 'i');
- return $.grep(array, function (value) {
- return matcher.test(value.command) || matcher.test(value.label) || matcher.test(value.value);
+ var matcher = new RegExp($.ui.autocomplete.escapeRegex(term.startsWith('#') ? term.substring(1) : term), 'i');
+ // By default, we do an ajax request to get the available options using a
+ // search feature implemented on the server. However, if the command starts
+ // with '#', then juts use the default behavior to search through the array
+ // parameter, which contains all the static entries.
+ if (term.startsWith('#')) {
+ return $.grep(array, function (value) {
+ return matcher.test(value.command) || matcher.test(value.label) || matcher.test(value.value);
+ })
+ }
+
+ var results;
+ $.ajax({
+ url: Drupal.url('global-search?search=' + encodeURIComponent(term)),
+ dataType: 'json',
+ // We have to wait for the request to complete before returning the
+ // results.
+ async: false,
+ success: function (data) {
+ results = $.grep(data, function (value) {
+ return matcher.test(value.command) || matcher.test(value.label) || matcher.test(value.value);
+ });
+ }
});
+ return results;
}

$.extend(proto, {
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
search_api_global.global-search:
path: '/global-search'
defaults:
_controller: 'Drupal\search_api_global\Controller\GlobalSearchCoffeeController::search'
requirements:
_permission: 'access coffee'
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Drupal\search_api_global\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\views\Views;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class GlobalSearchCoffeeController extends ControllerBase {

public function search(Request $request) {
$commands = [];
$search = $request->query->get('search');
// More advanced example to include view results.
if ($view = Views::getView('global_search')) {
$view->setDisplay('search');
if (!empty($search)) {
$view->setExposedInput(['search_api_fulltext' => $search]);
}
$view->preExecute();
$view->execute();

foreach ($view->result as $row) {
$entity = $row->_entity;
$url = '';
try {
$url = $entity->toUrl()->toString();
} catch (UndefinedLinkTemplateException|EntityMalformedException $e) {
// Just do nothing here, in case we could not build a URL we will just
// show an empty value there.
}
$commands[] = [
'value' => $url,
'label' => $entity->label() . ' (' . $entity->getEntityType()->getLabel() . ')',
'command' => $entity->label(),
];
}
}
return new JsonResponse($commands);
}
}

0 comments on commit 70e0bb9

Please sign in to comment.