-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstanford_courses.module
72 lines (61 loc) · 2.27 KB
/
stanford_courses.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
<?php
/**
* @file
* Stanford_courses.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_pre_render().
*/
function stanford_courses_views_pre_render(ViewExecutable $view) {
if (isset($view) && ($view->storage->id() == 'stanford_courses')) {
$view->element['#attached']['library'][] = 'stanford_courses/courses_page';
}
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function stanford_courses_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Only run on stanford course items.
if ($entity->bundle() !== "stanford_course" || !node_is_page($entity)) {
return;
}
$rh_action = $entity->get('rh_action')->getString();
$rh_redirect = $entity->get('rh_redirect')->getString();
// Display a message to the user that this page would redirect to another
// location for other users.
if ($rh_action == "page_redirect" && !empty($rh_redirect)) {
\Drupal::messenger()
->addWarning(t('This page will redirect to @url for other visitors.', ['@url' => $rh_redirect]));
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function stanford_courses_node_presave(EntityInterface $entity) {
if ($entity->bundle() != "stanford_course") {
return;
}
$rabbit_hole = \Drupal::config('rabbit_hole.behavior_settings.node_type_' . $entity->bundle());
$token_service = \Drupal::service('token');
// When the su_course_link field is filled out on an item we want to set the
// redirect options so it doesn't get indexed.
if ($rabbit_hole) {
/** @var \Drupal\Core\Utility\Token $token_service */
$target = $token_service->replace("[node:su_course_link:uri]", ['node' => $entity]);
// Check if the token that was provided is actually a url. If it is, then we
// can set the rabbit hole action to redirect to that url.
if (is_string($target) && UrlHelper::isValid($target, TRUE)) {
$entity->set('rh_action', 'page_redirect');
$entity->set('rh_redirect', $target);
$entity->set('rh_redirect_response', 301);
}
else {
$entity->set('rh_action', 'bundle_default');
$entity->set('rh_redirect', '');
}
}
}