View source
<?php
namespace Drupal\support_ticket\Plugin\views\row;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\views\Plugin\views\row\RssPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\support_ticket\SupportTicketStorageInterface;
class Rss extends RssPluginBase {
var $base_table = 'support_ticket_field_data';
var $base_field = 'stid';
var $support_tickets = array();
protected $entityTypeId = 'support_ticket';
protected $supportTicketStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager);
$this->supportTicketStorage = $entity_manager
->getStorage('support_ticket');
}
public function buildOptionsForm_summary_options() {
$options = parent::buildOptionsForm_summary_options();
$options['title'] = $this
->t('Title only');
$options['default'] = $this
->t('Use site default RSS settings');
return $options;
}
public function summaryTitle() {
$options = $this
->buildOptionsForm_summary_options();
return SafeMarkup::checkPlain($options[$this->options['view_mode']]);
}
public function preRender($values) {
$stids = array();
foreach ($values as $row) {
$stids[] = $row->{$this->field_alias};
}
if (!empty($stids)) {
$this->support_tickets = $this->supportTicketStorage
->loadMultiple($stids);
}
}
public function render($row) {
global $base_url;
$stid = $row->{$this->field_alias};
if (!is_numeric($stid)) {
return;
}
$display_mode = $this->options['view_mode'];
if ($display_mode == 'default') {
$display_mode = \Drupal::config('system.rss')
->get('items.view_mode');
}
$support_ticket = $this->support_tickets[$stid];
if (empty($support_ticket)) {
return;
}
$description_build = [];
$support_ticket->link = $support_ticket
->url('canonical', array(
'absolute' => TRUE,
));
$support_ticket->rss_namespaces = array();
$support_ticket->rss_elements = array(
array(
'key' => 'pubDate',
'value' => gmdate('r', $support_ticket
->getCreatedTime()),
),
array(
'key' => 'dc:creator',
'value' => $support_ticket
->getOwner()
->getDisplayName(),
),
array(
'key' => 'guid',
'value' => $support_ticket
->id() . ' at ' . $base_url,
'attributes' => array(
'isPermaLink' => 'false',
),
),
);
$build_mode = $display_mode;
$build = support_ticket_view($support_ticket, $build_mode);
unset($build['#theme']);
if (!empty($support_ticket->rss_namespaces)) {
$this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $support_ticket->rss_namespaces);
}
elseif (function_exists('rdf_get_namespaces')) {
$xml_rdf_namespaces = array();
foreach (rdf_get_namespaces() as $prefix => $uri) {
$xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
}
$this->view->style_plugin->namespaces += $xml_rdf_namespaces;
}
if ($display_mode != 'title') {
$description_build = $build;
}
$item = new \stdClass();
$item->description = $description_build;
$item->title = $support_ticket
->label();
$item->link = $support_ticket->link;
$item->elements =& $support_ticket->rss_elements;
$item->stid = $support_ticket
->id();
$build = array(
'#theme' => $this
->themeFunctions(),
'#view' => $this->view,
'#options' => $this->options,
'#row' => $item,
);
return $build;
}
}