Commit e4c24df2 authored by Dries's avatar Dries

Issue #1821844 by dawehner, jibran, damiankloip, xjm, ParisLiakos, wamilton,...

Issue #1821844 by dawehner, jibran, damiankloip, xjm, ParisLiakos, wamilton, olli: Aggregator views integration.
parent 6c3dae99
<?php
/**
* @file
* Provides views data for aggregator.module.
*
* @ingroup views_module_handlers
*/
/**
* Implements hook_views_data().
*/
function aggregator_views_data() {
$data = array();
$data['aggregator_item']['table']['group'] = t('Aggregator');
$data['aggregator_item']['table']['base'] = array(
'field' => 'iid',
'title' => t('Aggregator item'),
'help' => t('Aggregator items are imported from external RSS and Atom news feeds.'),
);
$data['aggregator_item']['table']['entity type'] = 'aggregator_item';
$data['aggregator_item']['iid'] = array(
'title' => t('Item ID'),
'help' => t('The unique ID of the aggregator item.'),
'field' => array(
'id' => 'numeric',
),
'argument' => array(
'id' => 'aggregator_iid',
'name field' => 'title',
'numeric' => TRUE,
),
'filter' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['aggregator_item']['title'] = array(
'title' => t('Title'),
'help' => t('The title of the aggregator item.'),
'field' => array(
'id' => 'aggregator_title_link',
'extra' => array('link'),
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string',
),
);
$data['aggregator_item']['link'] = array(
'title' => t('Link'),
'help' => t('The link to the original source URL of the item.'),
'field' => array(
'id' => 'url',
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string',
),
);
$data['aggregator_item']['author'] = array(
'title' => t('Author'),
'help' => t('The author of the original imported item.'),
'field' => array(
'id' => 'aggregator_xss',
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string',
),
);
$data['aggregator_item']['guid'] = array(
'title' => t('GUID'),
'help' => t('The guid of the original imported item.'),
'field' => array(
'id' => 'standard',
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string',
),
);
$data['aggregator_item']['description'] = array(
'title' => t('Body'),
'help' => t('The actual content of the imported item.'),
'field' => array(
'id' => 'aggregator_xss',
'click sortable' => FALSE,
),
'argument' => array(
'id' => 'string',
),
'filter' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
);
$data['aggregator_item']['timestamp'] = array(
'title' => t('Timestamp'),
'help' => t('The date the original feed item was posted. (With some feeds, this will be the date it was imported.)'),
'field' => array(
'id' => 'date',
),
'sort' => array(
'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
'argument' => array(
'id' => 'date',
),
);
$data['aggregator_feed']['table']['group'] = t('Aggregator feed');
$data['aggregator_feed']['table']['base'] = array(
'field' => 'fid',
'title' => t('Aggregator feed'),
);
$data['aggregator_feed']['table']['entity type'] = 'aggregator_feed';
$data['aggregator_feed']['table']['join'] = array(
'aggregator_item' => array(
'left_field' => 'fid',
'field' => 'fid',
),
);
$data['aggregator_feed']['fid'] = array(
'title' => t('Feed ID'),
'help' => t('The unique ID of the aggregator feed.'),
'field' => array(
'id' => 'numeric',
),
'argument' => array(
'id' => 'aggregator_fid',
'name field' => 'title',
'numeric' => TRUE,
),
'filter' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['aggregator_feed']['title'] = array(
'title' => t('Title'),
'help' => t('The title of the aggregator feed.'),
'field' => array(
'id' => 'aggregator_title_link',
'extra' => array('link'),
),
'sort' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string',
),
'argument' => array(
'id' => 'string',
),
);
$data['aggregator_feed']['link'] = array(
'title' => t('Link'),
'help' => t('The link to the source URL of the feed.'),
'field' => array(
'id' => 'url',
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string',
),
);
$data['aggregator_feed']['checked'] = array(
'title' => t('Last checked'),
'help' => t('The date the feed was last checked for new content.'),
'field' => array(
'id' => 'date',
),
'sort' => array(
'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
'argument' => array(
'id' => 'date',
),
);
$data['aggregator_feed']['description'] = array(
'title' => t('Description'),
'help' => t('The description of the aggregator feed.'),
'field' => array(
'id' => 'xss',
'click sortable' => FALSE,
),
'filter' => array(
'id' => 'string',
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
);
$data['aggregator_feed']['modified'] = array(
'title' => t('Last modified'),
'help' => t('The date of the most recent new content on the feed.'),
'field' => array(
'id' => 'date',
),
'sort' => array(
'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
'argument' => array(
'id' => 'date',
),
);
$data['aggregator_category_feed']['table']['join'] = array(
'aggregator_item' => array(
'left_field' => 'fid',
'field' => 'fid',
),
);
$data['aggregator_category']['table']['group'] = t('Aggregator category');
$data['aggregator_category']['table']['join'] = array(
'aggregator_item' => array(
'left_table' => 'aggregator_category_feed',
'left_field' => 'cid',
'field' => 'cid',
),
);
$data['aggregator_category']['cid'] = array(
'title' => t('Category ID'),
'help' => t('The unique ID of the aggregator category.'),
'field' => array(
'id' => 'numeric',
),
'argument' => array(
'id' => 'aggregator_category_cid',
'name field' => 'title',
'numeric' => TRUE,
),
'filter' => array(
'id' => 'aggregator_category_cid',
),
'sort' => array(
'id' => 'standard',
),
);
$data['aggregator_category']['title'] = array(
'title' => t('Category'),
'help' => t('The title of the aggregator category.'),
'field' => array(
'id' => 'aggregator_category',
),
'argument' => array(
'id' => 'string',
),
'sort' => array(
'id' => 'standard',
),
'filter' => array(
'id' => 'string',
),
);
return $data;
}
......@@ -136,7 +136,9 @@ public function label($langcode = NULL) {
* {@inheritdoc}
*/
public function postCreate(EntityStorageControllerInterface $storage_controller) {
$this->timestamp->value = REQUEST_TIME;
if (!isset($this->timestamp->value)) {
$this->timestamp->value = REQUEST_TIME;
}
}
/**
......
<?php
/**
* @file
* Contains \Drupal\aggregator\Plugin\views\argument\CategoryCid.
*/
namespace Drupal\aggregator\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\Numeric;
use Drupal\Component\Annotation\PluginID;
use Drupal\Component\Utility\String;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler to accept an aggregator category id.
*
* @ingroup views_argument_handlers
*
* @PluginID("aggregator_category_cid")
*/
class CategoryCid extends Numeric {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
}
/**
* {@inheritdoc}
*/
function titleQuery() {
$titles = $this->database->query("SELECT title FROM {aggregator_category} where cid IN (:cid)", array(':cid' => $this->value))->fetchCol();
return array_map(function ($title) {
return String::checkPlain($title);
}, $titles);
}
}
<?php
/**
* @file
* Contains \Drupal\aggregator\Plugin\views\argument\Fid.
*/
namespace Drupal\aggregator\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\Numeric;
use Drupal\Component\Annotation\PluginID;
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler to accept an aggregator feed id.
*
* @ingroup views_argument_handlers
*
* @PluginID("aggregator_fid")
*/
class Fid extends Numeric {
/**
* The entity manager service
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManager $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('plugin.manager.entity'));
}
/**
* {@inheritdoc}
*/
function titleQuery() {
$titles = array();
$feeds = $this->entityManager->getStorageController('aggregator_feed')->load($this->value);
foreach ($feeds as $feed) {
$titles[] = String::checkPlain($feed->label());
}
return $titles;
}
}
<?php
/**
* @file
* Contains \Drupal\aggregator\Plugin\views\argument\Iid.
*/
namespace Drupal\aggregator\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\Numeric;
use Drupal\Component\Annotation\PluginID;
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler to accept an aggregator item id.
*
* @ingroup views_argument_handlers
*
* @PluginID("aggregator_iid")
*/
class Iid extends Numeric {
/**
* The entity manager service
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManager $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('plugin.manager.entity'));
}
/**
* {@inheritdoc}
*/
function titleQuery() {
$titles = array();
$items = $this->entityManager->getStorageController('aggregator_item')->load($this->value);
foreach ($items as $feed) {
$titles[] = String::checkPlain($feed->label());
}
return $titles;
}
}
<?php
/**
* @file
* Contains \Drupal\aggregator\Plugin\views\field\Category.
*/
namespace Drupal\aggregator\Plugin\views\field;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Component\Annotation\PluginID;
/**
* Defines a simple renderer that allows linking to an aggregator category.
*
* @ingroup views_field_handlers
*
* @PluginID("aggregator_category")
*/
class Category extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->additional_fields['cid'] = 'cid';
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_category'] = array('default' => FALSE);
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, &$form_state) {
$form['link_to_category'] = array(
'#title' => t('Link this field to its aggregator category page'),
'#description' => t('This will override any other link you have set.'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['link_to_category']),
);
parent::buildOptionsForm($form, $form_state);
}
/**
* Render whatever the data is as a link to the category.
*
* @param string $data
* The XSS safe string for the link text.
* @param object $values
* The values retrieved from the database.
*
* @return data
* Returns string for the link text.
*/
protected function render_link($data, $values) {
$cid = $this->getValue($values, 'cid');
if (!empty($this->options['link_to_category']) && !empty($cid) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = "aggregator/categories/$cid";
}
return $data;
}
/**
* {@inheritdoc}
*/
public function render($values) {
$value = $this->getValue($values);
return $this->render_link($this->sanitizeValue($value), $values);
}
}
<?php
/**
* @file
* Contains \Drupal\aggregator\Plugin\views\field\TitleLink.
*/
namespace Drupal\aggregator\Plugin\views\field;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Component\Annotation\PluginID;
/**
* Defines a field handler that turns an item's title into a clickable link to
* the original source article.
*
* @ingroup views_field_handlers
*
* @PluginID("aggregator_title_link")
*/
class TitleLink extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->additional_fields['link'] = 'link';
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['display_as_link'] = array('default' => TRUE);
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, &$form_state) {
$form['display_as_link'] = array(
'#title' => t('Display as link'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['display_as_link']),
);
parent::buildOptionsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
function render($values) {
$value = $this->getValue($values);
return $this->render_link($this->sanitizeValue($value), $values);
}
/**
* Renders aggregator item's title as link.
*
* @param string $data
* The XSS safe string for the link text.
* @param object $values
* The values retrieved from the database.
*
* @return data
* Returns string for the link text.
*/
protected function render_link($data, $values) {
$link = $this->getValue($values, 'link');
if (!empty($this->options['display_as_link'])) {