SharethisBlock.php in ShareThis 8.2
File
src/Plugin/Block/SharethisBlock.php
View source
<?php
namespace Drupal\sharethis\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\Config;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\sharethis\SharethisManagerInterface;
class SharethisBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $sharethisSettings;
protected $sharethisManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Config $sharethis_settings, SharethisManagerInterface $sharethis_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->sharethisSettings = $sharethis_settings;
$this->sharethisManager = $sharethis_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')
->get('sharethis.settings'), $container
->get('sharethis.manager'));
}
public function build() {
if ($this->sharethisSettings
->get('location') === 'block') {
$st_js = $this->sharethisManager
->sharethisIncludeJs();
$markup = $this->sharethisManager
->blockContents();
return [
'#theme' => 'sharethis_block',
'#content' => $markup,
'#attached' => [
'library' => [
'sharethis/sharethispickerexternalbuttonsws',
'sharethis/sharethispickerexternalbuttons',
'sharethis/sharethis',
],
'drupalSettings' => [
'sharethis' => $st_js,
],
],
'#cache' => [
'contexts' => [
'url.path',
],
],
];
}
}
}