class Redirect in File (Field) Paths 8
Service for creating file redirects.
Hierarchy
- class \Drupal\filefield_paths\Redirect implements RedirectInterface
Expanded class hierarchy of Redirect
1 string reference to 'Redirect'
1 service uses Redirect
File
- src/
Redirect.php, line 15
Namespace
Drupal\filefield_pathsView source
class Redirect implements RedirectInterface {
/**
* The redirect storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $redirectStorage;
/**
* The stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructs a new redirect service.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The stream wrapper manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, StreamWrapperManagerInterface $stream_wrapper_manager, ConfigFactoryInterface $config_factory, LoggerInterface $logger) {
$this->redirectStorage = $entity_type_manager
->getStorage('redirect');
$this->streamWrapperManager = $stream_wrapper_manager;
$this->configFactory = $config_factory;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function createRedirect($source, $path, Language $language) {
$this->logger
->debug('Creating redirect from @source to @path.', [
'@source' => $source,
'@path' => $path,
]);
/** @var \Drupal\redirect\Entity\Redirect $redirect */
$redirect = $this->redirectStorage
->create([]);
$parsed_source = $this
->getPath($source);
$parsed_path = $this
->getPath($path);
$redirect
->setSource($parsed_source);
$redirect
->setRedirect($parsed_path);
$redirect
->setStatusCode($this->configFactory
->get('redirect.settings')
->get('default_status_code'));
// Check if the redirect doesn't already exist before saving.
$hash = $redirect
->generateHash($parsed_path, [], $language
->getId());
$redirects = $this->redirectStorage
->loadByProperties([
'hash' => $hash,
]);
if (empty($redirects)) {
// Redirect does not exist yet, save as new one.
$redirect
->save();
}
}
/**
* Returns the path to the file, starting from the Drupal root.
*
* @param string $file_uri
* The file url to get the path for.
*
* @return string|null
* The file path, if found. Null otherwise.
*/
protected function getPath($file_uri) {
if ($wrapper = $this->streamWrapperManager
->getViaUri($file_uri)) {
$directory = $wrapper
->getDirectoryPath();
$target = StreamWrapperManager::getTarget($file_uri);
return $directory . DIRECTORY_SEPARATOR . $target;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Redirect:: |
protected | property | The config factory service. | |
Redirect:: |
protected | property | A logger instance. | |
Redirect:: |
protected | property | The redirect storage. | |
Redirect:: |
protected | property | The stream wrapper manager. | |
Redirect:: |
public | function |
Creates a redirect for a moved File field. Overrides RedirectInterface:: |
|
Redirect:: |
protected | function | Returns the path to the file, starting from the Drupal root. | |
Redirect:: |
public | function | Constructs a new redirect service. |