Index: modules/userreference/userreference.rules.inc =================================================================== RCS file: modules/userreference/userreference.rules.inc diff -N modules/userreference/userreference.rules.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/userreference/userreference.rules.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,62 @@ + t('Load a referenced user'), + 'arguments' => array( + 'node' => array( + 'type' => 'node', + 'label' => t('Content containing the user reference field'), + ), + ), + 'new variables' => array( + 'referenced_user' => array( + 'type' => 'user', + 'label' => t('Referenced user'), + ), + ), + 'module' => 'CCK', + 'help' => 'Note that if the field has multiple values, only the first user will be loaded.', + ); + return $info; +} + +function userreference_rules_action_load($node, $settings) { + $uid = $node->{$settings['field']}[0]['uid']; + if (isset($uid)) { + $user = user_load(array('uid' => $uid)); + return array('referenced_user' => $user); + } +} + +function userreference_rules_action_load_form($settings, &$form) { + $settings += array('field' => ''); + $options = content_rules_get_field_names_by_type('userreference'); + $form['settings']['field'] = array( + '#type' => 'select', + '#title' => t('Field'), + '#default_value' => $settings['field'], + '#options' => $options, + '#required' => TRUE, + '#disabled' => empty($options), + '#description' => empty($options) ? t('There are no userreference fields defined.') : '', + ); +} + +/** + * Helps upgrading from the workflow-ng action. + * "workflow_ng_action_load_referenced_user" to the equivalent rules action. + */ +function workflow_ng_action_load_referenced_user_upgrade(&$element) { + $element['#name'] = 'userreference_rules_action_load'; +} Index: includes/content.rules.inc =================================================================== RCS file: includes/content.rules.inc diff -N includes/content.rules.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/content.rules.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,306 @@ + t('Populate a field'), + 'arguments' => array( + 'node' => array( + 'type' => 'node', + 'label' => t('Content'), + ), + ), + 'eval input' => array('code'), + 'help' => 'You should make sure that the used field exists in the given content type.', + 'module' => 'CCK', + ); + return $info; +} + +/** + * Action: populate a field. + */ +function content_rules_action_populate_field($node, $settings, $element, &$state) { + // Get information about the field. + $field = content_fields($settings['field_name'], $node->type); + $value = _content_rules_get_field_value($settings, $state); + + if (!empty($field) && is_array($value)) { + $node->$settings['field_name'] = $value; + return array('node' => $node); + } +} + + +/** + * Action "populate a field" configuration form. + * This is a multistep form! + */ +function content_rules_action_populate_field_form($settings, &$form, &$form_state) { + $settings += array('field_name' => '', 'code' => '', 'value' => NULL); + if (empty($settings['field_name'])) { + $form['settings']['field_name'] = array( + '#type' => 'select', + '#title' => t('Field'), + '#options' => content_rules_get_field_names_by_type(), + '#default_value' => $settings['field_name'], + '#description' => t('Select the machine-name of the field.'), + '#required' => TRUE, + ); + // Replace the usual submit handlers with a own handler. + $form['submit']['#submit'] = array('content_rules_action_populate_field_form_step_submit'); + $form['submit']['#value'] = t('Continue'); + } + else { + // Show the fields form here. + module_load_include('inc', 'content', 'includes/content.node_form'); + $field = content_fields($settings['field_name']); + + $form['#node'] = (object)array('type' => '', $settings['field_name'] => $settings['value']); + $form['#field_info'][$field['field_name']] = $field; + // We can't put it into $form['settings'] as this would break AHAH callbacks + $form += (array) content_field_form($form, $form_state, $field); + $form[ $settings['field_name'] ]['#weight'] = 4; + + unset($form['#cache']); + + // Advanced: PHP code. + $form['advanced_options'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced: Specify the fields value with PHP code'), + '#collapsible' => TRUE, + '#collapsed' => empty($settings['code']), + '#weight' => 5, + ); + + $db_info = content_database_info($field); + $columns = array_keys($db_info['columns']); + foreach ($columns as $key => $column) { + $columns[$key] = t("'@column' => value for @column", array('@column' => $column)); + } + $sample = t("return array(\n 0 => array(@columns),\n // You'll usually want to stop here. Provide more values\n // if you want your 'default value' to be multi-valued:\n 1 => array(@columns),\n 2 => ...\n);", array('@columns' => implode(', ', $columns))); + + $form['advanced_options']['code'] = array( + '#type' => 'textarea', + '#title' => t('Code'), + '#default_value' => $settings['code'], + '#rows' => 6, + '#description' => t("Advanced usage only: PHP code that returns a value. Should not include <?php ?> delimiters. If this field is filled out, the value returned by this code will override any value specified above. Expected format:
!sample
Using !link_devel 'devel load' tab on a content page might help you figure out the expected format.", array( + '!sample' => $sample, + '!link_devel' => l("devel.module's", 'http://www.drupal.org/project/devel'), + )), + ); + + // Add this file to be included when the form is built by rules + // as it's needed by CCKs add more button. + // See rules_after_build_include_files(). + $form['#includes'][] = './'. drupal_get_path('module', 'node') .'/node.pages.inc'; + } +} + +function content_rules_action_populate_field_form_step_submit($form, &$form_state) { + $form_state['element']['#settings']['field_name'] = $form_state['values']['settings']['field_name']; +} + +/** + * Validate the chosen value or php code. + */ +function content_rules_action_populate_field_validate($form, &$form_state) { + if (!isset($form_state['element']['#settings']['field_name'])) { + //Just validate the last step. + return; + } + + if (isset($form_state['values']['code']) && ($php = $form_state['values']['code'])) { + if (strpos($php, 'return') === FALSE) { + form_set_error('code', t('You have to return the default value in the expected format.')); + } + } + else { + // Validate the field. + $settings = $form_state['element']['#settings']; + $field = content_fields($settings['field_name']); + $field_types = _content_field_types(); + $function = $field_types[$field['type']]['module'] .'_field'; + if (function_exists($function)) { + $form['#node'] = (object)array('type' => '', $settings['field_name'] => $form_state['values'][$settings['field_name']]); + $items = isset($form['#node']->$field['field_name']) ? $form['#node']->$field['field_name'] : array(); + + //Make sure AHAH 'add more' button isn't sent to the fields + // for processing. + unset($items[$field['field_name'] .'_add_more']); + + $function('validate', $form['#node'], $field, $items, $form, NULL); + content_field('validate', $form['#node'], $field, $items, $form, NULL); + } + } +} + +function content_rules_action_populate_field_submit(&$settings, $form, &$form_state) { + $settings['value'] = array_filter($form_state['values'][$settings['field_name']], 'is_array'); + $settings['code'] = $form_state['values']['code']; + + if (function_exists('rules_action_custom_php_submit')) { + // Support adding variables to the php code, if php module is present. + rules_action_custom_php_submit($settings, $form, $form_state); + } + + // Add all values to the input evaluator, so that textfields / textares can + // make use of it. + $names = array('code'); + + foreach ($settings['value'] as $key => $data) { + foreach ($data as $col => $value) { + if (is_string($value) && $col != '_error_element') { + $names[] = "value|$key|$col"; + } + } + } + $form_state['element']['#info']['eval input'] = $names; +} + + +/** + * Label callback: Improve the label of the action. + */ +function content_rules_action_populate_field_label($settings, $argument_labels) { + return t('Populate @node\'s @field', array('@field' => $settings['field_name']) + $argument_labels); +} + +function workflow_ng_action_populate_field_upgrade(&$element) { + $element['#name'] = 'content_rules_action_populate_field'; + $element['#settings']['code'] = $element['#settings']['default_value_php']; + $element['#settings'][$element['#settings']['field_name']] = array(); + unset($element['#settings']['default_value_php']); +} + + +/** + * Implementation of hook_rules_condition_info(). + */ +function content_rules_condition_info() { + $info = array(); + $info['content_rules_field_has_value'] = array( + 'label' => t('Field has value'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content')), + ), + 'eval input' => array('code'), + 'help' => 'You should make sure that the used field exists in the given content type. The condition returns TRUE, if the selected field has the given value.', + 'module' => 'CCK', + ); + $info['content_rules_field_changed'] = array( + 'label' => t('Field has changed'), + 'arguments' => array( + 'node' => array('type' => 'node', 'label' => t('Content containing changes')), + 'node_unchanged' => array('type' => 'node', 'label' => t('Content not containing changes')), + ), + 'help' => 'You should make sure that the used field exists in the given content type.', + 'module' => 'CCK', + ); + return $info; +} + +/** + * Condition: Check the value of a field. + */ +function content_rules_field_has_value($node, $settings, $element, &$state) { + // Get information about the field. + $field = content_fields($settings['field_name'], $node->type); + $value = _content_rules_get_field_value($settings, $state); + + if (!empty($field) && is_array($value)) { + return $node->$settings['field_name'] == $value; + } + else { + return FALSE; + } +} + + +/** + * Use the same configuration form as the "populate field" action. + */ +function content_rules_field_has_value_form($settings, &$form, &$form_state) { + content_rules_action_populate_field_form($settings, $form, $form_state); +} +function content_rules_field_has_value_validate($form, &$form_state) { + content_rules_action_populate_field_validate($form, $form_state); +} +function content_rules_field_has_value_submit(&$settings, $form, &$form_state) { + content_rules_action_populate_field_submit($settings, $form, $form_state); +} + +function content_rules_field_has_value_label($settings, $argument_labels) { + return t('@node\'s @field has value', array('@field' => $settings['field_name']) + $argument_labels); +} + +/** + * Condition: Check if the field has changed. + */ +function content_rules_field_changed($node1, $node2, $settings) { + // Get information about the field. + $field = content_fields($settings['field_name'], $node1->type); + + return !empty($field) && $node1->$settings['field_name'] != $node2->$settings['field_name']; +} + +function content_rules_field_changed_form($settings, &$form, &$form_state) { + $settings += array('field_name' => ''); + $form['settings']['field_name'] = array( + '#type' => 'select', + '#title' => t('Field'), + '#options' => content_rules_get_field_names_by_type(), + '#default_value' => $settings['field_name'], + '#description' => t('Select the machine-name of the field to look at.'), + '#required' => TRUE, + ); +} + +function content_rules_field_changed_label($settings, $argument_labels) { + return t('@node\'s @field has changed', array('@field' => $settings['field_name']) + $argument_labels); +} + + +/** + * Returns the fields of a given field type only. + * Suitable for using it with #options. + */ +function content_rules_get_field_names_by_type($type = NULL) { + $fields = array(); + foreach (content_fields() as $field) { + if (!isset($type) || $field['type'] == $type) { + $fields[$field['field_name']] = $field['field_name']; + } + } + asort($fields); + return $fields; +} + + +function _content_rules_get_field_value($settings, &$state) { + if ($settings['code']) { + if (function_exists('rules_input_evaluator_php_apply')) { + // Support adding variables to the php code, if php module is present. + $value = rules_input_evaluator_php_apply($settings['code'], $settings['vars'], $state, FALSE); + } + else { + ob_start(); + $value = eval($settings['code']); + ob_end_clean(); + } + } + else { + $value = $settings['value']; + } + return $value; +} Index: modules/nodereference/nodereference.rules.inc =================================================================== RCS file: modules/nodereference/nodereference.rules.inc diff -N modules/nodereference/nodereference.rules.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/nodereference/nodereference.rules.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,60 @@ + t('Load a referenced node'), + 'arguments' => array( + 'node' => array( + 'type' => 'node', + 'label' => t('Content containing the node reference field'), + ), + ), + 'new variables' => array( + 'referenced_node' => array( + 'type' => 'node', + 'label' => t('Referenced content'), + ), + ), + 'module' => 'CCK', + 'help' => 'Note that if the field has multiple values, only the first content node will be loaded.', + ); + return $info; +} + +function nodereference_rules_action_load($node, $settings) { + if ($nid = $node->{$settings['field']}[0]['nid']) { + return array('referenced_node' => node_load(array('nid' => $nid))); + } +} + +function nodereference_rules_action_load_form($settings, &$form) { + $settings += array('field' => ''); + $options = content_rules_get_field_names_by_type('nodereference'); + $form['settings']['field'] = array( + '#type' => 'select', + '#title' => t('Field'), + '#default_value' => $settings['field'], + '#options' => $options, + '#required' => TRUE, + '#disabled' => empty($options), + '#description' => empty($options) ? t('There are no nodereference fields defined.') : '', + ); +} + +/** + * Helps upgrading from the workflow-ng action + * "workflow_ng_action_load_referenced_node" to the equivalent rules action. + */ +function workflow_ng_action_load_referenced_node_upgrade(&$element) { + $element['#name'] = 'nodereference_rules_action_load'; +}