preview_link.module in Preview Link 2.0.x
Same filename and directory in other branches
Module file.
File
preview_link.moduleView source
<?php
/**
* @file
* Module file.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\preview_link\PreviewLinkHooks;
use Drupal\preview_link\PreviewLinkUtility;
use Drupal\preview_link\Routing\PreviewLinkRouteProvider;
/**
* Implements hook_entity_type_alter().
*/
function preview_link_entity_type_alter(array &$entity_types) : void {
$supported_entity_types = array_filter($entity_types, [
PreviewLinkUtility::class,
'isEntityTypeSupported',
]);
/** @var \Drupal\Core\Entity\ContentEntityType $type */
foreach ($supported_entity_types as $type) {
$providers = $type
->getRouteProviderClasses() ?: [];
if (empty($providers['preview_link'])) {
$providers['preview_link'] = PreviewLinkRouteProvider::class;
$type
->setHandlerClass('route_provider', $providers);
}
if (!$type
->hasLinkTemplate('preview-link-generate')) {
$type
->setLinkTemplate('preview-link-generate', $type
->getLinkTemplate('canonical') . '/generate-preview-link');
}
}
}
/**
* Implements hook_cron().
*/
function preview_link_cron() : void {
\Drupal::classResolver(PreviewLinkHooks::class)
->cron();
}
/**
* Implements hook_theme().
*/
function preview_link_theme(array $existing, string $type, string $theme, string $path) : array {
return [
'preview_link' => [
'path' => $path . '/templates',
'template' => 'preview-link',
'variables' => [
'title' => NULL,
'link' => NULL,
'description' => NULL,
'remaining_lifetime' => NULL,
],
],
];
}
/**
* Implements hook_entity_access().
*/
function preview_link_entity_access(EntityInterface $entity, string $operation, AccountInterface $account) : AccessResultInterface {
$neutral = AccessResult::neutral()
->addCacheableDependency($entity)
->addCacheContexts([
'preview_link_route',
]);
if ($operation !== 'view' || !$entity instanceof ContentEntityInterface) {
return $neutral;
}
return \Drupal::service('access_check.preview_link')
->access($entity, \Drupal::routeMatch()
->getParameter('preview_token'));
}
/**
* Implements hook_entity_field_access().
*/
function preview_link_entity_field_access(string $operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) : AccessResultInterface {
$storageDefinition = $field_definition
->getFieldStorageDefinition();
if ($storageDefinition
->getTargetEntityTypeId() !== 'preview_link') {
return AccessResult::neutral();
}
if ($storageDefinition
->getName() === 'entities' && $operation === 'edit') {
return AccessResult::forbiddenIf(\Drupal::configFactory()
->get('preview_link.settings')
->get('multiple_entities') !== TRUE)
->addCacheTags([
'config:preview_link.settings',
]);
}
return AccessResult::neutral();
}
Functions
Name | Description |
---|---|
preview_link_cron | Implements hook_cron(). |
preview_link_entity_access | Implements hook_entity_access(). |
preview_link_entity_field_access | Implements hook_entity_field_access(). |
preview_link_entity_type_alter | Implements hook_entity_type_alter(). |
preview_link_theme | Implements hook_theme(). |