Drupal 9 image field migration

The issue related to the custom website with custom field for phone. It has spaces. One of the solution is to remove spaces on presave and process the old entries in DB removing the spaces.
But sometimes it better not to touch thing that works.
The task was to provide search by phone filter in views. There are no default methods in views to search with removed spaces. In this case I've found a solution to alter the query.
So for example the phone fields value is "+354 566 32 56" and we need to find it by searching "6632" string.
This code will help you with it:

 * Implements hook_views_query_alter().
 * @param ViewExecutable $view
 * @param Sql $query
function custommodule_views_query_alter(ViewExecutable $view, Sql $query) {
  if ($view->id() == 'commerce_orders' && $view->current_display == 'orders') {
    // Traverse through the 'where' part of the query.
    foreach ($query->where as &$condition_group) {
      foreach ($condition_group['conditions'] as &$condition) {
        // Replace whitespaces from phone field.
        if ($condition['operator'] == 'formula' && !empty(strpos($condition['field'], 'field_phone_number'))) {
          $condition['field'] = preg_replace(['/CONCAT_WS/','/[)]/'], ['REPLACE(CONCAT_WS', "), ' ', '')"], $condition['field'] );

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
 dP   dP  dP   dP  dP     dP  dP    dP  .d88888b  
88 88 88 88 88 .d8' Y8. .8P 88. "'
88aaa88 88aaa88 88aaa8P' Y8aa8P `Y88888b.
88 88 88 `8b. d8' `8b `8b
88 88 88 88 88 88 d8' .8P
dP dP dP dP dP dP Y88888P

Enter the code depicted in ASCII art style.