You are here

EditFeedForm.php in Feed Import 8

File

src/Form/EditFeedForm.php
View source
<?php

namespace Drupal\feed_import\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\feed_import_base\FeedImport;

/**
 * Feed Importer edit form.
 */
class EditFeedForm extends FormBase {

  /**
   * The feed being edited.
   *
   * @var object
   */
  protected $feed;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'feed_import_edit';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $feed_importer = NULL) {
    $this->feed = FeedImport::loadFeed($fid);
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Feed name'),
      '#maxlength' => 64,
      '#description' => t('This usually is source name.'),
      '#default_value' => $this->feed ? $this->feed->name : NULL,
      '#required' => TRUE,
    );
    $form['entity'] = array(
      '#type' => 'select',
      '#options' => FeedImport::getAllEntities(),
      '#default_value' => $this->feed->entity,
      '#title' => t('Entity name'),
      '#description' => t('Entity where you want to import content. Ex: node, user, ...'),
      '#maxlength' => 64,
      '#required' => TRUE,
      '#disabled' => !empty($this->feed->settings['fields']),
    );
    $form['cron_import'] = array(
      '#type' => 'checkbox',
      '#title' => t('Import at cron'),
      '#default_value' => $this->feed->cron_import,
      '#description' => t('Check this if you want to import feed items when cron runs.'),
    );
    $form['feed'] = array(
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#title' => t('Delete protection'),
      '#description' => t('Reschedule items if one of the below conditions is met.') . ' ' . t('This is useful for cron imported items.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['feed']['protect_on_invalid_source'] = array(
      '#type' => 'checkbox',
      '#title' => 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' => t('Total number of items is less than'),
      '#default_value' => $this->feed->settings['feed']['protect_on_fewer_items'],
      '#description' => t('You can also use a percentage by appending %. Percentage is reported to items count of last import.') . ' ' . t('Use 0 to ignore this setting.'),
    );
    $form['preprocess'] = array(
      '#type' => 'textfield',
      '#title' => t('Pre-process callback'),
      '#description' => t('You can use a pre-process function before the feed is imported in order to make some changes to configuration.'),
      '#default_value' => isset($this->feed->settings['preprocess']) ? $this->feed->settings['preprocess'] : '',
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save feed'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $v = $form_state
      ->getValues();
    $this->feed->name = $v['name'];
    $this->feed->entity = $v['entity'];
    $this->feed->cron_import = $v['cron_import'];
    $this->feed->settings['feed'] = $v['feed'];
    $this->feed->settings['preprocess'] = $v['preprocess'];
    if (FeedImport::saveFeed($this->feed)) {
      drupal_set_message(t('Feed saved'));
      $form_state
        ->setRedirect('feed_import.admin_edit', [
        'fid' => $this->feed->machine_name,
      ]);
    }
    else {
      drupal_set_message(t('Error saving feed'), 'error');
    }
  }

}

Classes

Namesort descending Description
EditFeedForm Feed Importer edit form.