You are here

function _entity_rules_add_rules_component_alter in Entity Rules 7

Utility Function to alter the add Rules component form.

This function is restrict the plugin type and to add basic Variables

3 calls to _entity_rules_add_rules_component_alter()
entity_rules_form_entity_rules_add_rule_form_alter in ./entity_rules.module
Implements hook_form_FORM_ID_alter().
entity_rules_form_entity_rules_form_clone_rules_config_alter in ./entity_rules.module
Implements hook_form_FORM_ID_alter().
entity_rules_form_entity_rules_form_edit_rules_config_alter in ./entity_rules.module
Implements hook_form_FORM_ID_alter().

File

./entity_rules.module, line 728
Module file for the Entity Rules.

Code

function _entity_rules_add_rules_component_alter(&$form, $op, $is_conditional) {
  $component_types = _entity_rules_get_component_types_for_op($op);
  if ($component_types && !empty($form['plugin_name']['#options'])) {
    foreach ($form['plugin_name']['#options'] as $key => $value) {
      if (!in_array($key, $component_types)) {
        unset($form['plugin_name']['#options'][$key]);
      }
    }
  }
  if (isset($form['settings']) && empty($form['#entity_rules_altered'])) {
    $form['#entity_rules_altered'] = TRUE;
    $form['settings']['tags'] = array(
      '#type' => 'value',
      '#value' => "entity_rules_{$op}",
    );
    $default_var_items[] = array(
      'type' => 'entity',
      'label' => 'Entity',
      'name' => 'entity',
      'usage' => '10',
      // Parameter var. Why is this not defined as constant in Rules
      'weight' => 0,
    );
    $var_items = array_merge($default_var_items, _entity_rules_get_var_items($op, $is_conditional));
    $item_keys = array();

    // Loop through all the variables to alter the row on the form.
    foreach ($var_items as $var_num => $var_item) {
      if (!isset($form['settings']['vars']['items'][$var_item['name']])) {
        $item_key = $var_num;
      }
      else {
        $item_key = $var_item['name'];
      }
      $item_keys[] = $item_key;
      if (isset($var_item['description'])) {
        $form['settings']['vars']['items'][$item_key]['type']['#description'] = $var_item['description'];
      }
      foreach ($var_item as $prop => $value) {
        if ($prop == 'entity_rules_settings') {
          continue;
        }
        if ($prop == 'weight' || $prop == 'usage' || empty($form['settings']['vars']['items'][$item_key][$prop]['#default_value'])) {
          $form['settings']['vars']['items'][$item_key][$prop]['#default_value'] = $value;
        }
        $form['settings']['vars']['items'][$item_key][$prop]['#disabled'] = TRUE;
      }
      if ($var_item['type'] == 'entity') {
        $entity_types = entity_get_info();
        $entity_options['entity'] = t('Any Entity');
        foreach ($entity_types as $entity_type => $info) {
          if ($info['fieldable']) {

            // Make sure this entity type supports the current operation
            if (in_array($op, array_keys(_entity_rules_get_rule_types($entity_type)))) {
              $entity_options[$entity_type] = $info['label'];
            }
          }
        }

        // If this rule_config is only available for 1 entity type remove "any"
        if (count($entity_options) == 2) {
          unset($entity_options['entity']);
        }
        $form['settings']['vars']['items'][$item_key]['type']['#disabled'] = FALSE;
        $form['settings']['vars']['items'][$item_key]['type']['#options'] = $entity_options;
        $form['settings']['vars']['items'][$item_key]['type']['#description'] = t('Choose which entity type this Rule should be availabe for.');
      }
    }

    // Remove all Var types except those that can be enter via textfield
    $text_field_var_types = array(
      'date',
      'integer',
      'decimal',
      'boolean',
      'text',
    );
    foreach ($form['settings']['vars']['items'] as $item_key => &$var_item) {
      if (!in_array($item_key, $item_keys, TRUE)) {
        $var_item['type']['#description'] = t('If set this variable will availalbe to set per bundle to pass to this Rule.');
        foreach ($var_item['type']['#options'] as $type => $value) {
          if (!in_array($type, $text_field_var_types)) {
            unset($var_item['type']['#options'][$type]);
          }
        }
      }
    }
  }
}