View source
<?php
namespace Drupal\google_cse\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GoogleCSEBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $configFactory;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('language_manager'));
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'search Google CSE');
}
public function defaultConfiguration() {
return [
'label' => $this
->t('Search'),
];
}
public function build() {
$config = $this->configFactory
->get('search.page.google_cse_search');
return [
'#theme' => 'google_cse_results',
'#form' => TRUE,
'#markup' => 'search',
'#attached' => [
'library' => [
'google_cse/googlecseWatermark',
],
'drupalSettings' => [
'googleCSE' => [
'cx' => $config
->get('configuration')['cx'],
'language' => google_cse_language(),
'resultsWidth' => intval($config
->get('configuration')['results_width']),
'domain' => $config
->get('configuration')['domain'],
],
],
],
];
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), $this->configFactory
->get('search.page.google_cse_search')
->getCacheTags());
}
}