You are here

public function Search::buildLink in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/freelinking/Search.php \Drupal\freelinking\Plugin\freelinking\Search::buildLink()

Build a link with the plugin.

Parameters

array $target: The target array with including the following keys:

  • text: The text to display in the URL.
  • indicator: The indicator string.
  • dest: The destination string for the plugin to turn into a URI.
  • tooltip: An optional tooltip.
  • language: A language object.

Return value

array Link array.

Overrides FreelinkingPluginInterface::buildLink

File

src/Plugin/freelinking/Search.php, line 93

Class

Search
Freelinking search plugin.

Namespace

Drupal\freelinking\Plugin\freelinking

Code

public function buildLink(array $target) {
  $failover = $this
    ->getConfiguration()['settings']['failover'];
  $searchString = str_replace(' ', '+', $target['dest']);
  if ($this->moduleHandler
    ->moduleExists('search')) {
    return [
      '#type' => 'link',
      '#title' => $target['text'] ? $target['text'] : $this
        ->t('Search: “@text”', [
        '@text' => $target['dest'],
      ]),
      '#url' => Url::fromUserInput('/search/node', [
        'query' => [
          'keys' => $searchString,
        ],
        'language' => $target['language'],
      ]),
      '#attributes' => [
        'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
          ->getTip(),
      ],
    ];
  }
  elseif ($failover === 'google') {
    return GoogleSearch::createRenderArray($searchString, $target['text'], $target['language'], $this
      ->getTip());
  }
  return [
    '#theme' => 'freelink_error',
    '#plugin' => 'search',
    '#message' => $this
      ->t('Search unavailable'),
  ];
}