You are here

public function ClassSettingsForm::buildForm in Feed Import 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/ClassSettingsForm.php, line 24

Class

ClassSettingsForm
Form for editing various Feed Importer settings.

Namespace

Drupal\feed_import\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $setting = NULL) {
  $form = parent::buildForm($form, $form_state);

  // Get all settings.
  $this->setting = $setting;
  $feed_settings = $this->entity
    ->getSettings();
  if (!($settings = $this
    ->getClassSettings($this->setting))) {
    $form_state
      ->setRedirectUrl($this->entity
      ->urlInfo('edit-form'));
  }

  // Get setting name.
  $values = $form_state
    ->getValues();
  if (isset($values['name'])) {
    $default = $values['options'];
    $sn = $values['name'];
  }
  else {
    if (!isset($feed_settings[$this->setting]['name'])) {
      $default = array();

      // Use a default setting.
      $first = reset($settings);
      $sn = key($settings);
      $feed_settings[$this->setting] = array(
        'name' => $sn,
        'class' => $first['class'],
        'options' => array(),
      );
    }
    else {
      $sn = $feed_settings[$this->setting]['name'];
      $default = $feed_settings[$this->setting]['options'];
    }
  }

  // Get setting.
  $set =& $settings[$sn];

  // Get setting options.
  $options = array();
  foreach ($settings as $key => &$r) {
    if (empty($r['hidden'])) {
      $options[$key] = $r['name'];
    }
  }
  $form['settings'] = [
    '#type' => 'fieldset',
    '#title' => t('%setting', [
      '%setting' => $this->setting,
    ]),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  ];
  $form['settings'][$this->setting]['class'] = array(
    '#type' => 'value',
    '#value' => $set['class'],
  );
  $form['settings'][$this->setting]['name'] = array(
    '#type' => 'select',
    '#title' => t('Select option'),
    '#options' => $options,
    '#default_value' => $sn,
    '#description' => isset($set['description']) ? $set['description'] : '',
    '#ajax' => array(
      'event' => 'change',
      'callback' => array(
        $this,
        'ajaxForm',
      ),
      'wrapper' => 'feed_import_class_settings',
      'method' => 'replaceWith',
    ),
  );
  $form['#id'] = 'feed_import_class_settings';
  $form['settings'][$this->setting]['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );

  // Get form fields.
  $fields = $this
    ->getClassOptions($set, $settings, $default);

  // Add settings into fieldset.
  $form['settings'][$this->setting]['options'] += $fields;
  return $form;
}