You are here

public static function RulesDataUI::selectionForm in Rules 7.2

Provides the selection form for a parameter.

File

ui/ui.data.inc, line 72
Contains data type related forms.

Class

RulesDataUI
Default UI related class for data types.

Code

public static function selectionForm($name, $info, $settings, RulesPlugin $element) {
  if (!isset($settings[$name . ':select'])) {
    $settings[$name . ':select'] = '';
    $vars = $element
      ->availableVariables();

    // Default to variables with the same name as the parameter.
    if (isset($vars[$name])) {
      $settings[$name . ':select'] = $name;
    }
    elseif (count($matches = RulesData::matchingDataSelector($vars, $info, '', 1, FALSE)) == 1) {
      $settings[$name . ':select'] = rules_array_key($matches);
    }
  }
  $form[$name . ':select'] = array(
    '#type' => 'rules_data_selection',
    '#title' => t('Data selector'),
    '#default_value' => $settings[$name . ':select'],
    '#required' => empty($info['optional']),
    '#autocomplete_path' => RulesPluginUI::path($element
      ->root()->name, 'autocomplete' . '/' . $name),
    // Make the autocomplete textfield big enough so that it can display
    // descriptions without word wraps.
    '#size' => 75,
    '#description' => t("The data selector helps you drill down into the data available to Rules. <em>To make entity fields appear in the data selector, you may have to use the condition 'Entity has field' (or 'Entity is of bundle').</em> More useful tips about data selection are available in <a href='@url'>the online documentation</a>.", array(
      '@url' => rules_external_help('data-selection'),
    )),
  );
  $cache = rules_get_cache();
  $form['types_help'] = array(
    '#theme' => 'rules_settings_help',
    '#heading' => t('Data types'),
  );
  if ($info['type'] == '*') {
    $type_labels[] = t('any');
  }
  else {
    $types = is_array($info['type']) ? $info['type'] : array(
      $info['type'],
    );
    $type_labels = array();
    foreach ($types as $type) {
      $type_labels[] = drupal_ucfirst(isset($cache['data_info'][$type]['label']) ? $cache['data_info'][$type]['label'] : $type);
    }
  }
  $form['types_help']['#text'] = format_plural(count($type_labels), 'Select data of the type %types.', 'Select data of the types %types.', array(
    '%types' => implode(', ', $type_labels),
  ));
  if (!empty($info['translatable'])) {
    if (empty($info['custom translation language'])) {
      $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the current interface language.');
    }
    else {
      $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the configured language.');
    }
    $form['translation'] = array(
      '#theme' => 'rules_settings_help',
      '#text' => $text,
      '#heading' => t('Translation'),
    );
  }
  $form['help'] = array(
    '#theme' => 'rules_data_selector_help',
    '#variables' => $element
      ->availableVariables(),
    '#parameter' => $info,
  );

  // Add data processor.
  $settings += array(
    $name . ':process' => array(),
  );
  $form[$name . ':process'] = array();
  RulesDataProcessor::attachForm($form[$name . ':process'], $settings[$name . ':process'], $info, $element
    ->availableVariables());
  return $form;
}