-
Notifications
You must be signed in to change notification settings - Fork 1
/
views_googlenews.module
67 lines (62 loc) · 2.48 KB
/
views_googlenews.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
<?php
/**
* @file
* Views Google News module bootstrap file.
*/
use Drupal\Component\Utility\SafeMarkup;
/**
* Prepares variables for GoogleNews feed templates.
*
* Default template: views-view-googlenews.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_googlenews(&$variables) {
$config = \Drupal::config('system.site');
$name = $config->get('name');
$items = $variables['rows'];
foreach ($items as &$item) {
if (empty($item['#row']['news_publication_name'])) {
$item['#row']['news_publication_name'] = $name;
}
if (empty($item['#row']['news_publication_language'])) {
$item['#row']['news_publication_language'] = \Drupal::languageManager()->getDefaultLanguage()->getId();
}
}
$variables['items'] = $items;
// During live preview we don't want to output the header since the contents
// of the feed are being displayed inside a normal HTML page.
if (empty($variables['view']->live_preview)) {
$variables['view']->getResponse()->headers->set('Content-Type', 'text/xml; charset=utf-8');
}
}
/**
* Prepares variables for views Google News item templates.
*
* Default template: views-view-row-googlenews.html.twig.
*
* @param array $variables
* An associative array containing:
* - row: The raw results rows.
*/
function template_preprocess_views_view_row_googlenews(&$variables) {
$item = $variables['row'];
// Allow item to be altered.
\Drupal::moduleHandler()->alter('views_googlenews_item', $item);
$variables['loc'] = check_url($item['loc']);
$variables['name'] = SafeMarkup::checkPlain($item['news_publication_name']);
$variables['language'] = SafeMarkup::checkPlain($item['news_publication_language']);
$variables['access'] = empty($item['news_access']) ? '' : SafeMarkup::checkPlain($item['news_access']);
$variables['genres'] = empty($item['news_genres']) ? '' : SafeMarkup::checkPlain($item['news_genres']);
$variables['publication_date'] = empty($item['news_publication_date']) ? '' : trim(
strip_tags($item['news_publication_date'])
);
$variables['title'] = SafeMarkup::checkPlain(strip_tags(($item['news_title'])));
$variables['keywords'] = empty($item['news_keywords']) ? '' : SafeMarkup::checkPlain(strip_tags($item['news_keywords']));
$variables['stock_tickers'] = empty($item['news_stock_tickers']) ? '' : SafeMarkup::checkPlain(
strip_tags($item['news_stock_tickers'])
);
}