Skip to content

Commit

Permalink
Tweaks to the text search form
Browse files Browse the repository at this point in the history
  • Loading branch information
pingevt committed Jan 14, 2024
1 parent a283b92 commit 1c05c45
Showing 1 changed file with 73 additions and 40 deletions.
113 changes: 73 additions & 40 deletions src/Form/TextFieldSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#value' => $this->t('Search'),
];

$striper = [
'transparent' => '#eeeeee',
'#eeeeee' => 'transparent',
];
$current_stripe = "#eeeeee";

if (!empty($session_data)) {
$form['results'] = [
'#weight' => -1,
Expand All @@ -94,18 +100,31 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];

foreach ($session_data[1]['data'] as $entity_type => $data) {

$list = [
'#theme' => 'item_list',
'#title' => 'Results for: ' . $entity_type,
'#items' => [],
];
$current_stripe = "#eeeeee";

foreach ($data as $id => $result_data) {

$link = Link::fromTextAndUrl($result_data['label'], $result_data['url']);
$list['#items'][] = [
'#markup' => "Found " . $this->formatPlural($result_data['count'], "1 time", "@count times") . " on " . $link->toString(),
'#wrapper_attributes' => [
'style' => "padding: 1em;background-color:" . $current_stripe,
],
[
'#markup' => "<p>Found " . $this->formatPlural($result_data['count'], "1 time", "@count times") . " on " . $link->toString() . "</p>",
],
[
'#theme' => 'item_list',
'#title' => 'Fields:',
'#items' => $result_data['fields'],
],
];
$current_stripe = $striper[$current_stripe];
}

$form['results']['results'][] = $list;
Expand Down Expand Up @@ -152,16 +171,20 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
];

foreach ($fields as $field_type => $field_data) {
foreach ($field_data as $entity_type => $fields) {
$batch['operations'][] = [
[$this, 'searchTextFields'],
[
$field_type,
$entity_type,
$fields,
$values['search'],
],
];
foreach ($field_data as $entity_type => $fields2) {
foreach ($fields2 as $f => $fdata) {
$batch['operations'][] = [
[$this, 'searchTextFields'],
[
$field_type,
$entity_type,
[
$f => $fdata,
],
$values['search'],
],
];
}
}
}

Expand Down Expand Up @@ -199,7 +222,7 @@ public static function searchTextFields($field_type, $entity_type, $fields, $sea
$r = $query->execute();

if (!empty($r)) {
$context['results']['raw'][$entity_type][$field_type] = $r;
$context['results']['raw'][$entity_type][$field_type][$field_id] = $r;

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.1.x, 8.1, 10.6)

Variable $field_id might not be defined.

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.0.x, 8.1, 10.6)

Variable $field_id might not be defined.

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.1.x, 8.2, 10.6)

Variable $field_id might not be defined.

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.0.x, 8.2, 10.4)

Variable $field_id might not be defined.

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.1.x, 8.1, 10.4)

Variable $field_id might not be defined.

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.1.x, 8.2, 10.4)

Variable $field_id might not be defined.

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.0.x, 8.1, 10.4)

Variable $field_id might not be defined.

Check failure on line 225 in src/Form/TextFieldSearch.php

View workflow job for this annotation

GitHub Actions / test (10.0.x, 8.2, 10.6)

Variable $field_id might not be defined.
}
}

Expand All @@ -209,36 +232,42 @@ public static function searchTextFields($field_type, $entity_type, $fields, $sea
public static function processResults(&$context) {
if (isset($context['results']['raw']) && !empty($context['results']['raw'])) {
foreach ($context['results']['raw'] as $entity_type => $field_types) {
foreach ($field_types as $results) {

$method_name = "processResults_" . $entity_type;
if (method_exists(__CLASS__, $method_name)) {
TextFieldSearch::$method_name($results, $context);
}
else {

$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage->loadMultiple($results);

foreach ($entities as $entity) {
try {
if (in_array('canonical', $entity->uriRelationships())) {
$data = $context['results']['data']['paragraph'][$entity->id()] ?? [
'url' => $entity->toUrl(),
'label' => $entity->label(),
'count' => 0,
];
$data['count']++;

$context['results']['data'][$entity_type][$entity->id()] = $data;
foreach ($field_types as $fields) {
foreach ($fields as $field_id => $results) {
$method_name = "processResults_" . $entity_type;
if (method_exists(__CLASS__, $method_name)) {
TextFieldSearch::$method_name($results, $field_id, $context);
}
else {

$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage->loadMultiple($results);

foreach ($entities as $entity) {
try {
if (in_array('canonical', $entity->uriRelationships())) {
$data = $context['results']['data'][$entity_type][$entity->id()] ?? [
'url' => $entity->toUrl(),
'label' => $entity->label(),
'count' => 0,
'fields' => [],
];
$data['count']++;

if (!in_array($field_id, $data['fields'])) {
$data['fields'][] = $field_id;
}

$context['results']['data'][$entity_type][$entity->id()] = $data;
}
else {
$context['results']['errors'][] = "Cannot create link for " . $entity->label();
}
}
else {
$context['results']['errors'][] = "Cannot create link for " . $entity->label();
catch (\Throwable $e) {
$context['results']['errors'][] = $e->getMessage();
}
}
catch (\Throwable $e) {
$context['results']['errors'][] = $e->getMessage();
}
}
}
}
Expand All @@ -254,7 +283,7 @@ public static function processResults(&$context) {
* We have to seperate this out b/c we need to look for its parent entity to
* create a link to it.
*/
public static function processResults_paragraph($results, &$context) {
public static function processResults_paragraph($results, $field_id, &$context) {
$storage = \Drupal::entityTypeManager()->getStorage('paragraph');
$entities = $storage->loadMultiple($results);

Expand Down Expand Up @@ -283,8 +312,12 @@ public static function processResults_paragraph($results, &$context) {
'url' => $parent->toUrl(),
'label' => $parent->label(),
'count' => 0,
'fields' => [],
];
$data['count']++;
if (!in_array($field_id, $data['fields'])) {
$data['fields'][] = $field_id;
}

$context['results']['data']['paragraph'][$parent->id()] = $data;
}
Expand Down

0 comments on commit 1c05c45

Please sign in to comment.