294697a2bc28db06be8117285f2c9315d9d559e6
[yaffs-website] / web / core / modules / aggregator / tests / modules / aggregator_test / src / Plugin / aggregator / processor / TestProcessor.php
1 <?php
2
3 namespace Drupal\aggregator_test\Plugin\aggregator\processor;
4
5 use Drupal\aggregator\Plugin\AggregatorPluginSettingsBase;
6 use Drupal\aggregator\Plugin\ProcessorInterface;
7 use Drupal\aggregator\FeedInterface;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9 use Drupal\Core\Form\ConfigFormBaseTrait;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a default processor implementation.
16  *
17  * Creates lightweight records from feed items.
18  *
19  * @AggregatorProcessor(
20  *   id = "aggregator_test_processor",
21  *   title = @Translation("Test processor"),
22  *   description = @Translation("Test generic processor functionality.")
23  * )
24  */
25 class TestProcessor extends AggregatorPluginSettingsBase implements ProcessorInterface, ContainerFactoryPluginInterface {
26   use ConfigFormBaseTrait;
27
28   /**
29    * Contains the configuration object factory.
30    *
31    * @var \Drupal\Core\Config\ConfigFactoryInterface
32    */
33   protected $configFactory;
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
39     return new static(
40       $configuration,
41       $plugin_id,
42       $plugin_definition,
43       $container->get('config.factory')
44     );
45   }
46
47   /**
48    * Constructs a TestProcessor object.
49    *
50    * @param array $configuration
51    *   A configuration array containing information about the plugin instance.
52    * @param string $plugin_id
53    *   The plugin_id for the plugin instance.
54    * @param mixed $plugin_definition
55    *   The plugin implementation definition.
56    * @param \Drupal\Core\Config\ConfigFactoryInterface $config
57    *   The configuration factory object.
58    */
59   public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config) {
60     $this->configFactory = $config;
61     parent::__construct($configuration + $this->getConfiguration(), $plugin_id, $plugin_definition);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function getEditableConfigNames() {
68     return ['aggregator_test.settings'];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
75     $processors = $this->config('aggregator.settings')->get('processors');
76     $info = $this->getPluginDefinition();
77
78     $form['processors'][$info['id']] = [
79       '#type' => 'details',
80       '#title' => t('Test processor settings'),
81       '#description' => $info['description'],
82       '#open' => in_array($info['id'], $processors),
83     ];
84     // Add some dummy settings to verify settingsForm is called.
85     $form['processors'][$info['id']]['dummy_length'] = [
86       '#title' => t('Dummy length setting'),
87       '#type' => 'number',
88       '#min' => 1,
89       '#max' => 1000,
90       '#default_value' => $this->configuration['items']['dummy_length'],
91     ];
92     return $form;
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
99     $this->configuration['items']['dummy_length'] = $form_state->getValue('dummy_length');
100     $this->setConfiguration($this->configuration);
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function process(FeedInterface $feed) {
107     foreach ($feed->items as &$item) {
108       // Prepend our test string.
109       $item['title'] = 'testProcessor' . $item['title'];
110     }
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function delete(FeedInterface $feed) {
117     // Append a random number, just to change the feed description.
118     $feed->description->value .= rand(0, 10);
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function postProcess(FeedInterface $feed) {
125     // Double the refresh rate.
126     $feed->refresh->value *= 2;
127     $feed->save();
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function getConfiguration() {
134     return $this->configFactory->get('aggregator_test.settings')->get();
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function setConfiguration(array $configuration) {
141     $config = $this->config('aggregator_test.settings');
142     foreach ($configuration as $key => $value) {
143       $config->set($key, $value);
144     }
145     $config->save();
146   }
147
148 }