class TestProcessor

Same name in other branches
  1. 9 core/modules/aggregator/tests/modules/aggregator_test/src/Plugin/aggregator/processor/TestProcessor.php \Drupal\aggregator_test\Plugin\aggregator\processor\TestProcessor

Defines a default processor implementation.

Creates lightweight records from feed items.

Plugin annotation


@AggregatorProcessor(
  id = "aggregator_test_processor",
  title = @Translation("Test processor"),
  description = @Translation("Test generic processor functionality.")
)

Hierarchy

Expanded class hierarchy of TestProcessor

2 string references to 'TestProcessor'
FeedProcessorPluginTest::testProcess in core/modules/aggregator/tests/src/Functional/FeedProcessorPluginTest.php
Test processing functionality.
TestProcessor::process in core/modules/aggregator/tests/modules/aggregator_test/src/Plugin/aggregator/processor/TestProcessor.php
Processes feed data.

File

core/modules/aggregator/tests/modules/aggregator_test/src/Plugin/aggregator/processor/TestProcessor.php, line 25

Namespace

Drupal\aggregator_test\Plugin\aggregator\processor
View source
class TestProcessor extends AggregatorPluginSettingsBase implements ProcessorInterface, ContainerFactoryPluginInterface {
    use ConfigFormBaseTrait;
    
    /**
     * Contains the configuration object factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('config.factory'));
    }
    
    /**
     * Constructs a TestProcessor object.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin_id for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config
     *   The configuration factory object.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config) {
        $this->configFactory = $config;
        parent::__construct($configuration + $this->getConfiguration(), $plugin_id, $plugin_definition);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getEditableConfigNames() {
        return [
            'aggregator_test.settings',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
        $processors = $this->config('aggregator.settings')
            ->get('processors');
        $info = $this->getPluginDefinition();
        $form['processors'][$info['id']] = [
            '#type' => 'details',
            '#title' => t('Test processor settings'),
            '#description' => $info['description'],
            '#open' => in_array($info['id'], $processors),
        ];
        // Add some dummy settings to verify settingsForm is called.
        $form['processors'][$info['id']]['dummy_length'] = [
            '#title' => t('Dummy length setting'),
            '#type' => 'number',
            '#min' => 1,
            '#max' => 1000,
            '#default_value' => $this->configuration['items']['dummy_length'],
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
        $this->configuration['items']['dummy_length'] = $form_state->getValue('dummy_length');
        $this->setConfiguration($this->configuration);
    }
    
    /**
     * {@inheritdoc}
     */
    public function process(FeedInterface $feed) {
        foreach ($feed->items as &$item) {
            // Prepend our test string.
            $item['title'] = 'testProcessor' . $item['title'];
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete(FeedInterface $feed) {
        // Append a random number, just to change the feed description.
        $feed->description->value .= rand(0, 10);
    }
    
    /**
     * {@inheritdoc}
     */
    public function postProcess(FeedInterface $feed) {
        // Double the refresh rate.
        $feed->refresh->value *= 2;
        $feed->save();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConfiguration() {
        return $this->configFactory
            ->get('aggregator_test.settings')
            ->get();
    }
    
    /**
     * {@inheritdoc}
     */
    public function setConfiguration(array $configuration) {
        $config = $this->config('aggregator_test.settings');
        foreach ($configuration as $key => $value) {
            $config->set($key, $value);
        }
        $config->save();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AggregatorPluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
AggregatorPluginSettingsBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
AggregatorPluginSettingsBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
TestProcessor::$configFactory protected property Contains the configuration object factory.
TestProcessor::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
TestProcessor::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
TestProcessor::delete public function Deletes stored feed data. Overrides ProcessorInterface::delete
TestProcessor::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
TestProcessor::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
TestProcessor::postProcess public function Refreshes feed information. Overrides ProcessorInterface::postProcess
TestProcessor::process public function Processes feed data. Overrides ProcessorInterface::process
TestProcessor::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
TestProcessor::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
TestProcessor::__construct public function Constructs a TestProcessor object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.