View source
<?php
namespace Drupal\aggregator\Plugin\aggregator\processor;
use Drupal\aggregator\ItemStorageInterface;
use Drupal\aggregator\Plugin\AggregatorPluginSettingsBase;
use Drupal\aggregator\Plugin\ProcessorInterface;
use Drupal\aggregator\FeedInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DefaultProcessor extends AggregatorPluginSettingsBase implements ProcessorInterface, ContainerFactoryPluginInterface {
use ConfigFormBaseTrait;
use UrlGeneratorTrait;
protected $configFactory;
protected $itemQuery;
protected $itemStorage;
protected $dateFormatter;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config, QueryInterface $item_query, ItemStorageInterface $item_storage, DateFormatterInterface $date_formatter) {
$this->configFactory = $config;
$this->itemStorage = $item_storage;
$this->itemQuery = $item_query;
$this->dateFormatter = $date_formatter;
parent::__construct($configuration + $this
->getConfiguration(), $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('entity.query')
->get('aggregator_item'), $container
->get('entity.manager')
->getStorage('aggregator_item'), $container
->get('date.formatter'));
}
protected function getEditableConfigNames() {
return [
'aggregator.settings',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('aggregator.settings');
$processors = $config
->get('processors');
$info = $this
->getPluginDefinition();
$counts = array(
3,
5,
10,
15,
20,
25,
);
$items = array_map(function ($count) {
return $this
->formatPlural($count, '1 item', '@count items');
}, array_combine($counts, $counts));
$intervals = array(
3600,
10800,
21600,
32400,
43200,
86400,
172800,
259200,
604800,
1209600,
2419200,
4838400,
9676800,
);
$period = array_map(array(
$this->dateFormatter,
'formatInterval',
), array_combine($intervals, $intervals));
$period[AGGREGATOR_CLEAR_NEVER] = t('Never');
$form['processors'][$info['id']] = array();
if (isset($form['basic_conf'])) {
$form['processors'][$info['id']] = array(
'#type' => 'details',
'#title' => t('Default processor settings'),
'#description' => $info['description'],
'#open' => in_array($info['id'], $processors),
);
}
$form['processors'][$info['id']]['aggregator_summary_items'] = array(
'#type' => 'select',
'#title' => t('Number of items shown in listing pages'),
'#default_value' => $config
->get('source.list_max'),
'#empty_value' => 0,
'#options' => $items,
);
$form['processors'][$info['id']]['aggregator_clear'] = array(
'#type' => 'select',
'#title' => t('Discard items older than'),
'#default_value' => $config
->get('items.expire'),
'#options' => $period,
'#description' => t('Requires a correctly configured <a href=":cron">cron maintenance task</a>.', array(
':cron' => $this
->url('system.status'),
)),
);
$lengths = array(
0,
200,
400,
600,
800,
1000,
1200,
1400,
1600,
1800,
2000,
);
$options = array_map(function ($length) {
return $length == 0 ? t('Unlimited') : $this
->formatPlural($length, '1 character', '@count characters');
}, array_combine($lengths, $lengths));
$form['processors'][$info['id']]['aggregator_teaser_length'] = array(
'#type' => 'select',
'#title' => t('Length of trimmed description'),
'#default_value' => $config
->get('items.teaser_length'),
'#options' => $options,
'#description' => t('The maximum number of characters used in the trimmed version of content.'),
);
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['items']['expire'] = $form_state
->getValue('aggregator_clear');
$this->configuration['items']['teaser_length'] = $form_state
->getValue('aggregator_teaser_length');
$this->configuration['source']['list_max'] = $form_state
->getValue('aggregator_summary_items');
$this
->setConfiguration($this->configuration);
}
public function process(FeedInterface $feed) {
if (!is_array($feed->items)) {
return;
}
foreach ($feed->items as $item) {
if (empty($item['title'])) {
continue;
}
if (!empty($item['guid'])) {
$values = array(
'fid' => $feed
->id(),
'guid' => $item['guid'],
);
}
elseif ($item['link'] && $item['link'] != $feed->link && $item['link'] != $feed->url) {
$values = array(
'fid' => $feed
->id(),
'link' => $item['link'],
);
}
else {
$values = array(
'fid' => $feed
->id(),
'title' => $item['title'],
);
}
if ($entry = entity_load_multiple_by_properties('aggregator_item', $values)) {
$entry = reset($entry);
}
else {
$entry = entity_create('aggregator_item', array(
'langcode' => $feed
->language()
->getId(),
));
}
if ($item['timestamp']) {
$entry
->setPostedTime($item['timestamp']);
}
$entry
->setTitle(Unicode::truncate($item['title'], 255, TRUE, TRUE));
$entry
->setAuthor(Unicode::truncate($item['author'], 255, TRUE, TRUE));
$entry
->setFeedId($feed
->id());
$entry
->setLink($item['link']);
$entry
->setGuid($item['guid']);
$description = '';
if (!empty($item['description'])) {
$description = $item['description'];
}
$entry
->setDescription($description);
$entry
->save();
}
}
public function delete(FeedInterface $feed) {
if ($items = $this->itemStorage
->loadByFeed($feed
->id())) {
$this->itemStorage
->delete($items);
}
drupal_set_message(t('The news items from %site have been deleted.', array(
'%site' => $feed
->label(),
)));
}
public function postProcess(FeedInterface $feed) {
$aggregator_clear = $this->configuration['items']['expire'];
if ($aggregator_clear != AGGREGATOR_CLEAR_NEVER) {
$age = REQUEST_TIME - $aggregator_clear;
$result = $this->itemQuery
->condition('fid', $feed
->id())
->condition('timestamp', $age, '<')
->execute();
if ($result) {
$entities = $this->itemStorage
->loadMultiple($result);
$this->itemStorage
->delete($entities);
}
}
}
public function getConfiguration() {
return $this->configFactory
->get('aggregator.settings')
->get();
}
public function setConfiguration(array $configuration) {
$config = $this
->config('aggregator.settings');
foreach ($configuration as $key => $value) {
$config
->set($key, $value);
}
$config
->save();
}
}