You are here

class TestProcessor in Drupal 8

Same name and namespace 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

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

Namesort descending Modifiers Type Description Overrides
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.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
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. Overrides PluginBase::__construct