You are here

function token_custom_edit_form in Custom Tokens 7

Same name and namespace in other branches
  1. 5 token_custom.module \token_custom_edit_form()
  2. 6 token_custom.module \token_custom_edit_form()
  3. 7.2 token_custom.admin.inc \token_custom_edit_form()

The add/edit token form

_state

Parameters

array $form:

string $op: Either 'add' or 'edit', according to the operation performed

$token: If $op is 'edit', $token contains the loaded token to edit.

Return value

array

1 string reference to 'token_custom_edit_form'
token_custom_menu in ./token_custom.module
Implementation of hook_menu().

File

./token_custom.admin.inc, line 84
Page callbacks and admin forms of the token_custom module.

Code

function token_custom_edit_form($form, &$form_state, $op, $token = NULL) {
  $form = array();
  $form_state['token_custom']['op'] = $op;
  $form_state['token_custom']['token'] = $token;
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Token name'),
    '#description' => t('The token\'s readable name'),
    '#default_value' => $token ? $token->name : NULL,
    '#maxlength' => 128,
    '#required' => TRUE,
  );
  $form['machine_name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Token machine name'),
    '#description' => t('A unique machine-readable name for this token. It must only contain lowercase letters, numbers, and underscores.'),
    '#default_value' => $token ? $token->machine_name : NULL,
    '#maxlength' => 32,
    '#disabled' => (bool) $token,
    '#machine_name' => array(
      'exists' => 'token_custom_load',
      'replace' => '-',
      'replace_pattern' => '[^a-z0-9\\-]+',
    ),
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Token description'),
    '#description' => t('The token\'s description that will appear in the token list'),
    '#default_value' => $token ? $token->description : NULL,
    '#maxlength' => 255,
    '#required' => TRUE,
  );

  /*
   * TODO Allow users to define their own custom types
   */
  $token_info = token_info();
  $options = array(
    'custom' => 'Custom Token',
  );
  foreach ($token_info['types'] as $type => $info) {
    $options[$type] = $info['name'];
    if (!empty($info['needs-data'])) {
      $options[$type] .= ' [needs: ' . $info['needs-data'] . ']';
    }
  }
  $form['type'] = array(
    '#type' => 'select',
    '#title' => 'Token type',
    '#description' => t('The token type determines the availability of the token according to the data in the $data array (ex. a token of type <em>node</em> will need $data[node].'),
    '#options' => $options,
    '#default_value' => $token && !empty($token->type) ? $token->type : 0,
  );
  $form['php_content'] = array(
    '#type' => 'textarea',
    '#title' => t('PHP replacement'),
    '#description' => t('Enter the php code that will be evaluated. You have access to all the arguments of !link\\. The code should return a string and should NOT be wrapped around %php.', array(
      '%php' => '<?php ?>',
      '!link' => l('token_replace()', 'http://api.drupal.org/api/drupal/includes--token.inc/function/token_replace/7', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )),
    )),
    '#required' => TRUE,
    '#default_value' => $token ? $token->php_content : NULL,
  );
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => 'Save token',
  );
  return $form;
}