View source
<?php
namespace Drupal\aggregator_test\Plugin\aggregator\processor;
use Drupal\aggregator\Plugin\AggregatorPluginSettingsBase;
use Drupal\aggregator\Plugin\ProcessorInterface;
use Drupal\aggregator\FeedInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TestProcessor extends AggregatorPluginSettingsBase implements ProcessorInterface, ContainerFactoryPluginInterface {
use ConfigFormBaseTrait;
protected $configFactory;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config) {
$this->configFactory = $config;
parent::__construct($configuration + $this
->getConfiguration(), $plugin_id, $plugin_definition);
}
protected function getEditableConfigNames() {
return [
'aggregator_test.settings',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$processors = $this
->config('aggregator.settings')
->get('processors');
$info = $this
->getPluginDefinition();
$form['processors'][$info['id']] = array(
'#type' => 'details',
'#title' => t('Test processor settings'),
'#description' => $info['description'],
'#open' => in_array($info['id'], $processors),
);
$form['processors'][$info['id']]['dummy_length'] = array(
'#title' => t('Dummy length setting'),
'#type' => 'number',
'#min' => 1,
'#max' => 1000,
'#default_value' => $this->configuration['items']['dummy_length'],
);
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['items']['dummy_length'] = $form_state
->getValue('dummy_length');
$this
->setConfiguration($this->configuration);
}
public function process(FeedInterface $feed) {
foreach ($feed->items as &$item) {
$item['title'] = 'testProcessor' . $item['title'];
}
}
public function delete(FeedInterface $feed) {
$feed->description->value .= rand(0, 10);
}
public function postProcess(FeedInterface $feed) {
$feed->refresh->value *= 2;
$feed
->save();
}
public function getConfiguration() {
return $this->configFactory
->get('aggregator_test.settings')
->get();
}
public function setConfiguration(array $configuration) {
$config = $this
->config('aggregator_test.settings');
foreach ($configuration as $key => $value) {
$config
->set($key, $value);
}
$config
->save();
}
}