-
Notifications
You must be signed in to change notification settings - Fork 0
/
feeds.module
185 lines (159 loc) · 5.04 KB
/
feeds.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* @file
* Feeds - basic API functions and hook implementations.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\feeds\Entity\Feed;
use Drupal\feeds\FeedInterface;
use Drupal\file\Entity\File;
/**
* Implements hook_help().
*/
function feeds_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'feeds.overview_types':
return '<p>' . t('Create one or more feed types for pulling content into Drupal.') . '</p>';
}
}
/**
* Implements hook_cron().
*/
function feeds_cron() {
$ids = \Drupal::entityQuery('feeds_feed')
->condition('queued', 0)
->condition('next', REQUEST_TIME, '<=')
->condition('next', -1, '<>')
->range(0, 100)
->sort('imported')
->execute();
foreach (Feed::loadMultiple($ids) as $feed) {
if ($feed->isLocked()) {
continue;
}
$queue = \Drupal::queue('feeds_feed_import:' . $feed->bundle());
if ($queue->createItem($feed)) {
// Add timestamp to avoid queueing item more than once.
$feed->setQueuedTime(REQUEST_TIME);
$feed->save();
}
}
// Delete queued timestamp after 12 hours assuming the update has failed.
$ids = \Drupal::entityQuery('feeds_feed')
->condition('queued', REQUEST_TIME - (3600 * 12), '<')
->execute();
foreach (Feed::loadMultiple($ids) as $feed) {
$feed->setQueuedTime(0);
$feed->save();
}
}
/**
* Implements hook_theme().
*/
function feeds_theme() {
return [
'feeds_feed_status' => [
'variables' => [
'progress_importing' => NULL,
'progress_clearing' => NULL,
'imported' => NULL,
'count' => NULL,
],
'file' => 'feeds.theme.inc',
],
'feeds_feed' => [
'render element' => 'elements',
'template' => 'feeds_feed',
],
];
}
/**
* Implements hook_file_download().
*/
function feeds_file_download($uri) {
// Get the file record based on the URI. If not in the database just return.
$files = \Drupal::entityManager()
->getStorage('file')
->loadByProperties(['uri' => $uri]);
foreach ($files as $item) {
// Since some database servers sometimes use a case-insensitive comparison
// by default, double check that the filename is an exact match.
if ($file->getFileUri() === $uri) {
$file = $item;
break;
}
}
if (!isset($file)) {
return;
}
// Check if this file belongs to Feeds.
$usage = \Drupal::service('file.usage')->listUsage($file);
if (!isset($usage['feeds'])) {
return;
}
$feeds = \Drupal::entityManager()
->getStorage('feeds_feed')
->loadByProperties(['source' => $uri]);
foreach ($feeds as $feed) {
if ($feed->getSource() === $uri && $feed->access('import')) {
return file_get_content_headers($file);
}
}
return -1;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Remove our field from the Field UI overview form.
*/
function feeds_form_field_ui_field_overview_form_alter(array &$form, FormStateInterface $form_state) {
// if (in_array('feeds_item', $form['#fields'])) {
// unset($form['#fields'][array_search('feeds_item', $form['#fields'])]);
// unset($form['fields']['feeds_item']);
// $rows_order = $form['fields']['#regions']['content']['rows_order'];
// $key = array_search('feeds_item', $rows_order);
// unset($form['fields']['#regions']['content']['rows_order'][$key]);
// }
}
/**
* Prepares variables for feed templates.
*
* Default template: feeds_feed.html.twig.
*
* Most themes utilize their own copy of feeds_feed.html.twig. The default is
* located inside "modules/feeds/templates/feeds_feedhtml.twig". Look in there
* for the full list of variables.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - feed: The feed object.
* - view_mode: View mode; e.g., 'full', 'teaser'...
*/
function template_preprocess_feeds_feed(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['feed'] = $feed = $variables['elements']['#feeds_feed'];
$variables['page'] = $variables['view_mode'] === 'full';
$variables['date'] = drupal_render($variables['elements']['created']);
unset($variables['elements']['created']);
$variables['author_name'] = drupal_render($variables['elements']['uid']);
unset($variables['elements']['uid']);
$variables['url'] = $feed->url('canonical', [
'language' => $feed->language(),
]);
$variables['label'] = $variables['elements']['title'];
unset($variables['elements']['title']);
// Helpful $content variable for templates.
$variables += ['content' => []];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Used by RDF to add attributes around the author and date submitted.
$variables['author_attributes'] = new Attribute();
// Add article ARIA role.
$variables['attributes']['role'] = 'article';
}