-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilverback_preview_link.module
79 lines (70 loc) · 2.67 KB
/
silverback_preview_link.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\silverback_preview_link\PreviewLinkHooks;
use Drupal\silverback_preview_link\PreviewLinkUtility;
use Drupal\silverback_preview_link\Routing\PreviewLinkRouteProvider;
/**
* Implements hook_entity_type_alter().
*/
function silverback_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 (count($providers['silverback_preview_link'] ?? []) === 0) {
$providers['silverback_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 silverback_preview_link_cron(): void {
\Drupal::classResolver(PreviewLinkHooks::class)->cron();
}
/**
* Implements hook_theme().
*/
function silverback_preview_link_theme(array $existing, string $type, string $theme, string $path): array {
return [
'preview_link' => [
'path' => $path . '/templates',
'template' => 'preview-link',
'variables' => [
'title' => NULL,
'preview_link_has_expired' => FALSE,
'preview_url' => NULL,
'preview_qr_code_url' => NULL,
'preview_qr_code_fallback' => NULL,
'expiry_description' => NULL,
'actions_description' => NULL,
'display_gif' => FALSE,
],
],
];
}
/**
* Implements hook_entity_field_access().
*/
function silverback_preview_link_entity_field_access(string $operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL): AccessResultInterface {
$storageDefinition = $field_definition->getFieldStorageDefinition();
if ($storageDefinition->getTargetEntityTypeId() !== 'silverback_preview_link') {
return AccessResult::neutral();
}
if ($storageDefinition->getName() === 'entities' && $operation === 'edit') {
return AccessResult::forbiddenIf(\Drupal::configFactory()->get('silverback_preview_link.settings')->get('multiple_entities') !== TRUE)
->addCacheTags(['config:preview_link.settings']);
}
return AccessResult::neutral();
}