You are here

function cck_default_value_form in Content Construction Kit (CCK) 7.3

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

1 call to cck_default_value_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 97
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_default_value_form(&$form, $form_state, $field) {
  $instance = field_info_instance($form['instance']['entity_type']['#value'], $field['field_name'], $form['instance']['bundle']['#value']);
  $default_value_function = !empty($instance['default_value_function']) ? $instance['default_value_function'] : '';
  $php_code = cck_field_get_setting('default_value_php', 'instance', $field, $instance);
  if (!empty($php_code)) {
    $default_value_function = 'cck_default_value_php';
  }

  // Add a field where users can specify some PHP
  // code that will return the default value.
  module_load_include('install', $field['module']);
  $schema = module_invoke($field['module'], 'field_schema', $field);
  $columns = array_keys($schema['columns']);
  $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['instance']['default_value_widget']['default_value_php'] = array(
    '#access' => user_access('Use PHP input for field settings (dangerous - grant with care)'),
    '#type' => 'textarea',
    '#title' => t('Default value PHP code'),
    '#default_value' => $php_code,
    '#description' => t('Advanced usage only: PHP code that returns a default value. 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_default_value_php</strong> and will override any other default value or function specified above. Expected format: <pre>!sample</pre>To figure out the expected format, you can use the <em>devel load</em> tab provided by <a href="@link_devel">devel module</a> on a content page.', array(
      '!sample' => $sample,
      '@link_devel' => 'http://www.drupal.org/project/devel',
    )),
    '#weight' => 5,
    '#parents' => array(
      'instance',
      'default_value_php',
    ),
  );

  // Add a field where users can specify a function
  // to return the default value.
  $form['instance']['default_value_widget']['default_value_function'] = array(
    '#type' => 'textfield',
    '#title' => t('Default value function'),
    '#default_value' => $default_value_function,
    '#description' => t('The name of a function that will return the default value.'),
    '#states' => array(
      'visible' => array(
        ':input[name="instance[default_value_php]"]' => array(
          'empty' => TRUE,
        ),
      ),
    ),
    '#weight' => 6,
    '#parents' => array(
      'instance',
      'default_value_function',
    ),
  );
}