View source
<?php
namespace Drupal\views_rss\Plugin\views\row;
use Drupal\Core\Link;
use Drupal\views\Plugin\views\row\RowPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\Xss;
class RssFields extends RowPluginBase {
protected $usesFields = TRUE;
protected function defineOptions() {
$options = parent::defineOptions();
$item_elements = views_rss_get('item_elements');
if (count($item_elements)) {
foreach ($item_elements as $module => $module_item_elements) {
foreach (array_keys($module_item_elements) as $element) {
list($namespace, $element_name) = views_rss_extract_element_names($element, 'core');
$options['item']['contains'][$namespace]['contains'][$module]['contains'][$element_name] = [
'default' => NULL,
];
}
}
}
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$initial_labels = array(
'' => $this
->t('- None -'),
);
$view_fields_labels = $this->displayHandler
->getFieldLabels();
$view_fields_labels = array_merge($initial_labels, $view_fields_labels);
$item_elements = views_rss_get('item_elements');
if (count($item_elements)) {
foreach ($item_elements as $module => $module_item_elements) {
foreach ($module_item_elements as $element => $definition) {
if (!isset($definition['configurable']) || $definition['configurable']) {
list($namespace, $element_name) = views_rss_extract_element_names($element, 'core');
if (!isset($form['item'][$namespace])) {
$form['item'][$namespace] = array(
'#type' => 'details',
'#title' => t('Item elements : @namespace', array(
'@namespace' => $namespace,
)),
'#description' => t('Select fields containing relevant values for <item> elements in "@namespace" namespace. See <a href="@guide_url">Views RSS documentation</a> for more information.', array(
'@namespace' => $namespace,
'@guide_url' => Url::fromUri('http://drupal.org/node/1344136'),
)),
'#open' => FALSE,
);
}
$default_value = NULL;
if (!empty($this->options['item'][$namespace][$module][$element_name])) {
$default_value = $this->options['item'][$namespace][$module][$element_name];
}
elseif (!empty($definition['group'])) {
$default_value = $this->options['item'][$namespace][$module][$definition['group']][$element_name];
}
$form_item = array(
'#type' => 'select',
'#title' => Xss::filter(isset($definition['title']) ? $definition['title'] : $element_name),
'#description' => Xss::filter(isset($definition['description']) ? $definition['description'] : NULL),
'#options' => $view_fields_labels,
'#default_value' => $default_value,
);
if (!empty($definition['settings form'])) {
$form_item = array_merge($form_item, $definition['settings form']);
if (!empty($definition['settings form']['#options'])) {
$form_item['#options'] = views_rss_map_assoc($definition['settings form']['#options']);
}
}
if (isset($definition['help']) && $definition['help']) {
$form_item['#description'] .= ' ' . Link::fromTextAndUrl('[?]', Url::fromUri($definition['help']), array(
'attributes' => array(
'title' => t('Need more information?'),
),
))
->toString();
}
if (isset($definition['group']) && $definition['group']) {
if (!isset($form['item'][$namespace][$module][$definition['group']])) {
$group_title = !empty($element_groups[$module][$definition['group']]['title']) ? $element_groups[$module][$definition['group']]['title'] : $definition['group'];
$group_description = !empty($element_groups[$module][$definition['group']]['description']) ? $element_groups[$module][$definition['group']]['description'] : NULL;
$form['item'][$namespace][$module][$definition['group']] = array(
'#type' => 'details',
'#title' => Xss::filter($group_title),
'#description' => Xss::filter($group_description),
'#open' => FALSE,
);
}
$form['item'][$namespace][$module][$definition['group']][$element_name] = $form_item;
}
else {
$form['item'][$namespace][$module][$element_name] = $form_item;
}
}
}
}
}
}
public function validate() {
$errors = parent::validate();
if (!\Drupal::moduleHandler()
->moduleExists('views_rss_core')) {
$errors[] = $this
->t('You have to enable <em>Views RSS: Core Elements</em> module to have access to basic feed elements.');
}
else {
if (empty($this->options['item']['core']['views_rss_core']['title']) && empty($this->options['item']['core']['views_rss_core']['description'])) {
$errors[] = $this
->t('You have to configure either <em>title</em> or <em>description</em> core element.');
}
}
return $errors;
}
protected function mapRow($row) {
$rendered_fields = $raw_fields = array();
$field_ids = array_keys($this->view->field);
if (!empty($field_ids)) {
foreach ($field_ids as $field_id) {
$rendered_fields[$field_id] = $this
->getField($row->index, $field_id);
$raw_fields[$field_id] = array();
if (method_exists($this->view->field[$field_id], 'getItems')) {
$raw_fields[$field_id]['items'] = $this->view->field[$field_id]
->getItems($row);
}
}
}
$item_elements = views_rss_get('item_elements');
foreach ($rendered_fields as $field_id => $rendered_field) {
$item = $raw_item = array();
foreach ($item_elements as $module => $module_item_elements) {
foreach ($module_item_elements as $element => $definition) {
list($namespace, $element_name) = views_rss_extract_element_names($element, 'core');
if (!empty($this->options['item'][$namespace][$module][$element_name])) {
$field_name = $this->options['item'][$namespace][$module][$element_name];
}
elseif (!empty($definition['group']) && !empty($this->options['item'][$namespace][$module][$definition['group']][$element_name])) {
$field_name = $this->options['item'][$namespace][$module][$definition['group']][$element_name];
}
else {
$field_name = NULL;
}
if (!empty($rendered_fields[$field_name])) {
$item[$module][$element] = $rendered_fields[$field_name];
}
else {
$item[$module][$element] = NULL;
}
if (!empty($raw_fields[$field_name])) {
$raw_item[$module][$element] = $raw_fields[$field_name];
}
}
}
}
$this->view->views_rss['raw_items'][$row->index] = $raw_item;
return $item;
}
public function render($row) {
static $row_index;
if (!isset($row_index)) {
$row_index = 0;
}
$item_elements = views_rss_get('item_elements');
$item_data = $this
->mapRow($row);
$hook = 'views_rss_preprocess_item';
$modules = \Drupal::moduleHandler()
->getImplementations($hook);
$item_variables = array(
'item' => &$item_data,
'view' => $this->view,
);
if (!empty($this->view->views_rss['raw_items'][$row->index])) {
$item_variables['raw'] = $this->view->views_rss['raw_items'][$row->index];
}
foreach ($modules as $module) {
\Drupal::moduleHandler()
->invoke($module, $hook, array(
$item_variables,
));
}
$item = new \stdClass();
$item->elements = array();
foreach ($item_data as $module => $module_item_elements) {
foreach ($module_item_elements as $element => $value) {
if (is_string($value)) {
$value = htmlspecialchars_decode($value, ENT_QUOTES);
}
$rss_elements = array(
array(
'key' => $element,
'value' => $value,
),
);
if (isset($item_elements[$module][$element]['preprocess functions']) && is_array($item_elements[$module][$element]['preprocess functions'])) {
foreach ($item_elements[$module][$element]['preprocess functions'] as $preprocess_function) {
if (function_exists($preprocess_function)) {
$item_variables = array(
'elements' => &$rss_elements,
'item' => $item_data,
'view' => $this->view,
);
if (!empty($this->view->views_rss['raw_items'][$row->index][$module][$element])) {
$item_variables['raw'] = $this->view->views_rss['raw_items'][$row->index][$module][$element];
}
$preprocess_function($item_variables);
}
}
}
foreach ($rss_elements as $key => $rss_element) {
if (empty($rss_element['value']) && empty($rss_element['attributes'])) {
unset($rss_elements[$key]);
}
}
if (empty($rss_elements)) {
continue;
}
if ($element === 'title' || $element === 'link') {
$rss_element = reset($rss_elements);
$item->{$element} = $rss_element['value'];
}
elseif ($element === 'description') {
$rss_element = reset($rss_elements);
if (is_string($rss_element['value'])) {
$item->{$element} = [
'#markup' => $rss_element['value'],
];
}
else {
$item->{$element} = $rss_element['value'];
}
}
else {
$item->elements = array_merge($item->elements, $rss_elements);
}
}
}
if (function_exists('rdf_get_namespaces') && !empty($this->view->style_plugin->options['namespaces']['add_rdf_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;
}
$build = [
'#theme' => $this
->themeFunctions(),
'#view' => $this->view,
'#options' => $this->options,
'#row' => $item,
'#field_alias' => isset($this->field_alias) ? $this->field_alias : '',
];
return $build;
}
public function getField($index, $field_id) {
if (empty($this->view->style_plugin) || !is_object($this->view->style_plugin) || empty($field_id)) {
return '';
}
return $this->view->style_plugin
->getField($index, $field_id);
}
}