View source
<?php
namespace Drupal\json_feed\Plugin\views\style;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\style\StylePluginBase;
class JsonFeedSerializer extends StylePluginBase {
protected $usesRowPlugin = TRUE;
protected $usesGrouping = FALSE;
public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
$url_options = [];
$input = $this->view
->getExposedInput();
if ($input) {
$url_options['query'] = $input;
}
$url_options['absolute'] = TRUE;
$url = $feed_url
->setOptions($url_options)
->toString();
$build['#attached']['library'][] = 'json_feed/json-feed';
$this->view->feedIcons[] = [
'#theme' => 'json_feed_icon',
'#url' => $url,
'#title' => $title,
];
$build['#attached']['html_head_link'][][] = [
'rel' => 'alternate',
'type' => 'application/json',
'title' => $title,
'href' => $url,
];
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['description'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['home_page_url_note'] = [
'#type' => 'item',
'#title' => $this
->t('JSON Feed home_page_url'),
'#description' => $this
->t("Set Link Display to your view's main Page display to enable home_page_url"),
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this
->t('JSON Feed description'),
'#default_value' => $this->options['description'],
'#description' => $this
->t('This will appear in the JSON feed itself.'),
'#maxlength' => 1024,
];
$form['author'] = [
'#type' => 'details',
'#title' => $this
->t('Author'),
'#open' => TRUE,
];
$form['author']['author_name_field'] = [
'#type' => 'textfield',
'#title' => $this
->t('feed author name attribute'),
'#description' => $this
->t("JSON author name attribute."),
'#default_value' => $this->options['author']['author_name_field'],
];
$form['author']['author_url_field'] = [
'#type' => 'textfield',
'#title' => $this
->t('feed author url attribute'),
'#description' => $this
->t("The URL of a site owned by the feed's author."),
'#default_value' => $this->options['author']['author_url_field'],
];
$form['author']['author_avatar_field'] = [
'#type' => 'textfield',
'#title' => $this
->t('feed author avatar attribute'),
'#description' => $this
->t("The URL for an image for the feed's author."),
'#default_value' => $this->options['author']['author_avatar_field'],
];
$form['expired'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Feed Expired'),
'#default_value' => $this->options['expired'],
'#description' => $this
->t('Specifies whether or not the feed is finished and will ever update again. For instance, a feed for a temporary event could expire.'),
];
}
public function validate() {
$errors = parent::validate();
if (!$this->view->display_handler
->getOption('sitename_title') && !$this->view
->getTitle()) {
$errors[] = $this
->t('The view must have a title');
}
return $errors;
}
public function getDescription() {
$description = $this->options['description'];
$description = $this
->tokenizeValue($description, 0);
return $description;
}
public function render() {
$items = [];
foreach ($this->view->result as $row_index => $row) {
$this->view->row_index = $row_index;
$items[] = $this->view->rowPlugin
->render($row);
}
unset($this->view->row_index);
$feed['version'] = 'https://jsonfeed.org/version/1';
$feed['title'] = strip_tags($this
->getTitle());
$feed['description'] = strip_tags($this
->getDescription());
$feed['home_page_url'] = strip_tags($this
->getFeedHomePageUrl());
$feed['feed_url'] = strip_tags($this->displayHandler
->getUrl()
->setAbsolute()
->toString());
$feed['favicon'] = strip_tags($this
->getFavicon());
$feed['author'] = array_map('strip_tags', $this
->getAuthor());
if ($next_url = $this
->getNextPage()) {
$feed['next_url'] = strip_tags($next_url);
}
$feed['items'] = $items;
$feed = array_filter($feed);
$feed['expired'] = $this
->isFeedExpired();
return Json::encode($feed);
}
protected function getAuthor() {
if (empty($this->options['author'])) {
return [];
}
$author_data = $this->options['author'];
$author['name'] = !empty($author_data['author_name_field']) ? strip_tags($author_data['author_name_field']) : NULL;
$author['url'] = !empty($author_data['author_url_field']) ? strip_tags($author_data['author_url_field']) : NULL;
$author['avatar'] = !empty($author_data['author_avatar_field']) ? strip_tags($author_data['author_avatar_field']) : NULL;
$author = array_filter($author);
return $author;
}
protected function getTitle() {
$config = \Drupal::config('system.site');
if ($this->view->display_handler
->getOption('sitename_title')) {
$title = $config
->get('name');
if ($slogan = $config
->get('slogan')) {
$title .= ' - ' . $slogan;
}
}
else {
$title = $this->view
->getTitle();
}
return $title;
}
protected function getFeedHomePageUrl() {
$link_display_id = $this->view->display_handler
->getLinkDisplay();
if ($link_display_id && ($display = $this->view->displayHandlers
->get($link_display_id))) {
$url = $this->view
->getUrl(NULL, $link_display_id);
}
$url_options = [
'absolute' => TRUE,
];
$base_url = Url::fromRoute('<front>')
->setAbsolute()
->toString();
if ($url) {
$config = \Drupal::config('system.site');
$url_string = $url
->setOptions($url_options)
->toString();
if ($url_string === Url::fromUserInput($config
->get('page.front'))
->toString()) {
$url_string = $base_url;
}
return $url_string;
}
return $base_url;
}
protected function getNextPage() {
$pager = $this->displayHandler
->getPlugin('pager');
if (empty($pager) || !$pager
->hasMoreRecords()) {
return NULL;
}
$pager_manager = \Drupal::service('pager.manager');
$options = [
'query' => $pager_manager
->getUpdatedParameters([], 0, $pager
->getCurrentPage() + 1),
];
return Url::fromRoute('<current>', [], $options)
->setAbsolute()
->toString();
}
protected function getFavicon() {
$favicon = theme_get_setting('favicon.url');
return $favicon ? Url::fromUserInput($favicon)
->setAbsolute()
->toString() : NULL;
}
protected function isFeedExpired() {
if (empty($this->options['expired'])) {
return FALSE;
}
return $this->options['expired'] ? TRUE : FALSE;
}
}