View source
<?php
namespace Drupal\freelinking\Plugin\freelinking;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\FreelinkingPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class File extends FreelinkingPluginBase implements ContainerFactoryPluginInterface {
protected $fileSystemConfig;
protected $streamWrapperManager;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $configFactory, StreamWrapperManagerInterface $streamWrapperManager, ModuleHandlerInterface $moduleHandler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fileSystemConfig = $configFactory
->get('system.file');
$this->streamWrapperManager = $streamWrapperManager;
$this->moduleHandler = $moduleHandler;
}
public function getIndicator() {
return '/^file$/i';
}
public function getTip() {
return $this
->t('Click to view a local file.');
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'settings' => [
'scheme' => $this->fileSystemConfig
->get('default_scheme'),
],
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['scheme'] = [
'#type' => 'select',
'#title' => $this
->t('File System'),
'#description' => $this
->t('Choose the file system to use to lookup files.'),
'#options' => $this
->getSchemeOptions(),
];
return $element;
}
public function buildLink(array $target) {
$scheme = $this
->getConfiguration()['settings']['scheme'];
$dest = preg_replace('/^\\//', '', $target['dest']);
$path = $scheme . '://' . $dest;
$stream_wrapper = $this
->loadScheme($scheme);
$stream_wrapper
->setUri($path);
if (!$stream_wrapper
->realpath()) {
return [
'#theme' => 'freelink_error',
'#plugin' => 'file',
'#message' => $this
->t('File @name not found', [
'@name' => basename($path),
]),
];
}
$headers = $this->moduleHandler
->invokeAll('file_download', [
$path,
]);
if (in_array(-1, $headers)) {
return [
'#theme' => 'freelink_error',
'#plugin' => 'file',
'#message' => $this
->t('File @name not found', [
'@name' => basename($path),
]),
];
}
$file_url = $stream_wrapper
->getExternalUrl();
return [
'#type' => 'link',
'#title' => isset($target['text']) ? $target['text'] : basename($path),
'#url' => Url::fromUri($file_url, [
'absolute' => TRUE,
'language' => $target['language'],
]),
'#attributes' => [
'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
->getTip(),
],
];
}
protected function getSchemeOptions() {
$options = [];
$schemes = $this->streamWrapperManager
->getNames(StreamWrapperInterface::WRITE_VISIBLE);
foreach ($schemes as $scheme => $name) {
$options[$scheme] = $name;
}
return $options;
}
protected function loadScheme($scheme) {
return $this->streamWrapperManager
->getViaScheme($scheme);
}
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('stream_wrapper_manager'), $container
->get('module_handler'));
}
}