-
Notifications
You must be signed in to change notification settings - Fork 1
/
github_cards.module
104 lines (87 loc) · 3.1 KB
/
github_cards.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* @file
* Contains github_cards.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function github_cards_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the github_cards module.
case 'help.page.github_cards':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('GitHub user and repo cards.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function github_cards_theme($existing, $type, $theme, $path) {
$theme = [];
$theme['github_card'] = [
'render element' => 'elements',
'path' => $path . '/templates',
'template' => 'github-card',
];
$theme['github_card__user'] = [
'path' => $path . '/templates',
'template' => 'github-card--user',
'base hook' => 'github_card',
];
$theme['github_card__repository'] = [
'path' => $path . '/templates',
'template' => 'github-card--repository',
'base hook' => 'github_card',
];
return $theme;
}
/**
* Prepares variables for GitHub Card templates.
*
* Default template: github-card.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - view_mode: View mode; e.g., 'full', 'teaser', etc.
*
* @see hook_entity_type_build()
* @see \Drupal\Core\Field\BaseFieldDefinition::setDisplayConfigurable()
*/
function github_cards_preprocess_github_card(array &$variables) {
/** @var \Drupal\github_cards\Entity\GitHubCardEntityInterface $card */
$card = $variables['elements']['#github_card'];
$card_info = $card->fetchResourceData();
$variables['github_card'] = $card;
$variables['github_card_info'] = $card_info;
$variables['label'] = $card->label();
$variables['url'] = !$card->isNew() ? $card->toUrl('canonical')->toString() : NULL;
$variables['attributes']['data-uuid'] = $card->uuid();
$variables['attributes']['data-github-resource-type'] = $card->getResourceType();
$variables['attributes']['data-github-id'] = $card_info['id'];
$variables['attributes']['data-github-node-id'] = $card_info['node_id'];
$variables['attributes']['data-github-name'] = $card_info['name'];
$variables['attributes']['data-github-url'] = $card_info['html_url'];
// Add article ARIA role.
$variables['attributes']['role'] = 'article';
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function github_cards_theme_suggestions_github_card(array $variables) {
$suggestions = [];
/** @var \Drupal\github_cards\Entity\GitHubCardEntityInterface $entity */
$entity = $variables['elements']['#github_card'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'github_card__' . $sanitized_view_mode;
$suggestions[] = 'github_card__' . $entity->getResourceType();
$suggestions[] = 'github_card__' . $entity->getResourceType() . '__' . $sanitized_view_mode;
$suggestions[] = 'github_card__' . $entity->id();
$suggestions[] = 'github_card__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}