You are here

public function FeedsRulesProcessor::configForm in Feeds Rules 7

As we do not process any entities directly, we only provide configuration on what rule set/action shall be used.

File

plugins/FeedsRulesProcessor.inc, line 145
Defines the Feeds Rules processor plugin.

Class

FeedsRulesProcessor
Feeds processor plugin to process a rules component for each feed items.

Code

public function configForm(&$form_state) {
  $components = rules_get_components(TRUE, 'action');
  $form['component'] = array(
    '#type' => 'select',
    '#title' => t('Import rules component'),
    '#required' => TRUE,
    '#description' => t('Select a rules action or rule set to be used to process each item of the source. Each parameter of that component can be set in this processor\'s mapping.'),
    '#options' => $components,
    '#default_value' => $this->config['component'],
  );

  // Feeds source mapping.
  $form['source-mapping'] = array(
    '#type' => 'select',
    '#title' => t('Source mapping'),
    '#required' => FALSE,
    '#description' => t('Besides mapping the parsed items to the rules params, you can pass the feeds source object to the rules component.'),
    '#empty_value' => '',
    '#options' => feeds_rules_get_rules_component_params($this->config['component'], array(
      'params',
    ), array(
      'feeds_source',
    )),
    '#default_value' => $this->config['source-mapping'],
  );

  /**
   * Configure the reverse component.
   */
  $form['reverse-component'] = array(
    '#type' => 'fieldset',
    '#title' => t('Reverse component'),
    '#description' => t('This component/action will be called when an imported item is about to be reverted. This way you can make sure to update or deleted data that was related to the imported item.'),
    '#tree' => TRUE,
  );
  $form['reverse-component']['component'] = array(
    '#type' => 'select',
    '#title' => t('Action'),
    '#required' => FALSE,
    '#empty' => TRUE,
    '#description' => t('Select a defined action or rule set'),
    '#empty_value' => '',
    '#options' => $components,
    '#default_value' => $this->config['reverse-component']['component'],
  );

  // If a component is selected show the mapping array.
  if (!empty($this->config['reverse-component']['component'])) {
    $reverse_action = rules_get_cache('comp_' . $this->config['reverse-component']['component']);

    // Stop if component can not be loaded (e.g. component was removed)
    if (!$reverse_action) {
      return $form;
    }
    $reverse_parameter_info = $reverse_action
      ->parameterInfo();
    $form['reverse-component']['mapping'] = array(
      '#type' => 'fieldset',
      '#title' => t('Mapping'),
    );

    // No params, no mapping needed.
    if (empty($reverse_parameter_info)) {
      $form['reverse-component']['mapping']['#description'] = t('This component has no parameters to configure.');
    }
    else {

      // Provide parameters and provided variables, as values for the target
      // "reverse" component.
      $sources = feeds_rules_get_rules_component_params($this->config['component']);

      // Provide the mapping form elements.
      $mapping = isset($this->config['reverse-component']['mapping']) ? $this->config['reverse-component']['mapping'] : array();
      foreach ($reverse_parameter_info as $key => $info) {
        $form['reverse-component']['mapping'][$key] = array(
          '#type' => 'select',
          '#title' => $info['label'],
          '#description' => t('Rules data of type "@type".', array(
            '@type' => $info['type'],
          )),
          '#options' => $sources,
          '#default_value' => isset($mapping[$key]) ? $mapping[$key] : '',
        );
      }
      $form['reverse-component']['mapping']['#description'] = t('Select the mapping for the reverse component. If your import component provided any data, you can reuse that in the mapping too.');
    }
  }
  return $form;
}