-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_entity.module
More file actions
86 lines (72 loc) · 2.82 KB
/
preview_entity.module
File metadata and controls
86 lines (72 loc) · 2.82 KB
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
80
81
82
83
84
85
86
<?php
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* @param array $build
* @param EntityInterface $entity
* @param EntityViewDisplayInterface $display
*/
function preview_entity_node_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($display->getMode() == 'search_index') {
return;
}
/** @var \Drupal\preview_entity\LinkBuilderInterface $builder */
$builder = \Drupal::service('preview_entity.link_builder');
$published = TRUE;
if (($entity instanceof EntityPublishedInterface) && !$entity->isPublished()) {
$published = FALSE;
}
if (($entity instanceof RevisionableInterface) && !$entity->isDefaultRevision()) {
$published = FALSE;
}
$text_attributes = [];
if ($published) {
$link = $builder->buildPublishedLink($entity);
$text = 'This content is published. Go to @link to view it on the live site.';
} else {
$valid_until = $builder->determineValidUntil();
$valid_until_string = \Drupal::service('date.formatter')
->format($valid_until, 'medium');
$link = $builder->buildPreviewLink($entity, $valid_until);
$text = 'This content is not published. Go to @link to preview it on the live site. This link will be valid until :date.';
$text_attributes[':date'] = $valid_until_string;
}
$build['preview_links'] = [
'#theme_wrappers' => ['preview_entity_container'],
'#text' => $text,
'#text_attributes' => $text_attributes,
'#attached' => ['library' => ['preview_entity/preview_entity']],
'link' => $link,
'#cache' => ['max-age' => 0],
];
}
function preview_entity_theme() {
return [
'preview_entity_container' => [
'render element' => 'element',
],
];
}
function template_preprocess_preview_entity_container(&$variables) {
$element = $variables['element'];
$variables['link'] = $element['#children'];
$variables['text'] = $element['#text'];
$variables['text_attributes'] = !empty($element['#text_attributes']) ? $element['#text_attributes'] : [];
$variables['text_attributes']['@link'] = $variables['link'];
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function preview_entity_field_widget_link_default_form_alter(&$element, &$form_state, $context) {
if($element['uri']['#type'] == 'entity_autocomplete') {
if(isset($element['uri']['#field_prefix'])){
$element['uri']['#description'] = t('Begin de titel van een entiteit te typen om deze te selecteren.');
}
else {
$element['uri']['#description'] = t('Begin de titel van een entiteit te typen om deze te selecteren. Een externe URL zoals %url is ook geldig.', ['%url' => 'http://example.com']);
}
}
}