search_excerpt example

'#theme' => 'username',
        '#account' => $node->getOwner(),
      ];

      $result = [
        'link' => $node->toUrl('canonical', ['absolute' => TRUE])->toString(),
        'type' => $type->label(),
        'title' => $node->label(),
        'node' => $node,
        'extra' => $extra,
        'score' => $item->calculated_score,
        'snippet' => search_excerpt($keys$rendered$item->langcode),
        'langcode' => $node->language()->getId(),
      ];

      $this->addCacheableDependency($node);

      // We have to separately add the node owner's cache tags because search       // module doesn't use the rendering system, it does its own rendering       // without taking cacheability metadata into account. So we have to do it       // explicitly here.       $this->addCacheableDependency($node->getOwner());

      
if (!isset($languages[$langcode])) {
          $languages[$langcode] = $this->languageManager->getLanguage($item->langcode);
        }
        $topic = $plugins[$section_plugin_id]->renderTopicForSearch($item->topic_id, $languages[$langcode]);
        if ($topic) {
          if (isset($topic['cacheable_metadata'])) {
            $this->addCacheableDependency($topic['cacheable_metadata']);
          }
          $results[] = [
            'title' => $topic['title'],
            'link' => $topic['url']->toString(),
            'snippet' => search_excerpt($keys$topic['title'] . ' ' . $topic['text']$item->langcode),
            'langcode' => $item->langcode,
          ];
        }
      }
    }

    return $results;
  }

  /** * {@inheritdoc} */

  protected function doSearchExcerpt($keys$render_array$langcode = NULL) {
    $render_array = search_excerpt($keys$render_array$langcode);
    $text = \Drupal::service('renderer')->renderPlain($render_array);
    // The search_excerpt() function adds some extra spaces -- not     // important for HTML formatting or this test. Remove these for comparison.     return preg_replace('| +|', ' ', $text);
  }

}
Home | Imprint | This part of the site doesn't use cookies.