You are here

function content_rules_action_populate_field_form in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 6.3 includes/content.rules.inc \content_rules_action_populate_field_form()

Action "populate a field" configuration form. This is a multistep form!

1 call to content_rules_action_populate_field_form()
content_rules_field_has_value_form in includes/content.rules.inc
Use the same configuration form as the "populate field" action.

File

includes/content.rules.inc, line 47
Provides basic rules module support.

Code

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,
    );

    // 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.
    $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 the value to set. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the value returned by this code will override any value specified above. Expected format: <pre>!sample</pre>Using <a href="@link_devel">devel.module\'s</a> \'devel load\' tab on a content page might help you figure out the expected format.', array(
        '!sample' => $sample,
        '@link_devel' => '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';
  }
}