View source
<?php
namespace Drupal\feeds\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\feeds\Exception\MissingTargetException;
use Drupal\feeds\Feeds\FeedsSingleLazyPluginCollection;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Plugin\DependentWithRemovalPluginInterface;
use Drupal\feeds\Plugin\Type\LockableInterface;
use Drupal\feeds\Plugin\Type\Target\ConfigurableTargetInterface;
class FeedType extends ConfigEntityBundleBase implements FeedTypeInterface, EntityWithPluginCollectionInterface {
protected $id;
protected $label;
protected $description;
protected $help;
protected $import_period = 3600;
protected $pluginTypes = [
'fetcher',
'parser',
'processor',
];
protected $fetcher = 'http';
protected $parser = 'syndication';
protected $processor = 'entity:node';
protected $fetcher_configuration = [];
protected $parser_configuration = [];
protected $processor_configuration = [];
protected $mappings = [];
protected $custom_sources = [];
protected $sources;
protected $targets;
protected $pluginCollection;
protected $targetPlugins = [];
protected $sourcePlugins = [];
public function __sleep() {
$vars = parent::__sleep();
$vars = array_flip($vars);
unset($vars['pluginCollection']);
$vars = array_flip($vars);
return $vars;
}
public function set($property_name, $value) {
if ($property_name === 'processor' && $this->processor !== $value) {
$this
->removeMappings();
}
return parent::set($property_name, $value);
}
public function getDescription() {
return $this->description;
}
public function getHelp() {
return $this->help;
}
public function isLocked() {
foreach ($this
->getPlugins() as $plugin) {
if ($plugin instanceof LockableInterface && $plugin
->isLocked()) {
return TRUE;
}
}
return FALSE;
}
public function getImportPeriod() {
return $this->import_period;
}
public function setImportPeriod($import_period) {
$this->import_period = (int) $import_period;
}
public function getMappingSources() {
if ($this->sources === NULL) {
$this->sources = $this
->getParser()
->getMappingSources();
$definitions = $this
->getSourcePluginManager()
->getDefinitions();
foreach ($definitions as $definition) {
$class = $definition['class'];
$class::sources($this->sources, $this, $definition);
}
$this
->alter('feeds_sources', $this->sources);
}
return $this->sources + $this->custom_sources;
}
public function getMappingTargets() {
if ($this->targets === NULL) {
$this->targets = [];
$definitions = \Drupal::service('plugin.manager.feeds.target')
->getDefinitions();
foreach ($definitions as $definition) {
$class = $definition['class'];
$class::targets($this->targets, $this, $definition);
}
\Drupal::moduleHandler()
->alter('feeds_targets', $this->targets, $this);
}
return $this->targets;
}
public function getMappings() {
return $this->mappings;
}
public function setMappings(array $mappings) {
$this->mappings = $mappings;
return $this;
}
public function addMapping(array $mapping) {
$this->mappings[] = $mapping;
return $this;
}
public function removeMapping($delta) {
unset($this->mappings[$delta]);
unset($this->targetPlugins[$delta]);
return $this;
}
public function removeMappings() {
$this->mappings = [];
return $this;
}
public function getMappedSources() {
$sources = [];
foreach ($this
->getMappings() as $mapping) {
foreach ($mapping['map'] as $column => $source) {
if ($source === '') {
continue;
}
$sources[$source] = $source;
}
}
return $sources;
}
public function addCustomSource($name, array $source) {
$this->custom_sources[$name] = $source;
return $this;
}
public function getCustomSource($name) {
if (!isset($this->custom_sources[$name])) {
return NULL;
}
return $this->custom_sources[$name];
}
public function customSourceExists($name) {
return isset($this->custom_sources[$name]);
}
public function removeCustomSource($name) {
unset($this->custom_sources[$name]);
return $this;
}
public function getPlugins() {
$plugins = [];
foreach ($this->pluginTypes as $type) {
$plugins[$type] = $this
->getPlugin($type);
}
return $plugins;
}
public function getFetcher() {
return $this
->getPlugin('fetcher');
}
public function getParser() {
return $this
->getPlugin('parser');
}
public function getProcessor() {
return $this
->getPlugin('processor');
}
protected function getPlugin($plugin_type) {
$bags = $this
->getPluginCollections();
return $bags[$plugin_type . '_configuration']
->get($this->{$plugin_type});
}
public function getTargetPlugin($delta) {
if (isset($this->targetPlugins[$delta])) {
return $this->targetPlugins[$delta];
}
$targets = $this
->getMappingTargets();
$target = $this->mappings[$delta]['target'];
if (!isset($targets[$target])) {
throw new MissingTargetException(sprintf('The Feeds target "%s" does not exist.', $target));
}
$id = $targets[$target]
->getPluginId();
$configuration = [];
$configuration['feed_type'] = $this;
$configuration['target_definition'] = $targets[$target];
if (isset($this->mappings[$delta]['settings'])) {
$configuration += $this->mappings[$delta]['settings'];
}
$this->targetPlugins[$delta] = \Drupal::service('plugin.manager.feeds.target')
->createInstance($id, $configuration);
return $this->targetPlugins[$delta];
}
public function getSourcePlugin($source) {
if (!isset($this->sourcePlugins[$source])) {
$sources = $this
->getMappingSources();
if (isset($sources[$source]['id'])) {
$configuration = [
'feed_type' => $this,
'source' => $source,
];
$this->sourcePlugins[$source] = $this
->getSourcePluginManager()
->createInstance($sources[$source]['id'], $configuration);
}
else {
$this->sourcePlugins[$source] = FALSE;
}
}
return $this->sourcePlugins[$source];
}
public function getPluginOptionsList($plugin_type) {
$manager = \Drupal::service("plugin.manager.feeds.{$plugin_type}");
$options = [];
foreach ($manager
->getDefinitions() as $id => $definition) {
$options[$id] = $definition['title'];
}
return $options;
}
public function getPluginCollections() {
if (!isset($this->pluginCollection)) {
$this->pluginCollection = [];
foreach ($this->pluginTypes as $type) {
$this->pluginCollection[$type . '_configuration'] = new FeedsSingleLazyPluginCollection(\Drupal::service("plugin.manager.feeds.{$type}"), $this
->get($type), $this
->get($type . '_configuration'), $this);
}
}
return $this->pluginCollection;
}
public function uri() {
return [
'path' => 'admin/structure/feeds/manage/' . $this
->id(),
'options' => [
'entity_type' => $this->entityType,
'entity' => $this,
],
];
}
public function preSave(EntityStorageInterface $storage_controller, $update = TRUE) {
foreach ($this
->getPlugins() as $type => $plugin) {
$plugin
->onFeedTypeSave($update);
}
foreach ($this->targetPlugins as $delta => $target_plugin) {
if ($target_plugin instanceof ConfigurableTargetInterface) {
$this->mappings[$delta]['settings'] = $target_plugin
->getConfiguration();
}
else {
unset($this->mappings[$delta]['settings']);
}
}
$this->mappings = array_values($this->mappings);
parent::preSave($storage_controller, $update);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
\Drupal::service('plugin.manager.queue_worker')
->clearCachedDefinitions();
\Drupal::queue('feeds_feed_refresh:' . $this
->id())
->createQueue();
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
foreach ($entities as $entity) {
foreach ($entity
->getPlugins() as $plugin) {
$plugin
->onFeedTypeDelete();
}
if ($queue = \Drupal::queue('feeds_feed_refresh:' . $entity
->id())) {
$queue
->deleteQueue();
}
}
\Drupal::service('plugin.manager.queue_worker')
->clearCachedDefinitions();
}
public function toArray() {
$properties = parent::toArray();
$properties['mappings'] = $this->mappings;
return $properties;
}
protected function getSourcePluginManager() {
return \Drupal::service('plugin.manager.feeds.source');
}
protected function alter($type, &$data) {
return \Drupal::moduleHandler()
->alter($type, $data, $this);
}
public function calculateDependencies() {
parent::calculateDependencies();
foreach ($this
->getMappings() as $delta => $mapping) {
try {
$plugin = $this
->getTargetPlugin($delta);
$this
->calculatePluginDependencies($plugin);
} catch (MissingTargetException $e) {
watchdog_exception('feeds', $e);
}
}
return $this;
}
public function onDependencyRemoval(array $dependencies) {
$changed = FALSE;
if (isset($dependencies['module']) && in_array('feeds', $dependencies['module'])) {
return FALSE;
}
foreach ($this
->getMappings() as $delta => $mapping) {
$plugin = $this
->getTargetPlugin($delta);
if ($plugin instanceof DependentWithRemovalPluginInterface) {
if ($plugin
->onDependencyRemoval($dependencies)) {
$this
->removeMapping($delta);
$changed = TRUE;
}
}
}
if ($changed) {
$this
->calculateDependencies();
}
return $changed;
}
}