You are here

public function DynamicFilterForm::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/DynamicFilterForm.php, line 38
Contains \Drupal\feed_import\Form\DynamicFilterForm

Class

DynamicFilterForm

Namespace

Drupal\feed_import\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $fid = NULL) {
  $this->feed = FeedImport::loadFeed($fid);
  if (is_null($form_state
    ->getValues())) {
    drupal_set_message(t('Dynamic filter functions are created using eval() which can be dangerous for import!'), 'warning');
  }
  $form['description'] = array(
    '#markup' => t('If possible please use a php file including filters (see Filter settings).') . '<br>' . t('Import will not start if any of these functions have syntax errors. If you have problems please check logs.') . '<br>' . '<strong>' . t('If an error occurs on runtime in one of these functions the whole script stops immediately, resulting a broken import process!') . '<br>' . t('Main purpose of these filter functions is testing.') . '<br>' . '</strong>' . '<i>' . t('Ok, eval() can be generally dangerous, but Feed Import assumes that access is enabled only for administrator.') . '</i>',
  );
  $form['fields'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#attributes' => array(
      'id' => 'feed_import_func_fields',
    ),
  );
  $func = 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];
      $func += $this
        ->generateFunctionItem($i, $field);
      unset($field);
    }
    unset($fv);
  }
  else {
    $current_item = -1;
    foreach ($this->feed->settings['functions'] as &$field) {
      $current_item++;
      $func += $this
        ->generateFunctionItem($current_item, $field, TRUE);
    }
    unset($field);
  }
  $trigger = $form_state
    ->getTriggeringElement();
  $cbk = isset($trigger['#name']) ? $trigger['#name'] : '';
  if ($cbk == 'add_new_func') {
    $fv = $form_state
      ->getValue('fields');
    $form_state
      ->set('field_added', FALSE);
    if ($field = Unicode::strtolower($form_state
      ->getValue('func'))) {
      $i = -1;
      $exists = FALSE;
      while (++$i <= $current_item) {
        if (isset($fv['container_' . $i]['name']) && $fv['container_' . $i]['name'] == $field) {
          $exists = TRUE;
          break;
        }
      }
      if (!$exists) {
        $form_state
          ->set('field_added', TRUE);
        $current_item++;
        $func += $this
          ->generateFunctionItem($current_item, array(
          'name' => $field,
          'args' => '',
          'body' => '',
        ));
      }
    }
  }
  elseif (preg_match('/remove_container_([0-9]{1,9})/', $cbk, $match)) {

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

  // Add fields.
  $form['fields'] += $func;
  $form['func'] = array(
    '#type' => 'textfield',
    '#title' => t('Function name'),
    '#attributes' => array(
      'id' => 'func-name',
    ),
    '#description' => t('Name must start with underscore and can contain only alphanumeric chars or underscores.'),
    '#prefix' => '<div id="func-container">',
    '#suffix' => '</div>',
  );
  $form['add_new_func'] = array(
    '#type' => 'button',
    '#name' => 'add_new_func',
    '#value' => t('Add function'),
    '#ajax' => array(
      'event' => 'click',
      'method' => 'replaceWith',
      'callback' => array(
        $this,
        'ajaxAddItem',
      ),
      'wrapper' => 'feed_import_func_fields',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#name' => 'save',
    '#value' => t('Save functions'),
  );

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