Commit fd655c70 authored by catch's avatar catch

Issue #1905870 by Berdir: Make feeds/feed items multilingual.

parent 2171aedc
......@@ -133,6 +133,13 @@ function aggregator_schema() {
'default' => '',
'description' => 'Title of the feed.',
),
'langcode' => array(
'description' => 'The {language}.langcode of this feed.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
),
'url' => array(
'type' => 'text',
'not null' => TRUE,
......@@ -232,6 +239,13 @@ function aggregator_schema() {
'default' => '',
'description' => 'Title of the feed item.',
),
'langcode' => array(
'description' => 'The {language}.langcode of this feed item.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
),
'link' => array(
'type' => 'text',
'not null' => TRUE,
......@@ -293,3 +307,31 @@ function aggregrator_update_8000() {
'aggregator_category_selector' => 'source.category_selector',
));
}
/**
* Adds the langcode field to {aggregator_item} and {aggregator_feed}.
*/
function aggregator_update_8001() {
// Add the field.
db_add_field('aggregator_feed', 'langcode', array(
'description' => 'The {language}.langcode of this feed.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
));
db_add_field('aggregator_item', 'langcode', array(
'description' => 'The {language}.langcode of this feed item.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
));
// Set langcode to LANGUAGE_DEFAULT.
db_update('aggregator_feed')
->fields(array('langcode' => LANGUAGE_DEFAULT))
->execute();
db_update('aggregator_item')
->fields(array('langcode' => LANGUAGE_DEFAULT))
->execute();
}
......@@ -49,7 +49,7 @@ function aggregator_aggregator_process($feed) {
$entry = reset($entry);
}
else {
$entry = entity_create('aggregator_item', array());
$entry = entity_create('aggregator_item', array('langcode' => $feed->language()->langcode));
}
if ($item['timestamp']) {
$entry->timestamp->value = $item['timestamp'];
......
......@@ -30,6 +30,14 @@ public function form(array $form, array &$form_state, EntityInterface $feed) {
'#description' => t('The name of the feed (or the name of the website providing the feed).'),
'#required' => TRUE,
);
$form['langcode'] = array(
'#title' => t('Language'),
'#type' => 'language_select',
'#default_value' => $feed->language()->langcode,
'#languages' => LANGUAGE_ALL,
);
$form['url'] = array(
'#type' => 'url',
'#title' => t('URL'),
......
......@@ -127,6 +127,11 @@ public function baseFieldDefinitions() {
'description' => t('The title of the feed.'),
'type' => 'string_field',
);
$fields['langcode'] = array(
'label' => t('Language code'),
'description' => t('The feed language code.'),
'type' => 'language_field',
);
$fields['url'] = array(
'label' => t('URL'),
'description' => t('The URL to the feed.'),
......
......@@ -87,6 +87,11 @@ public function baseFieldDefinitions() {
'description' => t('The title of the feed item.'),
'type' => 'string_field',
);
$fields['langcode'] = array(
'label' => t('Language code'),
'description' => t('The feed item language code.'),
'type' => 'language_field',
);
$fields['link'] = array(
'label' => t('Link'),
'description' => t('The link of the feed item.'),
......
......@@ -50,6 +50,13 @@ class Feed extends EntityNG implements ContentEntityInterface {
*/
public $title;
/**
* The feed language code.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $langcode;
/**
* URL to the feed.
*
......
......@@ -56,6 +56,13 @@ class Item extends EntityNG implements ContentEntityInterface {
*/
public $title;
/**
* The feed language code.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $langcode;
/**
* Link to the feed item.
*
......
......@@ -43,14 +43,16 @@ function setUp() {
* @param $feed_url
* (optional) If given, feed will be created with this URL, otherwise
* /rss.xml will be used. Defaults to NULL.
* @param array $edit
* Array with additional form fields.
*
* @return \Drupal\aggregator\Plugin\Core\Entity\Feed $feed
* Full feed object if possible.
*
* @see getFeedEditArray()
*/
function createFeed($feed_url = NULL) {
$edit = $this->getFeedEditArray($feed_url);
function createFeed($feed_url = NULL, array $edit = array()) {
$edit = $this->getFeedEditArray($feed_url, $edit);
$this->drupalPost('admin/config/services/aggregator/add/feed', $edit, t('Save'));
$this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), format_string('The feed !name has been added.', array('!name' => $edit['title'])));
......@@ -76,11 +78,13 @@ function deleteFeed(Feed $feed) {
* @param $feed_url
* (optional) If given, feed will be created with this URL, otherwise
* /rss.xml will be used. Defaults to NULL.
* @param array $edit
* Array with additional form fields.
*
* @return
* A feed array.
*/
function getFeedEditArray($feed_url = NULL) {
function getFeedEditArray($feed_url = NULL, array $edit = array()) {
$feed_name = $this->randomName(10);
if (!$feed_url) {
$feed_url = url('rss.xml', array(
......@@ -88,7 +92,7 @@ function getFeedEditArray($feed_url = NULL) {
'absolute' => TRUE,
));
}
$edit = array(
$edit += array(
'title' => $feed_name,
'url' => $feed_url,
'refresh' => '900',
......
<?php
/**
* @file
* Contains \Drupal\aggregator\Tests\FeedLanguageTest.
*/
namespace Drupal\aggregator\Tests;
use Drupal\Core\Language\Language;
/**
* Tests aggregator feeds in multiple languages.
*/
class FeedLanguageTest extends AggregatorTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('language');
/**
* List of langcodes.
*
* @var array
*/
protected $langcodes = array();
public static function getInfo() {
return array(
'name' => 'Multilingual feeds',
'description' => 'Checks creating of feeds in multiple languages',
'group' => 'Aggregator',
);
}
public function setUp() {
parent::setUp();
// Create test languages.
$this->langcodes = array(language_load('en'));
for ($i = 1; $i < 3; ++$i) {
$language = new Language(array(
'langcode' => 'l' . $i,
'name' => $this->randomString(),
));
language_save($language);
$this->langcodes[$i] = $language;
}
}
/**
* Tests creation of feeds with a language.
*/
public function testFeedLanguage() {
$feeds = array();
// Create feeds.
$feeds[1] = $this->createFeed(NULL, array('langcode' => $this->langcodes[1]->langcode));
$feeds[2] = $this->createFeed(NULL, array('langcode' => $this->langcodes[2]->langcode));
// Make sure that the language has been assigned.
$this->assertEqual($feeds[1]->language()->langcode, $this->langcodes[1]->langcode);
$this->assertEqual($feeds[2]->language()->langcode, $this->langcodes[2]->langcode);
// Create example nodes to create feed items from and then update the feeds.
$this->createSampleNodes();
$this->cronRun();
// Loop over the created feed items and verify that their language matches
// the one from the feed.
foreach ($feeds as $feed) {
$items = entity_load_multiple_by_properties('aggregator_item', array('fid' => $feed->id()));
$this->assertTrue(count($items) > 0, 'Feed items were created.');
foreach ($items as $item) {
$this->assertEqual($item->language()->langcode, $feed->language()->langcode);
}
}
}
}
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