You are here

function cck_allowed_values_form in Content Construction Kit (CCK) 7.3

Add fields to allowed values form to allow users to input a function or a PHP snippet that will return the allowed values.

1 call to cck_allowed_values_form()
cck_form_alter in ./cck.module
Add fields to allowed values form to allow users to input a function or a PHP snippet that will return the allowed values.

File

./cck.module, line 56
Allows administrators to use php code snippets to define allowed values or default values. The snippets are stored in a database table and retrieved in callback functions.

Code

function cck_allowed_values_form(&$form, $form_state, $field) {
  $php_code = cck_field_get_setting('allowed_values_php', 'field', $field);
  $allowed_values_function = $form['field']['settings']['allowed_values_function']['#value'];
  if (!empty($php_code)) {
    $allowed_values_function = 'cck_allowed_values_php';
  }

  // Add a field where users can specify some PHP
  // code that will return the allowed values list.
  $form['field']['settings']['allowed_values_php'] = array(
    '#access' => user_access('Use PHP input for field settings (dangerous - grant with care)'),
    '#type' => 'textarea',
    '#title' => t('Allowed values PHP code'),
    '#default_value' => $php_code,
    '#description' => t('Advanced usage only: PHP code that returns an array of allowed values. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the value returned by this code will be used as the response to the function <strong>cck_allowed_values_php</strong> and will override any other allowed values list or function specified above. Expected format: <pre>!sample</pre>', array(
      '!sample' => t("return array(\n  value_1 => label_1,\n  value_2 => label_2\n  ...\n);"),
    )),
    '#weight' => 5,
  );

  // Add a field where users can specify a function
  // to return the allowed values list.
  $form['field']['settings']['allowed_values_function'] = array(
    '#type' => 'textfield',
    '#title' => t('Allowed values function'),
    '#default_value' => $allowed_values_function,
    '#description' => t('The name of a function that will return the allowed values list.'),
    '#weight' => 6,
    '#states' => array(
      'visible' => array(
        ':input[name="field[settings][allowed_values_php]"]' => array(
          'empty' => TRUE,
        ),
      ),
    ),
  );
}