-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppi_print_variant.module
78 lines (67 loc) · 2.26 KB
/
ppi_print_variant.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
<?php
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
function ppi_print_variant_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.ppi_print_variant':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Create new content from existing content without online specific paragraphs.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_entity_type_build().
*/
function ppi_print_variant_entity_type_build(array &$entity_types) {
if(isset($entity_types['node'])) {
$entity_types['node']->setFormClass('ppi_print_variant', 'Drupal\ppi_print_variant\Form\PpiPrintVariantNodeForm');
}
}
/**
* Implements hook_entity_operation().
*/
function ppi_print_variant_entity_operation(\Drupal\Core\Entity\EntityInterface $entity) {
$operations = [];
$bundle = $entity->bundle();
// Only add an operation for node entities.
if ($entity->getEntityTypeId() !== 'node') {
return $operations;
}
if (!_ppi_print_variant_has_print_variant_permission($bundle)) {
return $operations;
}
$path = '/ppi/' . $entity->id() . '/print_variant';
$operations['print_variant'] = [
'title' => t('Create Print Variant'),
'weight' => '100',
'url' => Url::fromUri('internal:/' . trim($path, '/')),
];
return $operations;
}
/**
* Determine if the current user has permission to create a print variant.
*
* @param string $bundle
* The bundle machine name to examine.
*
* @return bool
* TRUE or FALSE
*/
function _ppi_print_variant_has_print_variant_permission($bundle) {
if(!is_string($bundle)) {
throw new InvalidArgumentException('Argument 1 passed to _ppi_print_variant_has_print_variant_permission() must be of type string, ' . gettype($bundle) . ' given.');
}
$current_user = \Drupal::currentUser();
if ($current_user->hasPermission("administer nodes")) {
return TRUE;
}
if ($current_user->hasPermission("bypass node access")) {
return TRUE;
}
if ($current_user->hasPermission("print_variant $bundle content") && $current_user->hasPermission("create $bundle content")) {
return TRUE;
}
return FALSE;
}