class File in Freelinking 4.0.x
Same name and namespace in other branches
- 8.3 src/Plugin/freelinking/File.php \Drupal\freelinking\Plugin\freelinking\File
Freelinking file plugin.
Plugin annotation
@Freelinking(
id = "file",
title = @Translation("File"),
weight = 0,
hidden = false,
settings = {
"scheme" = "public"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\freelinking\Plugin\FreelinkingPluginBase implements FreelinkingPluginInterface
- class \Drupal\freelinking\Plugin\freelinking\File implements ContainerFactoryPluginInterface
- class \Drupal\freelinking\Plugin\FreelinkingPluginBase implements FreelinkingPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of File
1 file declares its use of File
- FileTest.php in tests/
src/ Unit/ Plugin/ freelinking/ FileTest.php
1 string reference to 'File'
- FileTest::getPlugin in tests/
src/ Unit/ Plugin/ freelinking/ FileTest.php - Get the plugin to test.
File
- src/
Plugin/ freelinking/ File.php, line 28
Namespace
Drupal\freelinking\Plugin\freelinkingView source
class File extends FreelinkingPluginBase implements ContainerFactoryPluginInterface {
/**
* File system configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $fileSystemConfig;
/**
* Stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Initialize method.
*
* @param array $configuration
* The plugin configuration array.
* @param string $plugin_id
* The plugin ID.
* @param array $plugin_definition
* The plugin definition array.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The configuration factory.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $streamWrapperManager
* The stream wrapper manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*/
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;
}
/**
* {@inheritdoc}
*/
public function getIndicator() {
return '/^file$/i';
}
/**
* {@inheritdoc}
*/
public function getTip() {
return $this
->t('Click to view a local file.');
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'settings' => [
'scheme' => $this->fileSystemConfig
->get('default_scheme'),
],
];
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function buildLink(array $target) {
$scheme = $this
->getConfiguration()['settings']['scheme'];
// Remove slash if it's present at first character.
$dest = preg_replace('/^\\//', '', $target['dest']);
$path = $scheme . '://' . $dest;
$stream_wrapper = $this
->loadScheme($scheme);
$stream_wrapper
->setUri($path);
// Make sure that the file exists on the given scheme.
if (!$stream_wrapper
->realpath()) {
return [
'#theme' => 'freelink_error',
'#plugin' => 'file',
'#message' => $this
->t('File @name not found', [
'@name' => basename($path),
]),
];
}
// Check file access.
$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),
]),
];
}
// Return a link to the file.
$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(),
],
];
}
/**
* Get the stream managers keyed by name for the options list.
*
* @return array
* An array of options.
*/
protected function getSchemeOptions() {
$options = [];
$schemes = $this->streamWrapperManager
->getNames(StreamWrapperInterface::WRITE_VISIBLE);
foreach ($schemes as $scheme => $name) {
$options[$scheme] = $name;
}
return $options;
}
/**
* Load the stream wrapper for a given scheme.
*
* @param string $scheme
* The scheme to load.
*
* @return \Drupal\Core\StreamWrapper\StreamWrapperInterface
* The stream wrapper to use.
*/
protected function loadScheme($scheme) {
return $this->streamWrapperManager
->getViaScheme($scheme);
}
/**
* {@inheritdoc}
*/
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'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
File:: |
protected | property | File system configuration. | |
File:: |
protected | property | Module handler. | |
File:: |
protected | property | Stream wrapper manager. | |
File:: |
public | function |
Build a link with the plugin. Overrides FreelinkingPluginInterface:: |
|
File:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
File:: |
public | function |
Gets default configuration for this plugin. Overrides FreelinkingPluginBase:: |
|
File:: |
public | function |
A regular expression string to indicate what to replace for this plugin. Overrides FreelinkingPluginInterface:: |
|
File:: |
protected | function | Get the stream managers keyed by name for the options list. | |
File:: |
public | function |
Provides tips for this freelinking plugin. Overrides FreelinkingPluginInterface:: |
|
File:: |
protected | function | Load the stream wrapper for a given scheme. | |
File:: |
public | function |
Plugin configuration form. Overrides FreelinkingPluginBase:: |
|
File:: |
public | function |
Initialize method. Overrides PluginBase:: |
|
FreelinkingPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
FreelinkingPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
1 |
FreelinkingPluginBase:: |
public | function |
Get the failover plugin ID (if applicable). Overrides FreelinkingPluginInterface:: |
1 |
FreelinkingPluginBase:: |
public | function |
Determine if the plugin is built-in (always on). Overrides FreelinkingPluginInterface:: |
|
FreelinkingPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |