You are here

function properties_template_admin_templates_form_validate in Dynamic properties 7

Form builder; Valdate template form.

File

properties_template/properties_template.admin.inc, line 217
Contains admin menu callbacks for properties_template.module.

Code

function properties_template_admin_templates_form_validate($form, &$form_state) {

  // Update template object.
  $form_state['template']->label = $form_state['values']['label'];
  $form_state['template']->name = $form_state['values']['name'];
  $form_state['template']->categories = array();
  uasort($form_state['values']['categories'], '_field_sort_items_helper');
  foreach ($form_state['values']['categories'] as $name => $category) {
    if (is_array($category) && isset($category['category'])) {
      if (empty($category['category'])) {
        $category_object = (object) array(
          'label' => '',
          'name' => '',
          'weight' => count($form_state['template']->categories),
        );
        $form_state['template']->categories[$name] = $category_object;
      }
      elseif ($category_object = properties_category_load($category['category'])) {
        $category_object->weight = count($form_state['template']->categories);
        $form_state['template']->categories[$category_object->name] = $category_object;
      }
      else {

        // Update empty category.
        $form_state['template']->categories[$category['category']]->name = $category['category'];
        $form_state['template']->categories[$category['category']]->label = isset($category['label']) ? $category['label'] : '';
        $form_state['template']->categories[$category['category']]->weight = count($form_state['template']->categories);
        $form_state['template']->categories[$category['category']]->new = TRUE;

        // If label is empty, add validation error.
        if (empty($form_state['template']->categories[$category['category']]->label)) {

          // Not possible to do this as a proper form validation step because
          // the form is not rebuilt in case of validation errors.
          drupal_set_message(t('Category %name does not exist, create a category before adding it to the template.', array(
            '%name' => $category['category'],
          )), 'error');
          $form_state['rebuild'] = TRUE;
        }
      }
    }
  }
}