View source
<?php
namespace Drupal\feed_import\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\feed_import_base\FeedImport;
class FeedImporterAddForm extends EntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$feed_importer = $this->entity;
if ($this->operation === 'edit') {
$form['#title'] = $this
->t('Edit Feed Importer: @name', [
'@name' => $feed_importer->name,
]);
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Feed Importer Name'),
'#maxlength' => 255,
'#description' => $this
->t('This usually is source name.'),
'#default_value' => $feed_importer->name,
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#default_value' => $feed_importer->id,
'#disabled' => !$feed_importer
->isNew(),
'#machine_name' => [
'source' => [
'label',
],
'exists' => '\\Drupal\\feed_import_base\\FeedImport::loadFeed',
],
];
$settings = $feed_importer
->getSettings();
$form['entity'] = [
'#type' => 'select',
'#options' => FeedImport::getAllEntities(),
'#default_value' => 'node',
'#title' => $this
->t('Entity name'),
'#description' => $this
->t('Entity where you want to import content. Ex: node, user, ...'),
'#maxlength' => 64,
'#required' => TRUE,
'#disabled' => !empty($settings['fields']),
];
$form['cron_import'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Import at cron'),
'#default_value' => $feed_importer
->getCron(),
'#description' => $this
->t('Check this if you want to import feed items when cron runs.'),
];
if ($this->operation === 'edit') {
$form['feed'] = [
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => $this
->t('Delete protection'),
'#description' => $this
->t('Reschedule items if one of the below conditions is met. This is useful for cron imported items.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['feed']['protect_on_invalid_source'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Source file or network is unavailable'),
'#default_value' => $this->feed->settings['feed']['protect_on_invalid_source'],
);
$form['feed']['protect_on_fewer_items'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Total number of items is less than'),
'#default_value' => $this->feed->settings['feed']['protect_on_fewer_items'],
'#description' => $this
->t('You can also use a percentage by appending %. Percentage is reported to items count of last import. Use 0 to ignore this setting.'),
);
$form['preprocess'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Pre-process callback'),
'#description' => $this
->t('You can use a pre-process function before the feed is imported in order to make some changes to configuration.'),
'#default_value' => $feed_importer
->getPreprocessor(),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Save feed'),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$v = $form_state
->getValues();
$feed = (object) FeedImport::getEmptyFeed();
$feed->machine_name = $v['machine_name'];
$feed->settings['hashes']['options']['group'] = $v['machine_name'];
$feed->name = $v['name'];
$feed->entity = $v['entity'];
$feed->cron_import = $v['cron_import'];
if (FeedImport::saveFeed($feed)) {
drupal_set_message(t('Feed saved'));
$form_state
->setRedirect('feed_import.admin_edit', [
'fid' => $feed->machine_name,
]);
}
else {
drupal_set_message(t('Error saving feed'), 'error');
}
}
}