View source
<?php
namespace Drupal\feeds_tamper\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class TamperFormBase extends FormBase {
use TamperFormTrait;
const VAR_TAMPER_ID = 'tamper_id';
const VAR_TAMPER_LABEL = 'label';
const VAR_PLUGIN_CONFIGURATION = 'plugin_configuration';
const VAR_WEIGHT = 'weight';
public static function create(ContainerInterface $container) {
$form = parent::create($container);
$form
->setTamperManager($container
->get('plugin.manager.tamper'));
$form
->setTamperMetaManager($container
->get('feeds_tamper.feed_type_tamper_manager'));
return $form;
}
public function tamperTitle(RouteMatchInterface $route_match) {
$feed_type = $route_match
->getParameter('feeds_feed_type');
$source_field = $route_match
->getParameter('source_field');
$tamper_uuid = $route_match
->getParameter('tamper_uuid');
if ($source_field) {
return $this
->t('Add a tamper plugin to @label : @source', [
'@label' => $feed_type
->label(),
'@source' => $source_field,
]);
}
elseif ($tamper_uuid) {
$tamper_meta = $this->feedTypeTamperManager
->getTamperMeta($feed_type);
$tamper = $tamper_meta
->getTamper($tamper_uuid);
return $this
->t('Edit @label', [
'@label' => $tamper
->getPluginDefinition()['label'],
]);
}
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form[self::VAR_TAMPER_ID] = [
'#type' => 'select',
'#title' => $this
->t('The plugin to add'),
'#options' => $this
->getPluginOptions(),
'#required' => TRUE,
'#default_value' => $this->plugin ? $this->plugin
->getPluginDefinition()['id'] : NULL,
'#ajax' => [
'callback' => '::getPluginForm',
'wrapper' => 'plugin-config',
],
];
$form[self::VAR_PLUGIN_CONFIGURATION] = [
'#type' => 'container',
'#tree' => TRUE,
'#attributes' => [
'id' => [
'plugin-config',
],
],
];
if ($this->plugin) {
$form[self::VAR_PLUGIN_CONFIGURATION][self::VAR_TAMPER_LABEL] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => '255',
'#description' => $this
->t('A useful description of what this plugin is doing.'),
'#required' => TRUE,
'#default_value' => $this->plugin
->getSetting(self::VAR_TAMPER_LABEL) ? $this->plugin
->getSetting(self::VAR_TAMPER_LABEL) : $this->plugin
->getPluginDefinition()['label'],
];
$form[self::VAR_PLUGIN_CONFIGURATION]['description'] = [
'#markup' => $this->plugin
->getPluginDefinition()['description'],
];
$subform_state = SubformState::createForSubform($form[self::VAR_PLUGIN_CONFIGURATION], $form, $form_state);
$form[self::VAR_PLUGIN_CONFIGURATION] = $this->plugin
->buildConfigurationForm($form[self::VAR_PLUGIN_CONFIGURATION], $subform_state);
}
$form[self::VAR_WEIGHT] = [
'#type' => 'hidden',
'#value' => $this
->getWeight(),
];
$cancel_url = Url::fromRoute('entity.feeds_feed_type.tamper', [
'feeds_feed_type' => $this->feedsFeedType
->id(),
]);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
'#button_type' => 'primary',
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $cancel_url,
];
return $form;
}
public function getPluginForm(array $form, FormStateInterface $form_state) {
if (!$this->plugin || !$this->plugin
->getSetting(self::VAR_TAMPER_LABEL)) {
$form[self::VAR_PLUGIN_CONFIGURATION][self::VAR_TAMPER_LABEL]['#value'] = $form[self::VAR_PLUGIN_CONFIGURATION][self::VAR_TAMPER_LABEL]['#default_value'];
}
return $form[self::VAR_PLUGIN_CONFIGURATION];
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!empty($this->plugin)) {
$subform_state = SubformState::createForSubform($form[self::VAR_PLUGIN_CONFIGURATION], $form, $form_state);
$this->plugin
->validateConfigurationForm($form[self::VAR_PLUGIN_CONFIGURATION], $subform_state);
}
}
protected function getPluginOptions() {
$plugin_options = array_map(function ($grouped_plugins) {
$group_options = [];
foreach ($grouped_plugins as $id => $plugin_definition) {
$group_options[$id] = $plugin_definition['label'];
}
return $group_options;
}, $this->tamperManager
->getGroupedDefinitions());
return $plugin_options;
}
protected function prepareConfig($source, FormStateInterface $form_state) {
$config = [
'plugin' => $this->plugin
->getPluginId(),
'source' => $source,
'weight' => $form_state
->getValue(self::VAR_WEIGHT),
'label' => $form_state
->getValue([
self::VAR_PLUGIN_CONFIGURATION,
self::VAR_TAMPER_LABEL,
]),
];
$plugin_config = $form_state
->getValue(self::VAR_PLUGIN_CONFIGURATION);
if ($plugin_config) {
$config += $plugin_config;
}
return $config;
}
protected function getWeight() {
$request = $this
->getRequest();
if ($request->query
->has(self::VAR_WEIGHT)) {
return (int) $request->query
->get(self::VAR_WEIGHT);
}
if ($this->plugin) {
return (int) $this->plugin
->getSetting(self::VAR_WEIGHT);
}
return 0;
}
}