scanner-8.x-1.0-rc3/scanner.module

scanner.module
<?php

/**
 * @file
 * Primary hook implementations for the Scanner module.
 */

use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Entity\Query\Sql\Query;

/**
 * Implements hook_theme().
 */
function scanner_theme($existing, $type, $theme, $path): array {
  return [
    'scanner_results' => [
      'variables' => [
        'data' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_query_TAG_alter() for 'scanner_search_regexp_like'.
 */
function scanner_query_scanner_search_regexp_like_alter(AlterableInterface $query): void {
  if (!$query instanceof SelectInterface) {
    return;
  }
  scanner_add_regexp_like_condition($query);
}

/**
 * Adds a WHERE condition using the REGEXP_LIKE() function.
 *
 * @param \Drupal\Core\Database\Query\SelectInterface $query
 *   The query to modify.
 */
function scanner_add_regexp_like_condition(SelectInterface $query): void {
  try {
    [
      'entity_type_id' => $entity_type_id,
      'fieldname' => $fieldname,
      'langcode' => $langcode,
      'mode' => $mode,
      'pattern' => $pattern,
    ] = $query->getMetaData('scanner_search_regexp_like');

    $entity_query = \Drupal::entityQuery($entity_type_id);
    // It will only work with an SQL entity query.
    if (!$entity_query instanceof Query) {
      return;
    }

    // Use the entity query helper to add the field to $query.
    $tables = $entity_query->getTables($query);
    $field = $tables->addField($fieldname, 'INNER', $langcode);

    $connection = \Drupal::database();
    // Escape the field name on Drupal 9+.
    // https://www.drupal.org/node/2986894
    $field = $connection->escapeField($field);

    // Add the conditional expression.
    $query->where("REGEXP_LIKE($field, :pattern, :match_type)", [
      ':match_type' => $mode ? 'c' : 'i',
      ':pattern' => $pattern,
    ]);
  }
  catch (QueryException $e) {

  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc