umami_search_web_components-1.0.x-dev/umami_search_web_components.install
umami_search_web_components.install
<?php
/**
* @file
* Primary module hooks for umami_search_web_components module.
*/
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\search_api\Entity\Index;
use Drupal\user\Entity\Role;
/**
* Implements hook_install().
*/
function umami_search_web_components_install() {
// Remove the default search block.
\Drupal::entityTypeManager()->getStorage('block')->load('umami_search')?->delete();
// Uninstall core's search.
\Drupal::service('module_installer')->uninstall(['search']);
Role::load('anonymous')->grantPermission('use search with content endpoint')->save();
// Create the search node.
/** @var \Drupal\node\NodeInterface $node */
$node = \Drupal::entityTypeManager()->getStorage('node')->create([
'title' => 'Search',
'type' => 'page',
'uid' => 1,
'moderation_state' => 'published',
'langcode' => 'en',
'path' => [['alias' => '/search']],
]);
$node->setPublished();
$node->save();
$section = new Section('search_web_components_twocol', [
'endpoint' => 'content',
'column_widths' => '20-80',
'url' => '/api/search/content',
'defaultPerPage' => 5,
'updateUrl' => TRUE,
'enableDialog' => TRUE,
'dialogBreakpoint' => '750',
'dialogCloseText' => 'Close',
'dialogClosePosition' => 'top',
'dialogModal' => TRUE,
]);
$firstComponents = [
[
'region' => 'firstFirst',
'config' => [
'id' => 'swc_search_result_summary',
'label' => 'Search Component: Result Summary',
'label_display' => FALSE,
'summaryText' => '@start to @end of @total',
],
],
[
'region' => 'firstSecond',
'config' => [
'id' => 'swc_search_dialog_toggle',
'label' => 'Search Component: Dialog Toggle',
'label_display' => FALSE,
'aboveBreakpointText' => 'Filters',
'dialogOpenText' => 'Filters',
'dialogCloseText' => 'Close',
'showAppliedCount' => TRUE,
],
],
[
'region' => 'firstThird',
'config' => [
'id' => 'swc_facet_button',
'label' => 'Search Component: Author Button Facet',
'label_display' => FALSE,
'provider' => 'search_web_components',
'endpoint' => 'content',
'facet' => 'author',
'key' => NULL,
'preferAttributes' => NULL,
'overrideLabel' => NULL,
'showCount' => NULL,
'showLabel' => NULL,
'showReset' => NULL,
'resetText' => NULL,
'collapsible' => NULL,
'closed' => NULL,
'showCountInCollapseLabel' => NULL,
],
],
[
'region' => 'firstThird',
'config' => [
'id' => 'swc_facet_checkbox',
'label' => 'Search Component: Recipe Category Checkbox Facet',
'label_display' => FALSE,
'provider' => 'search_web_components',
'endpoint' => 'content',
'facet' => 'recipe_category',
'key' => NULL,
'preferAttributes' => NULL,
'overrideLabel' => NULL,
'showCount' => NULL,
'showLabel' => NULL,
'showReset' => NULL,
'resetText' => NULL,
'collapsible' => NULL,
'closed' => NULL,
'showCountInCollapseLabel' => NULL,
'useRadios' => NULL,
],
],
[
'region' => 'firstThird',
'config' => [
'id' => 'swc_facet_dropdown',
'label' => 'Search Component: Recipe Category Dropdown Facet',
'label_display' => FALSE,
'endpoint' => 'manual_entry',
'facet' => NULL,
'key' => 'recipe_category',
'preferAttributes' => NULL,
'overrideLabel' => '',
'showCount' => TRUE,
'showLabel' => TRUE,
'showReset' => FALSE,
'resetText' => 'Reset (@count)',
'collapsible' => FALSE,
'closed' => TRUE,
'showCountInCollapseLabel' => TRUE,
'selectLabel' => 'Select a category',
],
],
[
'region' => 'firstThird',
'config' => [
'id' => 'swc_facet_checkbox',
'label' => 'Search Component: Tags Checkbox Facet',
'label_display' => FALSE,
'endpoint' => 'content',
'facet' => 'tags',
'key' => NULL,
'preferAttributes' => NULL,
'overrideLabel' => NULL,
'showCount' => NULL,
'showLabel' => NULL,
'showReset' => NULL,
'resetText' => NULL,
'collapsible' => NULL,
'closed' => NULL,
'showCountInCollapseLabel' => NULL,
'useRadios' => NULL,
],
],
];
$secondComponents = [
[
'region' => 'secondFirst',
'config' => [
'id' => 'swc_search_input',
'label' => 'Search Component: Search Input',
'label_display' => FALSE,
'provider' => 'search_web_components',
'labelText' => 'Search',
'placeHolderText' => 'Search',
'clearText' => 'Remove',
],
],
[
'region' => 'secondSecond',
'config' => [
'id' => 'swc_search_applied_facets',
'label' => 'Search Component: Applied Facets',
'label_display' => FALSE,
'provider' => 'search_web_components',
'removeText' => 'Remove @value',
'resetText' => 'Reset',
'showReset' => TRUE,
'showIndividual' => TRUE,
],
],
[
'region' => 'secondSecond',
'config' => [
'id' => 'swc_search_results_per_page',
'label' => 'Search Component: Results per page',
'label_display' => FALSE,
'provider' => 'search_web_components',
'type' => 'select',
'labelText' => 'Per page',
'options' => NULL,
'htmlSelectLabel' => '',
],
],
[
'region' => 'secondSecond',
'config' => [
'id' => 'swc_search_sort',
'label' => 'Search Component: Sort',
'label_display' => FALSE,
'provider' => 'search_web_components',
'type' => 'select',
'labelText' => 'Sort by',
'sorts' => NULL,
'htmlSelectLabel' => '',
],
],
[
'region' => 'secondThird',
'uuid' => '66bddb82-ae54-4c1d-82a2-8522f35dfaac',
'config' => [
'id' => 'swc_search_results',
'label' => 'Search Component: Results',
'label_display' => FALSE,
'provider' => 'search_web_components',
],
],
[
'region' => 'secondThird',
'config' => [
'id' => 'swc_search_no_results_message',
'label' => 'Search Component: No Results Message',
'label_display' => FALSE,
'provider' => 'search_web_components',
'noResultsContent' => [
'value' => '<h2>No results found.</h2><p><strong>Search Help</strong></p><ul><li>Check your search for typos.</li><li>Use more generic search terms.</li><li>Need help finding something? Call <a href="#">(555) 555-5555</a> or <a href="#">Email Us</a></li></ul>',
'format' => 'full_html',
],
],
],
[
'region' => 'secondFourth',
'config' => [
'id' => 'swc_search_simple_pager',
'label' => 'Search Component: Simple Pager',
'label_display' => FALSE,
'provider' => 'search_web_components',
'pagesToDisplay' => 1,
'showNextPrev' => TRUE,
'prevLabel' => '<',
'nextLabel' => '>',
'showFirstLast' => FALSE,
'firstLabel' => 'First page',
'lastLabel' => 'Last page',
],
],
];
foreach ($firstComponents as $fc) {
$c = new SectionComponent($fc['uuid'] ?? \Drupal::service('uuid')->generate(), $fc['region'], $fc['config']);
$section->appendComponent($c);
}
foreach ($secondComponents as $sc) {
$cc = new SectionComponent($sc['uuid'] ?? \Drupal::service('uuid')->generate(), $sc['region'], $sc['config']);
$section->appendComponent($cc);
}
$node->set('layout_builder__layout', $section);
$node->save();
Index::load('content')->indexItems();
drupal_flush_all_caches();
}
