You are here

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

Class

FilterForm

Namespace

Drupal\feed_import\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $fid = NULL, $setting = NULL) {
  $this->feed = FeedImport::loadFeed($fid);
  $this->setting = $setting;
  $fields = array_keys($this->feed->settings['fields']);
  if (is_null($form_state
    ->get('filter_fields'))) {

    // Save fields to be filtered.
    $form_state
      ->set('filter_fields', $fields);
  }
  $param_field = $this->feed->settings['filter']['options']['param'];
  $help = array();
  $help[0] = t('Filter name') . ': ' . t('A name given by you for this filter.');
  $help[1] = t('Filter function') . ': ' . t('Name of the php function to apply on field value.') . ' ';
  $help[1] .= t('You may also use a static function like this: ClassName::functionName.') . ' ';
  $help[1] .= t('Also check our provided filters in FeedImportFilter class.');
  $help[2] = t('Function params') . ': ' . t('Enter here params (one per line) for php function.') . ' ';
  $help[2] .= t('Enter "@param_field" (without quotes) were you want to be sent field value as parameter.', array(
    '@param_field' => $param_field,
  )) . ' ';
  $help[3] = t('Filtered value is the resulted string of all function calls from top to bottom.');
  $help = implode('<br />', $help);
  $form['help'] = array(
    '#markup' => &$help,
  );
  $v = $form_state
    ->getValues();
  if ($v) {
    foreach ($form_state
      ->get('filter_fields') as $field) {
      $filters = array(
        $field => array(),
      );
      $pos = 0;
      if (!empty($v['table_content'][$field])) {
        foreach ($v['table_content'][$field] as &$filter) {
          $vars = array(
            'name' => $filter['name'],
            'function' => $filter['function'],
            'params' => preg_split('/\\r?\\n/', $filter['params']),
          );
          $filters[$field][$filter['name'] . '-' . $pos] = $this
            ->addNewFilter($pos, $vars);
          $pos++;
        }
      }

      // Add new field.
      if ($form_state
        ->get('filter_action') == 'add' && $form_state
        ->get('action_field') == $field) {
        $vars = array(
          'name' => '',
          'function' => '',
          'params' => array(
            $param_field,
          ),
        );
        $filters[$field][$pos] = $this
          ->addNewFilter($pos, $vars);
      }
      $form['container_' . $field] = $this
        ->addContainer($field, $filters);
    }
  }
  else {
    foreach ($this->feed->settings['fields'] as $field => &$val) {
      $filters = array(
        $field => array(),
      );
      $pos = 0;
      foreach ($val[$this->setting] as $name => &$filter) {
        $vars = array(
          'name' => $name,
          'function' => $filter['function'],
          'params' => $filter['params'],
        );
        $filters[$field][$name] = $this
          ->addNewFilter($pos, $vars);
        $pos++;
      }
      $form['container_' . $field] = $this
        ->addContainer($field, $filters);
    }
  }
  if ($fields) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save filters'),
      '#prefix' => t('Your filters will be saved only after you press the button below.') . '<br />',
    );
  }
  return $form;
}