You are here

function widgets_definition_edit_form in Widgets 7

Form builder; Form for adding a new widget set.

See also

widgets_set_add_form_submit()

widgets_set_name_validate()

1 string reference to 'widgets_definition_edit_form'
widgets_menu in ./widgets.module
Implements hook_menu().

File

./widgets.admin.inc, line 889
Administration pages for widget settings.

Code

function widgets_definition_edit_form($form, &$form_state, $definition = NULL, $add = FALSE) {
  $operation = $add ? 'add' : 'edit';
  if (isset($definition)) {

    // Check if clone operation.
    if ($add) {
      $operation = 'clone';
      $definition = widgets_element_definition_load($definition);
      $name = $definition['name'] . '-custom';

      // Check name is unique.
      $i = 1;
      while (widgets_element_definition_load($name)) {
        $name = $definition['name'] . '-custom-' . $i;
        $i++;
      }
      $definition['name'] = $name;
    }
    $title = t('Edit %name definition', array(
      '%name' => $definition['name'],
    ));
    drupal_set_title($title, PASS_THROUGH);
  }
  $form['operation'] = array(
    '#type' => 'value',
    '#value' => $operation,
  );
  $form['#tree'] = TRUE;
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Definition name'),
    '#default_value' => isset($definition['name']) ? $definition['name'] : '',
    '#description' => t('The name is used to identify the widget set in blocks, tokens and other displays. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
    '#element_validate' => array(
      'widgets_set_name_validate',
    ),
    '#required' => TRUE,
  );
  $form['template'] = array(
    '#type' => 'textarea',
    '#title' => t('Template'),
    '#default_value' => isset($definition['template']) ? $definition['template'] : '',
    '#description' => t('Enter the markup code for the widget.') . ' ' . widgets_definition_template_syntax(),
    '#required' => TRUE,
  );
  if (module_exists('token') && FALSE) {
    $form['tokens'] = array(
      '#type' => 'item',
      '#title' => t('Available tokens'),
      '#markup' => theme('token_tree', array(
        'token_types' => 'all',
        'click_insert' => FALSE,
        'show_restricted' => TRUE,
      )),
    );
  }
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['advanced']['add_js'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add JavaScript'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#description' => t('Some widgets required JavaScript in addition to the standard template code. You can add additional JavaScript code using the below inputs. By default JavaScript added here will be unique per widget set. This is particularly useful if you have a set of widgets that enabled by a single JavaScript.'),
  );
  $form['advanced']['add_js']['data'] = array(
    '#type' => 'textarea',
    '#rows' => 3,
    '#title' => t('JavaScript'),
    '#default_value' => isset($definition['add_js']['data']) ? $definition['add_js']['data'] : '',
    '#description' => t('Enter any JavaScript you want added with this widget.') . ' ',
  );
  $options = array(
    'set_pre' => 'Before the widget set',
    'set_post' => 'After the widget set',
    'header' => 'In the page header',
    'footer' => 'In the page footer',
  );
  $form['advanced']['add_js']['options']['scope'] = array(
    '#type' => 'select',
    '#title' => t('Scope'),
    '#options' => $options,
    '#default_value' => isset($definition['add_js']['options']['scope']) ? $definition['add_js']['options']['scope'] : '',
    '#description' => t('Select where you want the JavaScript added.') . ' ',
  );
  if (isset($definition)) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Update definition'),
    );
  }
  else {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Create new definition'),
    );
  }
  return $form;
}