You are here

function properties_admin_categories_form in Dynamic properties 7

Form builder; Add/Edit form for categories.

1 string reference to 'properties_admin_categories_form'
properties_menu in ./properties.module
Implements hook_menu().

File

./properties.admin.inc, line 237
Contains admin menu callbacks for properties.module.

Code

function properties_admin_categories_form($form, &$form_state, $category = NULL) {
  if (!isset($form_state['category'])) {
    if (empty($category)) {
      $category = (object) array(
        'label' => '',
        'name' => '',
        'attributes' => array(),
        'new' => TRUE,
      );
    }
    $form_state['category'] = $category;
  }

  // Make sure there are always 3 empty attribute displayed.
  $add_empty = TRUE;
  foreach ($form_state['category']->attributes as $attribute) {
    if (empty($attribute->name)) {
      $add_empty = FALSE;
      break;
    }
  }
  if ($add_empty) {

    // Use a random id for these to trick out browsers.
    $form_state['category']->attributes[] = (object) array(
      'name' => '',
      'label' => '',
      'weight' => count($form_state['category']->attributes),
    );
  }
  drupal_add_css(drupal_get_path('module', 'properties') . '/properties_admin.css');
  $form['label'] = array(
    '#title' => t('Category label'),
    '#type' => 'textfield',
    '#default_value' => $form_state['category']->label,
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $form_state['category']->name,
    '#maxlength' => 128,
    '#disabled' => empty($form_state['category']->new),
    '#machine_name' => array(
      'exists' => 'properties_category_load',
      'source' => array(
        'label',
      ),
      'replace' => '_',
    ),
    '#description' => t('A unique machine-readable name for this category. It must only contain lowercase letters, numbers, and underscores. This name will be used when using this attribute.'),
  );
  $attributes = array();
  $delta = 0;
  foreach ($form_state['category']->attributes as $name => $attribute) {
    if (empty($attribute->name) && isset($form_state['input']['attributes'][$delta])) {
      unset($form_state['input']['attributes'][$delta]);
    }
    $element = array();
    $element['attribute'] = array(
      '#type' => 'textfield',
      '#default_value' => $attribute->name,
      '#autocomplete_path' => 'properties_autocomplete/attribute',
      '#ajax' => array(
        'callback' => 'properties_admin_update_label_js',
        'wrapper' => 'attributes-wrapper',
        'effect' => 'fade',
      ),
    );

    // If name is given but label is empty, this means that this is a new
    // attribute. Display a textfield to enter a label.
    if (!empty($attribute->new)) {
      $element['label'] = array(
        '#type' => 'textfield',
        '#default_value' => $attribute->label,
        '#maxlength' => 255,
        '#required' => TRUE,
      );
    }
    else {
      $element['label'] = array(
        '#markup' => check_plain($attribute->label),
      );
    }

    // We name the element '_weight' to avoid clashing with elements
    // defined by widget.
    $element['_weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for row @number', array(
        '@number' => $delta,
      )),
      '#title_display' => 'invisible',
      '#default_value' => $delta,
      '#weight' => 100,
    );
    $attributes[!empty($attribute->name) ? $attribute->name : $delta] = $element;
    $delta++;
  }
  $attributes += array(
    '#type' => 'fieldset',
    '#theme' => 'properties_admin_categories_attributes_form',
    '#title' => t('Attributes'),
    '#description' => t('Configure the default attributes of this category.'),
    '#prefix' => '<div id="attributes-wrapper">',
    '#suffix' => '</div>',
    '#max_delta' => $delta,
    '#tree' => TRUE,
  );
  $attributes['add_more'] = array(
    '#type' => 'submit',
    '#name' => 'attributes_add_more',
    '#value' => t('Add another attribute'),
    '#attributes' => array(
      'class' => array(
        'field-add-more-submit',
      ),
    ),
    '#limit_validation_errors' => array(
      array(
        'attributes',
      ),
    ),
    '#submit' => array(
      'properties_admin_add_more_submit',
    ),
    '#ajax' => array(
      'callback' => 'properties_admin_add_more_js',
      'wrapper' => 'attributes-wrapper',
      'effect' => 'fade',
    ),
  );
  $form['attributes'] = $attributes;
  if (empty($category->name)) {
    $form['add'] = array(
      '#value' => t('Save'),
      '#type' => 'submit',
      '#submit' => array(
        'properties_admin_categories_add',
      ),
    );
    $form['add_another'] = array(
      '#value' => t('Save and add another'),
      '#type' => 'submit',
      '#submit' => array(
        'properties_admin_categories_add_another',
      ),
    );
  }
  else {
    $form['save'] = array(
      '#value' => t('Save'),
      '#type' => 'submit',
      '#submit' => array(
        'properties_admin_categories_save',
      ),
    );
  }
  return $form;
}