You are here

function rb_numbers_action_views_sum_form in Rules Bonus Pack 6

Configuration form for 'rb_numbers_action_views_sum'. This is a multistep form!

File

./rb_numbers.module, line 149
Functions for extending number management with Rules.

Code

function rb_numbers_action_views_sum_form($settings, &$form, &$form_state) {

  // Set default values for possibly non-set elements.
  if (isset($form_state['values']['settings'])) {
    $settings += $form_state['values']['settings'];
  }
  $settings += array(
    'view' => '',
    'args' => '',
    'view_fields' => array(),
  );

  // Do form settings for the first step.
  if (empty($settings['view'])) {

    // Build a select list with all Views displays on the site.
    $selectable_displays = array();
    foreach (views_get_all_views() as $view_name => $view) {
      foreach ($view->display as $display_name => $display) {
        $selectable_displays[$view_name . '|' . $display_name] = $view_name . '|' . check_plain($display->display_title);
      }
    }
    $form['settings']['view'] = array(
      '#type' => 'select',
      '#title' => t('View and display to use'),
      '#options' => $selectable_displays,
      '#default_value' => $settings['view'],
      '#description' => t('Select the view you want to use for creating a sum.'),
    );
    $form['settings']['args'] = array(
      '#type' => 'textarea',
      '#default_value' => $settings['args'],
      '#title' => t('View arguments'),
      '#description' => t('Add any arguments you want to send to the view, one per
        line. You may use tokens.'),
    );

    // Hide some form elements in the first step.
    $form['negate']['#access'] = FALSE;
    $form['input_help']['#access'] = FALSE;
    $form['weight']['#access'] = FALSE;

    // Replace the usual submit handlers with a own handler, to allow multi-step.
    $form['submit']['#submit'] = array(
      'rb_numbers_action_views_sum_form_step_submit',
    );
    $form['submit']['#value'] = t('Continue');
  }
  else {

    // This is step two in the configuration – allow for selecting view fields.
    // Build a list of all fields in the selected view.
    $views_settings = explode('|', $settings['view']);
    $view_name = $views_settings[0];
    $display_name = $views_settings[1];
    $view = views_get_view($view_name);
    $selectable_fields = array();
    foreach ($view->display[$display_name]->display_options['fields'] as $field) {
      $selectable_fields[$field[id]] = empty($field['label']) ? $field['id'] : $field['label'];
    }
    $form['settings']['view_fields'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Fields to sum'),
      '#options' => $selectable_fields,
      '#default_value' => $settings['view_fields'],
      '#multiple' => TRUE,
      '#required' => TRUE,
      '#description' => t('The content of the selected fields will be used to
        build a sum.'),
    );
  }
}