Rss.php 2.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @file
 * Contains \Drupal\aggregator\Plugin\views\row\Rss.
 */

namespace Drupal\aggregator\Plugin\views\row;

use Drupal\views\Plugin\views\row\RowPluginBase;
11
use Drupal\views\Annotation\ViewsRow;
12
13
14
15
16
use Drupal\Core\Annotation\Translation;

/**
 * Defines a row plugin which loads an aggregator item and renders as RSS.
 *
17
 * @ViewsRow(
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
 *   id = "aggregator_rss",
 *   theme = "views_view_row_rss",
 *   title = @Translation("Aggregator item"),
 *   help = @Translation("Display the aggregator item using the data from the original source."),
 *   base = {"aggregator_item"},
 *   display_types = {"feed"}
 * )
 */
class Rss extends RowPluginBase {

  /**
   * The table the aggregator item is using for storage.
   *
   * @var string
   */
  public $base_table = 'aggregator_item';

  /**
   * The actual field which is used to identify a aggregator item.
   *
   * @var string
   */
  public $base_field = 'iid';

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();

    $options['item_length'] = array('default' => 'default');

    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, &$form_state) {
    $form['item_length'] = array(
      '#type' => 'select',
      '#title' => t('Display type'),
      '#options' => array(
        'fulltext' => t('Full text'),
        'teaser' => t('Title plus teaser'),
        'title' => t('Title only'),
        'default' => t('Use default RSS settings'),
      ),
      '#default_value' => $this->options['item_length'],
    );
  }

  /**
   * {@inheritdoc}
   */
73
  public function render($row) {
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
    $entity = $row->_entity;

    $item = new \stdClass();
    foreach ($entity->getProperties() as $name => $value) {
      // views_view_row_rss takes care about the escaping.
      $item->{$name} = $value->value;
    }

    $item->elements = array(
      array(
        'key' => 'pubDate',
        // views_view_row_rss takes care about the escaping.
        'value' => gmdate('r', $entity->timestamp->value),
      ),
      array(
        'key' => 'dc:creator',
        // views_view_row_rss takes care about the escaping.
        'value' => $entity->author->value,
      ),
      array(
        'key' => 'guid',
        // views_view_row_rss takes care about the escaping.
        'value' => $entity->guid->value,
        'attributes' => array('isPermaLink' => 'false'),
      ),
    );

101
102
103
104
105
106
107
    $build = array(
      '#theme' => $this->themeFunctions(),
      '#view' => $this->view,
      '#options' => $this->options,
      '#row' => $item,
    );
    return drupal_render($build);
108
109
110
  }

}