You are here

public static function RulesFormsDataElement::inputForm in Rules Forms Support 7

Returns a select list with options including only available elements.

Overrides RulesDataDirectInputFormInterface::inputForm

File

includes/rules_forms.ui.inc, line 143
Defines data type classes for forms and form elements.

Class

RulesFormsDataElement
Defines the form element data type UI.

Code

public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  $options = array();

  // Get the type of attribute being set from the end of the element name.
  // The action name should directly correspond with the attribute it sets
  // for this reason.
  $element_name = $element
    ->getElementName();
  $attributes = explode('_', $element_name);
  $attribute = array_pop($attributes);

  // Loop through each variable and retrieve the options that apply to
  // this field type.
  foreach ($element
    ->availableVariables() as $key => $value) {

    // Populate the options list with only form elements.
    if (!in_array($key, array(
      'site',
      'form',
      'form_state',
      'form_id',
      'user',
    ))) {
      $type = substr($key, 0, strpos($key, ':'));

      // Filter the elements by determining whether this attribute
      // can be applied to them.
      if (isset(self::$attributes[$attribute]) && (self::$attributes[$attribute] == 'all' || in_array($type, self::$attributes[$attribute]))) {
        if (!isset($options[$type])) {
          $options[$type] = array();
        }
        $options[$type][$key] = $value['label'];
      }
    }
  }
  ksort($options);
  $form = parent::defaultForm($name, $info, $settings, 'Form element');
  $form[$name]['#options'] = $options;
  return $form;
}