Commit bb54afd8 authored by alexpott's avatar alexpott

Issue #2028039 by ParisLiakos, jsbalsera, undertext, mcrittenden, xeniak,...

Issue #2028039 by ParisLiakos, jsbalsera, undertext, mcrittenden, xeniak, amateescu: Expand ItemInterface (aggregator.module) with methods.
parent f7cabeb6
......@@ -63,21 +63,22 @@ function aggregator_load_feed_items($type, $data = NULL, $limit = 20) {
function template_preprocess_aggregator_item(&$variables) {
$item = $variables['aggregator_item'];
$variables['feed_url'] = check_url($item->link->value);
$variables['feed_title'] = check_plain($item->title->value);
$variables['content'] = aggregator_filter_xss($item->description->value);
$variables['feed_url'] = check_url($item->getLink());
$variables['feed_title'] = check_plain($item->getTitle());
$variables['content'] = aggregator_filter_xss($item->getDescription());
$variables['source_url'] = '';
$variables['source_title'] = '';
if (isset($item->ftitle) && isset($item->fid->value)) {
$variables['source_url'] = url("aggregator/sources/$item->fid->value");
$fid = $item->getFeedId();
if (isset($item->ftitle) && $fid !== NULL) {
$variables['source_url'] = url('aggregator/sources/' . $fid);
$variables['source_title'] = check_plain($item->ftitle);
}
if (date('Ymd', $item->timestamp->value) == date('Ymd')) {
$variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp->value)));
if (date('Ymd', $item->getPostedTime()) == date('Ymd')) {
$variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->getPostedTime())));
}
else {
$variables['source_date'] = format_date($item->timestamp->value, 'medium');
$variables['source_date'] = format_date($item->getPostedTime(), 'medium');
}
$variables['categories'] = array();
......@@ -185,18 +186,18 @@ function template_preprocess_aggregator_summary_items(&$variables) {
function template_preprocess_aggregator_summary_item(&$variables) {
$item = $variables['aggregator_item'];
$variables['url'] = l(check_plain($item->label()), check_url(url($item->link->value, array('absolute' => TRUE))), array(
$variables['url'] = l(check_plain($item->label()), check_url(url($item->getLink(), array('absolute' => TRUE))), array(
'attributes' => array(
'class' => array('feed-item-url',),
'class' => array('feed-item-url'),
),
));
$variables['age'] = array(
'#theme' => 'datetime',
'#attributes' => array(
'datetime' => format_date($item->timestamp->value, 'html_datetime', '', 'UTC'),
'class' => array('feed-item-age'),
'datetime' => format_date($item->getPostedTime(), 'html_datetime', '', 'UTC'),
'class' => array('feed-item-age',),
),
'#text' => t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp->value))),
'#text' => t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->getPostedTime()))),
'#html' => TRUE,
);
}
......
......@@ -34,90 +34,6 @@
*/
class Item extends ContentEntityBase implements ItemInterface {
/**
* The feed item ID.
*
* @todo rename to id.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $iid;
/**
* The feed ID.
*
* @todo rename to feed_id.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $fid;
/**
* Title of the feed item.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $title;
/**
* The feed language code.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $langcode;
/**
* Link to the feed item.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $link;
/**
* Author of the feed item.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $author;
/**
* Body of the feed item.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $description;
/**
* Posted date of the feed item, as a Unix timestamp.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $timestamp;
/**
* Unique identifier for the feed item.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $guid;
/**
* {@inheritdoc}
*/
public function init() {
parent::init();
// We unset all defined properties, so magic getters apply.
unset($this->iid);
unset($this->fid);
unset($this->title);
unset($this->author);
unset($this->description);
unset($this->guid);
unset($this->link);
unset($this->timestamp);
}
/**
* Implements Drupal\Core\Entity\EntityInterface::id().
*/
......@@ -138,8 +54,8 @@ public function label($langcode = NULL) {
public function postCreate(EntityStorageControllerInterface $storage_controller) {
parent::postCreate($storage_controller);
if (!isset($this->timestamp->value)) {
$this->timestamp->value = REQUEST_TIME;
if ($this->getPostedTime() === NULL) {
$this->setPostedTime(REQUEST_TIME);
}
}
......@@ -214,4 +130,101 @@ public static function baseFieldDefinitions($entity_type) {
return $fields;
}
/**
* @inheritdoc
*/
public function getFeedId() {
return $this->get('fid')->value;
}
/**
* @inheritdoc
*/
public function setFeedId($fid) {
return $this->set('fid', $fid);
}
/**
* @inheritdoc
*/
public function getTitle() {
return $this->get('title')->value;
}
/**
* @inheritdoc
*/
public function setTitle($title) {
return $this->set('title', $title);
}
/**
* @inheritdoc
*/
public function getLink() {
return $this->get('link')->value;
}
/**
* @inheritdoc
*/
public function setLink($link) {
return $this->set('link', $link);
}
/**
* @inheritdoc
*/
public function getAuthor() {
return $this->get('author')->value;
}
/**
* @inheritdoc
*/
public function setAuthor($author) {
return $this->set('author', $author);
}
/**
* @inheritdoc
*/
public function getDescription() {
return $this->get('description')->value;
}
/**
* @inheritdoc
*/
public function setDescription($description) {
return $this->set('description', $description);
}
/**
* @inheritdoc
*/
public function getPostedTime() {
return $this->get('timestamp')->value;
}
/**
* @inheritdoc
*/
public function setPostedTime($timestamp) {
return $this->set('timestamp', $timestamp);
}
/**
* @inheritdoc
*/
public function getGuid() {
return $this->get('guid')->value;
}
/**
* @inheritdoc
*/
public function setGuid($guid) {
return $this->set('guid', $guid);
}
}
......@@ -14,4 +14,134 @@
*/
interface ItemInterface extends ContentEntityInterface {
/**
* Returns the feed id of aggregator item.
*
* @return int
* The feed id.
*/
public function getFeedId();
/**
* Sets the feed id of aggregator item.
*
* @param int $fid
* The feed id
*
* @return \Drupal\aggregator\ItemInterface
* The called feed item entity.
*/
public function setFeedId($fid);
/**
* Returns the title of the feed item.
*
* @return string
* The title of the feed item.
*/
public function getTitle();
/**
* Sets the title of the feed item.
*
* @param string $title
* The title of the feed item.
*
* @return \Drupal\aggregator\ItemInterface
* The called feed item entity.
*/
public function setTitle($title);
/**
* Returns the link to the feed item.
*
* @return string
* The link to the feed item.
*/
public function getLink();
/**
* Sets the link to the feed item.
*
* @param string $link
* The link to the feed item.
* @return \Drupal\aggregator\ItemInterface
* The called feed item entity.
*/
public function setLink($link);
/**
* Returns the author of the feed item.
*
* @return string
* The author of the feed item.
*/
public function getAuthor();
/**
* Sets the author of the feed item.
*
* @param string $author
* The author name of the feed item.
* @return \Drupal\aggregator\ItemInterface
* The called feed item entity.
*/
public function setAuthor($author);
/**
* Returns the body of the feed item.
*
* @return string
* The body of the feed item.
*/
public function getDescription();
/**
* Sets the body of the feed item.
*
* @param string $description
* The body of the feed item.
*
* @return \Drupal\aggregator\ItemInterface
* The called feed item entity.
*/
public function setDescription($description);
/**
* Returns the posted date of the feed item, as a Unix timestamp.
*
* @return int
* The posted date of the feed item, as a Unix timestamp.
*/
public function getPostedTime();
/**
* Sets the posted date of the feed item, as a Unix timestamp.
*
* @param int $timestamp
* The posted date of the feed item, as a Unix timestamp.
*
* @return \Drupal\aggregator\ItemInterface
* The called feed item entity.
*/
public function setPostedTime($timestamp);
/**
* Returns the unique identifier for the feed item.
*
* @return string
* The unique identifier for the feed item.
*/
public function getGuid();
/**
* Sets the unique identifier for the feed item.
*
* @param string $guid
* The unique identifier for the feed item.
*
* @return \Drupal\aggregator\ItemInterface
* The called feed item entity.
*/
public function setGuid($guid);
}
......@@ -50,7 +50,7 @@ public function deleteCategories(array $entities) {
* {@inheritdoc}
*/
public function saveCategories(Item $item) {
$result = $this->database->query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $item->fid->value));
$result = $this->database->query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $item->getFeedId()));
foreach ($result as $category) {
$this->database->merge('aggregator_category_item')
->key(array(
......
......@@ -171,17 +171,17 @@ public function process(Feed $feed) {
$entry = entity_create('aggregator_item', array('langcode' => $feed->language()->id));
}
if ($item['timestamp']) {
$entry->timestamp->value = $item['timestamp'];
$entry->setPostedTime($item['timestamp']);
}
// Make sure the item title and author fit in the 255 varchar column.
$entry->title->value = truncate_utf8($item['title'], 255, TRUE, TRUE);
$entry->author->value = truncate_utf8($item['author'], 255, TRUE, TRUE);
$entry->setTitle(truncate_utf8($item['title'], 255, TRUE, TRUE));
$entry->setAuthor(truncate_utf8($item['author'], 255, TRUE, TRUE));
$entry->fid->value = $feed->id();
$entry->link->value = $item['link'];
$entry->description->value = $item['description'];
$entry->guid->value = $item['guid'];
$entry->setFeedId($feed->id());
$entry->setLink($item['link']);
$entry->setDescription($item['description']);
$entry->setGuid($item['guid']);
$entry->save();
}
}
......
......@@ -41,7 +41,7 @@ public function testProcess() {
$this->updateFeedItems($feed);
foreach ($feed->items as $iid) {
$item = entity_load('aggregator_item', $iid);
$this->assertTrue(strpos($item->title->value, 'testProcessor') === 0);
$this->assertTrue(strpos($item->getTitle(), 'testProcessor') === 0);
}
}
......
......@@ -101,13 +101,13 @@ public function testAggregatorItemView() {
// Ensure that the rendering of the linked title works as expected.
foreach ($view->result as $row) {
$iid = $view->field['iid']->getValue($row);
$expected_link = l($items[$iid]->title->value, $items[$iid]->link->value, array('absolute' => TRUE));
$expected_link = l($items[$iid]->getTitle(), $items[$iid]->getLink(), array('absolute' => TRUE));
$this->assertEqual($view->field['title']->advancedRender($row), $expected_link, 'Ensure the right link is generated');
$expected_author = aggregator_filter_xss($items[$iid]->author->value);
$expected_author = aggregator_filter_xss($items[$iid]->getAuthor());
$this->assertEqual($view->field['author']->advancedRender($row), $expected_author, 'Ensure the author got filtered');
$expected_description = aggregator_filter_xss($items[$iid]->description->value);
$expected_description = aggregator_filter_xss($items[$iid]->getDescription());
$this->assertEqual($view->field['description']->advancedRender($row), $expected_description, 'Ensure the author got filtered');
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment