You are here

public function NodeTooltip::process in Glossify 8

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

modules/glossify_node/src/Plugin/Filter/NodeTooltip.php, line 115

Class

NodeTooltip
Filter to find and process found taxonomy terms in the fields value.

Namespace

Drupal\glossify_node\Plugin\Filter

Code

public function process($text, $langcode) {

  // Get node types.
  $node_types = explode(';', $this->settings['glossify_node_bundles']);
  if (count($node_types)) {
    $terms = [];

    // Get node data.
    $query = \Drupal::database()
      ->select('node_field_data', 'nfd');
    $query
      ->addfield('nfd', 'nid', 'id');
    $query
      ->addfield('nfd', 'title', 'name');
    $query
      ->addfield('nfd', 'title', 'name_norm');
    $query
      ->addField('nb', 'body_value', 'tip');
    $query
      ->join('node__body', 'nb', 'nb.entity_id = nfd.nid');
    $query
      ->condition('nfd.type', $node_types, 'IN');
    $query
      ->condition('nfd.status', 1);
    $query
      ->condition('nfd.langcode', $langcode);
    $query
      ->orderBy('name_norm', 'DESC');
    $results = $query
      ->execute()
      ->fetchAllAssoc('name_norm');

    // Build terms array.
    foreach ($results as $result) {

      // Make name_norm lowercase, it seems not possible in PDO query?
      if (!$this->settings['glossify_node_case_sensitivity']) {
        $result->name_norm = strtolower($result->name_norm);
      }
      $terms[$result->name_norm] = $result;
    }

    // Process text.
    if (count($terms) > 0) {
      $text = $this
        ->parseTooltipMatch($text, $terms, $this->settings['glossify_node_case_sensitivity'], $this->settings['glossify_node_first_only'], $this->settings['glossify_node_type'], $this->settings['glossify_node_urlpattern']);
    }
  }
  return new FilterProcessResult($text);
}