You are here

function token_custom_edit_form in Custom Tokens 7.2

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 token_custom.admin.inc \token_custom_edit_form()

The add/edit token form

Parameters

array $form: The form array.

array $form_state: The form state.

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

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

Return value

array The token edit/add form.

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

File

./token_custom.admin.inc, line 129
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 hyphens.'),
    '#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,
  );
  $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,
    '#maxlength' => 128,
    '#default_value' => $token && !empty($token->type) ? $token->type : 0,
  );
  $form['content'] = array(
    '#type' => 'text_format',
    '#title' => t('Content'),
    '#description' => t('Enter the content that will be replaced with this token.'),
    '#default_value' => isset($token->content) ? $token->content : '',
    '#format' => isset($token->format) ? $token->format : filter_default_format(),
  );

  // Add help text if PHP filter is available.
  if (module_exists('php')) {
    $ref = l('token_replace()', 'http://api.drupal.org/api/drupal/includes--token.inc/function/token_replace/7', array(
      'attributes' => array(
        'target' => '_blank',
      ),
    ));
    $form['content']['#description'] .= '<br />' . t('PHP Filter : You will have access to all the arguments of !link (ex : $data[\'node\'] for node token type, $options). Be sure to carefully read the documentation regarding the security implications of using the php input filter.', array(
      '!link' => $ref,
    ));
  }
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  if ($op == 'edit') {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  return $form;
}