You are here

function xbbcode_custom_tags in Extensible BBCode 7

Same name and namespace in other branches
  1. 8 xbbcode.admin.inc \xbbcode_custom_tags()
  2. 5 xbbcode-settings.php \xbbcode_custom_tags()
  3. 6 xbbcode.admin.inc \xbbcode_custom_tags()

List custom tags and edit or delete them.

Parameters

$form: The prepopulated form array.

$form_state: The state of the (entire) configuration form.

$name: If passed, load this tag for editing. Otherwise, list all tags and show a collapsed tag creation form.

Return value

A form ready for building.

1 string reference to 'xbbcode_custom_tags'
xbbcode_menu in ./xbbcode.module
Implements hook_menu().

File

./xbbcode.admin.inc, line 22
Administrative interface for modifying tags and settings.

Code

function xbbcode_custom_tags($form, &$form_state, $name = NULL) {
  module_load_include('inc', 'xbbcode', 'xbbcode.crud');

  // Determine whether the user has loaded an existing tag for editing (via edit link).
  $editing_tag = !empty($name);

  // If the form was submitted, then a new tag is being added.
  $adding_tag = !empty($form_state['input']) && $form_state['input']['op'] == t('Save');
  $access_php = module_exists('php') && user_access('use PHP for settings');
  $use_php = FALSE;

  // The upshot is that if a tag is being edited or added, the otherwise optional fields become required.
  // If editing a tag, load this tag and populate the form with its values.
  if ($editing_tag) {
    $tag = xbbcode_custom_tag_load($name);
    $use_php = $tag->options['php'];
    $form['edit'] = array(
      '#type' => 'fieldset',
      '#title' => t('Editing Tag %name', array(
        '%name' => $name,
      )),
      '#collapsible' => FALSE,
    );
  }
  else {
    $tags = array_keys(xbbcode_custom_tag_load());

    // If any tags already exist, build a list for deletion and editing.
    if (!empty($tags)) {
      foreach ($tags as $tag) {
        $options[$tag] = '[' . $tag . '] ' . l(t('Edit'), "admin/config/content/xbbcode/tags/{$tag}/edit");
      }
      $form['existing'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Existing tags'),
        '#description' => t('Check these tags and click "Delete" to delete them.'),
        '#options' => $options,
      );
    }
    else {

      // If no tags exist, then a new tag must be added now.
      $adding_tag = TRUE;
    }
    $form['edit'] = array(
      '#type' => 'fieldset',
      '#title' => t('Create new tag'),
      '#collapsible' => TRUE,
      '#collapsed' => count($tags),
    );

    // Create an empty tag.
    $tag = (object) array(
      'name' => '',
      'description' => '',
      'markup' => '',
      'sample' => '',
    );
  }

  // Regardless of whether a new tag or an existing tag is being edited,
  // show the edit form now. The fields are required only if a new tag is being
  // saved (during the submission phase), or if an existing tag is being edited.
  $form['edit']['name'] = array(
    '#type' => 'textfield',
    '#default_value' => $tag->name,
    '#field_prefix' => '[',
    '#field_suffix' => ']',
    '#required' => $editing_tag || $adding_tag,
    '#maxlength' => 32,
    '#size' => 16,
    '#description' => t('The name of this tag. The name will be used in the text as [name]...[/name]. Must be alphanumeric and will automatically be converted to lowercase.'),
  );
  $form['edit']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $tag->description,
    '#required' => $editing_tag || $adding_tag,
    '#description' => t('This will be shown on help pages'),
  );
  $form['edit']['sample'] = array(
    '#type' => 'textfield',
    '#title' => t('Sample tag'),
    '#required' => $editing_tag || $adding_tag,
    '#description' => t('Enter an example of how this tag would be used. It will be shown on the help pages.'),
    '#default_value' => $tag->sample,
  );
  $form['edit']['options'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Tag options'),
    '#options' => array(
      'selfclosing' => t('Tag is self-closing (requires no closing tag, like <code>[img]</code>).'),
      'nocode' => t('Ignore further BBCode inside this tag.'),
      'plain' => t('Escape all HTML inside this tag.'),
    ),
    '#description' => t('The last two options should in most cases be used together. Note that HTML will not be escaped twice even if this tag is used in a format that allows no HTML in the first place.'),
  );
  $form['edit']['php'] = array(
    '#type' => 'checkbox',
    '#title' => t('Evaluate as PHP code.'),
    '#return_value' => TRUE,
    '#description' => t('This option requires the PHP module to be enabled, and the appropriate permission.'),
    '#default_value' => $use_php,
  );
  foreach ($form['edit']['options']['#options'] as $key => $value) {
    if (!empty($tag->options[$key])) {
      $form['edit']['options']['#default_value'][] = $key;
    }
  }
  $form['edit']['markup'] = array(
    '#type' => 'textarea',
    '#attributes' => array(
      'style' => 'font-family:monospace',
    ),
    '#title' => t('Rendering code'),
    '#default_value' => $tag->markup,
    '#required' => $editing_tag || $adding_tag,
    '#description' => t('The text that [tag]content[/tag] should be replaced with, or PHP code that prints/returns the text.'),
  );
  if (!$access_php) {
    $form['edit']['php']['#disabled'] = TRUE;
    $form['edit']['php']['#value'] = $form['edit']['php']['#default_value'];

    // Imitate the behavior of filter.module on forbidden formats.
    if ($use_php) {
      $form['edit']['markup']['#disabled'] = TRUE;
      $form['edit']['markup']['#resizable'] = FALSE;
      $form['edit']['markup']['#value'] = $form['edit']['markup']['#default_value'];
      $form['edit']['markup']['#pre_render'] = array(
        'filter_form_access_denied',
      );
    }
  }
  $form['edit']['help'] = array(
    '#type' => 'markup',
    '#title' => t('Coding help'),
    '#markup' => t('<p>The above field should be filled either with HTML or PHP code depending on whether you enabled the PHP code option. PHP code must be placed in &lt;?php ?&gt; enclosures, or it will be
    printed literally.</p>
    <p>If your tag uses static HTML, then the tag\'s content and attributes will be inserted into your code by replacing placeholders. In PHP code, they will be available in the <code>$tag</code> object.</p>
    <dl>
      <dt><code>{content}</code> or <code>$tag->content</code></dt>
      <dd>The text between opening and closing tags, if the tag is not self-closing. Example: <code>[url=http://www.drupal.org]<strong>Drupal</strong>[/url]</code></dd>
      <dt><code>{option}</code> or <code>$tag->option</code></dt>
      <dd>The single tag attribute, if one is entered. Example: <code>[url=<strong>http://www.drupal.org</strong>]Drupal[/url]</code>.</dd>
      <dt>any other <code>{attribute}</code> or <code>$tag->attr(\'attribute\')</code></dt>
      <dd>The tag attribute of the same name, if it is entered. E.g: <strong>{by}</strong> or <strong><code>$tag->attr(\'by\')</code></strong> for <code>[quote&nbsp;by=<strong>Author</strong>&nbsp;date=2008]Text[/quote]</code>. If the attribute is not entered, the placeholder will be replaced with an empty string, and the <code>attr()</code> return value will be <code>NULL</code>.</dd>
    </dl>'),
  );
  $form['edit']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array(
      'xbbcode_custom_tags_save_submit',
    ),
  );
  if (!empty($name) || count($tags)) {
    $delete = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array(
        'xbbcode_custom_tags_delete_submit',
      ),
    );
    if (!empty($name)) {
      $form['edit']['delete'] = $delete;
    }
    else {
      $form['delete'] = $delete;
    }
  }
  return $form;
}