View source
<?php
namespace Drupal\freelinking\Plugin\freelinking;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\FreelinkingPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Search extends FreelinkingPluginBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $moduleHandler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $moduleHandler;
}
public function getIndicator() {
return '/^search$/';
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'settings' => [
'failover' => 'error',
],
];
}
public function getTip() {
return $this
->t('Search this site for content.');
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['failover'] = [
'#type' => 'select',
'#title' => $this
->t('Failover Option'),
'#description' => $this
->t('If Search is disabled or inaccessible do something else.'),
'#options' => [
'error' => $this
->t('Error Message'),
'google' => $this
->t('Google'),
],
];
return $element;
}
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'),
];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'));
}
}