You are here

public function FieldsForm::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 FormInterface::buildForm

File

src/Form/FieldsForm.php, line 34
Contains \Drupal\feed_import\Form\FieldsForm

Class

FieldsForm

Namespace

Drupal\feed_import\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $fid = NULL) {
  $this->feed = FeedImport::loadFeed($fid);

  // Set uniq path.
  $form['uniq'] = array(
    '#type' => 'textfield',
    '#default_value' => $this->feed->settings['uniq_path'],
    '#title' => t('Enter path to unique id'),
    '#description' => t('Used to monitor items for updates.'),
  );

  // Fields list
  $form['fields'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'feed_import_path_fields',
    ),
    '#tree' => TRUE,
  );
  $paths = $fields = array();
  $current_item = $form_state
    ->get('current_item');
  if (!is_null($current_item)) {
    $fv = $form_state
      ->getValue('fields');
    for ($i = 0; $i <= $current_item; $i++) {
      if (!isset($fv['container_' . $i])) {
        continue;
      }
      $field =& $fv['container_' . $i];
      $fields[] = $field['field'];
      $paths += $this
        ->generatePathItem($i, $field);
      unset($field);
    }
    unset($fv);
  }
  else {
    $current_item = -1;
    foreach ($this->feed->settings['fields'] as &$field) {
      $current_item++;
      $fields[] = $field['field'];
      $paths += $this
        ->generatePathItem($current_item, $field, TRUE);
    }
    unset($field);
  }
  $fv = $form_state
    ->getValues();
  $trigger = $form_state
    ->getTriggeringElement();
  if ($trigger) {
    if ($trigger['#name'] == 'add_new_item') {
      $form_state
        ->set('field_added', FALSE);
      $field = $fv['add_new_item_mode'] ? 'add_new_item_field' : 'add_new_item_manual';
      if ($field = Unicode::strtolower($fv[$field])) {
        $i = -1;
        $exists = FALSE;
        while (++$i <= $form_state
          ->get('current_item')) {
          if (isset($fv['fields']['container_' . $i]['field']) && $fv['fields']['container_' . $i]['field'] == $field) {
            $exists = TRUE;
            break;
          }
        }
        if (!$exists) {
          $form_state
            ->set('field_added', TRUE);
          $current_item++;
          $paths += $this
            ->generatePathItem($current_item, array(
            'field' => $field,
            'default_value' => '',
            'default_action' => FeedImportProcessor::ACTION_DEFAULT_VALUE,
            'update_mode' => FeedImportProcessor::UPDATE_COMBINE,
            'paths' => '',
          ));
        }
      }
    }
    elseif (preg_match('/remove_container_([0-9]{1,9})/', $trigger['#name'], $match)) {

      // Delete container.
      unset($paths['container_' . $match[1]]);
    }
  }
  $form_state
    ->set('current_item', $current_item);

  // Add fields.
  $form['fields'] += $paths;
  unset($paths);

  // Generate field options.
  $opts = FeedImport::getEntityInfo($this->feed->entity);
  $opts = array_merge($opts->properties, array_keys($opts->fields));
  $opts = array_diff($opts, $fields);
  $opts = array_combine($opts, $opts);

  // Add new field mode.
  $form['add_new_item_mode'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use defined fields'),
    '#default_value' => TRUE,
    '#id' => 'add-new-item-mode',
  );

  // Add entity fields.
  $form['add_new_item_field'] = array(
    '#type' => 'select',
    '#options' => $opts,
    '#title' => t('Select defined field'),
    '#description' => t('Select field name and click "' . t('Add field') . '" button'),
    '#id' => 'add-new-item-field',
    '#states' => array(
      'visible' => array(
        ':input[name=add_new_item_mode]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Manual field.
  $form['add_new_item_manual'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter field name'),
    '#description' => t('Write field name and click "@name" button', array(
      '@name' => t('Add field'),
    )),
    '#id' => 'add-new-item-manual',
    '#states' => array(
      'visible' => array(
        ':input[name=add_new_item_mode]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );

  // Add new field button.
  $form['add_new_item'] = array(
    '#type' => 'button',
    '#name' => 'add_new_item',
    '#value' => t('Add field'),
    '#inline' => TRUE,
    '#ajax' => array(
      'event' => 'click',
      'callback' => array(
        $this,
        'ajaxAddItem',
      ),
      'wrapper' => 'feed_import_path_fields',
      'method' => 'append',
    ),
  );

  // Submit buttons.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save feed'),
  );

  // Add js.
  $form['#attached'] = array(
    'library' => array(
      'feed_import/behaviors',
    ),
  );
  return $form;
}