function properties_template_admin_templates_form in Dynamic properties 7
Form builder; Add/Edit form for templates.
1 string reference to 'properties_template_admin_templates_form'
- properties_template_menu in properties_template/
properties_template.module - Implements hook_menu().
File
- properties_template/
properties_template.admin.inc, line 42 - Contains admin menu callbacks for properties_template.module.
Code
function properties_template_admin_templates_form($form, &$form_state, $template = NULL) {
if (!isset($form_state['template'])) {
if (isset($_GET['entity_type'])) {
$template = (object) array(
'label' => '',
'name' => '',
'categories' => array(),
'new' => TRUE,
);
$entity_type = $_GET['entity_type'];
$entity_id = $_GET['entity_id'];
$field_name = $_GET['field_name'];
if (entity_get_info($entity_type) && field_info_field($field_name)) {
$entity = entity_load($entity_type, array(
$entity_id,
));
$entity = reset($entity);
if ($entity && isset($entity->{$field_name})) {
$langcode = field_language($entity_type, $entity, $field_name);
$field = $entity->{$field_name};
$categories = array();
foreach ($field[$langcode] as $item) {
$categories[$item['category']] = $item['category'];
}
if (!empty($categories)) {
$category_objects = properties_category_load_multiple($categories);
foreach ($category_objects as $category_object) {
$category_object->weight = count($template->categories);
$template->categories[$category_object->name] = $category_object;
}
}
}
}
}
elseif (empty($template) || !is_object($template)) {
$template = (object) array(
'label' => '',
'name' => '',
'categories' => array(),
'new' => TRUE,
);
}
$form_state['template'] = $template;
}
// Make sure there are always 3 empty categories displayed.
$add_empty = TRUE;
foreach ($form_state['template']->categories as $category) {
if (empty($category->name)) {
$add_empty = FALSE;
break;
}
}
if ($add_empty) {
// Use a random id for these to trick out browsers.
$form_state['template']->categories[] = (object) array(
'name' => '',
'label' => '',
'weight' => count($form_state['template']->categories),
);
}
drupal_add_css(drupal_get_path('module', 'properties') . '/properties_admin.css');
$form['label'] = array(
'#title' => t('Template label'),
'#type' => 'textfield',
'#default_value' => $form_state['template']->label,
'#maxlength' => 255,
'#required' => TRUE,
);
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => $form_state['template']->name,
'#maxlength' => 128,
'#disabled' => empty($form_state['template']->new),
'#machine_name' => array(
'exists' => 'properties_template_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 category.'),
);
$categories = array();
$delta = 0;
foreach ($form_state['template']->categories as $name => $category) {
if (empty($category->name) && isset($form_state['input']['categories'][$delta])) {
unset($form_state['input']['categories'][$delta]);
}
$element = array();
$element['category'] = array(
'#type' => 'textfield',
'#default_value' => $category->name,
'#autocomplete_path' => 'properties_autocomplete/category',
'#ajax' => array(
'callback' => 'properties_admin_update_label_js',
'wrapper' => 'attributes-wrapper',
'effect' => 'fade',
),
);
// TODO: add categories-wrapper
$element['label'] = array(
'#markup' => $category->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,
);
$categories[!empty($category->name) ? $category->name : $delta] = $element;
$delta++;
}
$categories += array(
'#type' => 'fieldset',
'#theme' => 'properties_template_admin_templates_categories_form',
'#title' => t('Categories'),
'#description' => t('Configure the default categories of this template.'),
'#prefix' => '<div id="attributes-wrapper">',
'#suffix' => '</div>',
'#max_delta' => $delta,
'#tree' => TRUE,
);
$categories['add_more'] = array(
'#type' => 'submit',
'#name' => 'categories_add_more',
'#value' => t('Add another category'),
'#categories' => array(
'class' => array(
'field-add-more-submit',
),
),
'#limit_validation_errors' => array(
array(
'categories',
),
),
'#submit' => array(
'properties_template_admin_add_more_submit',
),
'#ajax' => array(
'callback' => 'properties_template_admin_add_more_js',
'wrapper' => 'attributes-wrapper',
'effect' => 'fade',
),
);
$form['categories'] = $categories;
if (empty($template->name)) {
$form['add'] = array(
'#value' => t('Save'),
'#type' => 'submit',
'#submit' => array(
'properties_template_admin_templates_add',
),
);
$form['add_another'] = array(
'#value' => t('Save and add another'),
'#type' => 'submit',
'#submit' => array(
'properties_template_admin_templates_add_another',
),
);
}
else {
$form['save'] = array(
'#value' => t('Save'),
'#type' => 'submit',
'#submit' => array(
'properties_template_admin_templates_save',
),
);
}
return $form;
}