GoogleSearch.php in Freelinking 8.3
File
src/Plugin/freelinking/GoogleSearch.php
View source
<?php
namespace Drupal\freelinking\Plugin\freelinking;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\FreelinkingPluginBase;
class GoogleSearch extends FreelinkingPluginBase {
public function getIndicator() {
return '/^google$/';
}
public function getTip() {
return $this
->t('Search google for the specified terms.');
}
public function buildLink(array $target) {
$searchString = str_replace(' ', '+', $target['dest']);
$tooltip = isset($target['tooltip']) ? $target['tooltip'] : $this
->getTip();
return self::createRenderArray($searchString, $target['text'], $target['language'], $tooltip);
}
public static function createRenderArray($searchString, $text, $language, TranslatableMarkup $tip) {
$lang = NULL === $language || LanguageInterface::LANGCODE_DEFAULT === $language
->getId() ? 'en' : $language
->getId();
return [
'#type' => 'link',
'#title' => 'Google Search ' . $text,
'#url' => Url::fromUri('https://google.com/search', [
'absolute' => TRUE,
'query' => [
'q' => $searchString,
'hl' => $lang,
],
'language' => $language,
]),
'#attributes' => array(
'title' => $tip,
),
];
}
}