TamperAddForm.php in Feeds Tamper 8.2
File
src/Form/TamperAddForm.php
View source
<?php
namespace Drupal\feeds_tamper\Form;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\feeds\FeedTypeInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class TamperAddForm extends TamperFormBase {
protected $sourceField;
public function getFormId() {
return 'feeds_tamper_add_form';
}
private function assertSourceField(FeedTypeInterface $feeds_feed_type, $source_field) {
$sources = $feeds_feed_type
->getMappingSources();
if (!isset($sources[$source_field])) {
throw new NotFoundHttpException();
}
}
protected function preparePlugin($tamper_id = NULL) {
if (empty($tamper_id)) {
return NULL;
}
$meta = $this->feedTypeTamperManager
->getTamperMeta($this->feedsFeedType);
try {
$tamper = $this->tamperManager
->createInstance($tamper_id, [
'source_definition' => $meta
->getSourceDefinition(),
]);
return $tamper;
} catch (PluginException $e) {
$this
->messenger()
->addError($this
->t('The specified plugin is invalid.'));
}
}
public function buildForm(array $form, FormStateInterface $form_state, FeedTypeInterface $feeds_feed_type = NULL, $source_field = NULL) {
$this
->assertSourceField($feeds_feed_type, $source_field);
$this->feedsFeedType = $feeds_feed_type;
$this->sourceField = $source_field;
$this->plugin = $this
->preparePlugin($form_state
->getValue(self::VAR_TAMPER_ID));
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (empty($this->plugin) || $this->plugin
->getPluginId() !== $form_state
->getValue('tamper_id')) {
$form_state
->setRebuild();
return;
}
$config = $this
->prepareConfig($this->sourceField, $form_state);
$tamper_meta = $this->feedTypeTamperManager
->getTamperMeta($this->feedsFeedType);
$tamper_meta
->addTamper($config);
$this->feedsFeedType
->save();
$this
->messenger()
->addStatus($this
->t('Plugin %plugin_label was successfully added to %source.', [
'%plugin_label' => $this->plugin
->getPluginDefinition()['label'],
'%source' => $this->sourceField,
]));
}
}
Classes
Name |
Description |
TamperAddForm |
Provides a form to add a tamper plugin to a feed type. |