From 987172b49fa1ab25b8b385f014581a3359da557d Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Tue, 24 May 2011 22:44:41 +0200
Subject: [PATCH] Issue #1161276 by xatoo, Berdir: Move logic from template into preprocess function.

---
 properties-properties-form.tpl.php                |  109 ++++-----------------
 properties.admin.inc                              |   12 ++-
 properties.module                                 |  110 ++++++++++++++++++++-
 properties_template/properties_template.admin.inc |    1 +
 4 files changed, 134 insertions(+), 98 deletions(-)

diff --git a/properties-properties-form.tpl.php b/properties-properties-form.tpl.php
index d4afcc6..9bcd702 100644
--- a/properties-properties-form.tpl.php
+++ b/properties-properties-form.tpl.php
@@ -1,92 +1,19 @@
 <?php
-
-$element = $variables['element'];
-$output = '';
-
-$table_id = 'properties-table';
-
-if (user_access('add properties attributes')) {
-  $header = array(
-    t('Attribute name'),
-    t('Attribute label'),
-    t('Value'),
-    t('Category'),
-    t('Order'),
-    t('Delete'),
-  );
-}
-else {
-  $header = array(
-    t('Attribute'),
-    t('Value'),
-  );
-  if (user_access('add properties categories')) {
-    $header[] = t('Order');
-  }
-}
-$rows = array();
-if (user_access('add properties attributes') || user_access('add properties categories')) {
-  drupal_add_tabledrag($table_id, 'match', 'parent', 'property-category-select', 'property-category-select', 'category-name');
-  drupal_add_tabledrag($table_id, 'order', 'sibling', 'property-weight');
-}
-
-foreach (element_children($element['listing']) as $category_name) {
-  $category = $element['listing'][$category_name];
-  $category['_weight']['#attributes']['class'] = array('property-weight');
-  $category['name']['#attributes']['class'] = array('category-name');
-  $category['category']['#attributes']['class'] = array('property-category-select');
-
-  $cells = array();
-  $cells[] = '<strong>' . $category['#label'] . '</strong>';
-  $cells[] = '&nbsp;';
-  if (user_access('add properties attributes')) {
-    $cells[] = '&nbsp;';
-    $cells[] = '&nbsp;';
-  }
-  if (user_access('add properties categories')) {
-    $cells[] = drupal_render($category['category']) . drupal_render($category['name']) . drupal_render($category['_weight']);
-    $cells[] = drupal_render($category['delete']);
-  }
-  $rows[] = array(
-    'data' => $cells,
-    'class' => array('draggable', 'tabledrag-root'),
-  );
-
-  foreach (element_children($category['properties']) as $delta) {
-    $property = $category['properties'][$delta];
-
-    // Set special classes needed for table drag and drop.
-    $property['category']['#attributes']['class'] = array('property-category-select');
-    $property['_weight']['#attributes']['class'] = array('property-weight');
-
-    $cells = array();
-    if (user_access('add properties attributes')) {
-      $cells[] = array('data' => theme('indentation', array('size' => 1)) . drupal_render($property['attribute']), 'class' => array('properties-attribute-row-small'));
-      $cells[] = drupal_render($property['label']);
-    }
-    else {
-      $cells[] = theme('indentation', array('size' => 1)) . drupal_render($property['label']);
-    }
-
-    $cells[] = drupal_render($property['value']);
-    if (user_access('add properties attributes')) {
-      $cells[] = drupal_render($property['category']);
-      $cells[] = drupal_render($property['_weight']);
-      $cells[] = drupal_render($property['delete']);
-    }
-    elseif (user_access('add properties categories')) {
-      $cells[] = '&nbsp;';
-    }
-    $rows[] = array(
-      'data' => $cells,
-      'class' => user_access('add properties attributes') ? array('draggable', 'tabledrag-leaf') : array('properties-tabledrag-children'),
-    );
-  }
-}
-
-$output = '<div class="form-item field-widget-properties-table">';
-$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => $table_id, 'class' => array('properties-table'))));
-$output .= '<div class="clearfix inline">' . drupal_render($element['actions']) . '</div>';
-$output .= '</div>';
-
-echo $output;
+/**
+ * @file
+ * Default theme implementation of the properties form.
+ *
+ * Available variables:
+ * - $table: the rendered draggable table with properties.
+ * - $actions: the rendered buttons for the addition of categories and property fields.
+ * - $element: Provides an array with a wide range of additional internal data.
+ *
+ * @see template_preprocess_properties_properties_form()
+ */
+?>
+<div class="form-item field-widget-properties-table">
+  <?php print render($table); ?>
+  <div class="clearfix inline">
+    <?php print render($actions); ?>
+  </div>
+</div>
diff --git a/properties.admin.inc b/properties.admin.inc
index 0ecefb2..655447b 100644
--- a/properties.admin.inc
+++ b/properties.admin.inc
@@ -90,6 +90,11 @@ function properties_admin_attributes_list($form, $form_state) {
     '#title' => t('Search'),
     '#title_display' => 'invisible',
     '#size' => 20,
+  );
+
+  $form['filter']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Search'),
     '#ajax' => array(
       'callback' => 'properties_admin_attributes_list_js',
       'wrapper' => 'attributes-list',
@@ -98,11 +103,6 @@ function properties_admin_attributes_list($form, $form_state) {
     ),
   );
 
-  $form['filter']['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Search'),
-  );
-
   $form['filter']['reset'] = array(
     '#type' => 'submit',
     '#value' => t('Reset'),
@@ -172,6 +172,7 @@ function properties_admin_attributes_form($form, &$form_state, $attribute = NULL
     '#disabled' => !empty($attribute->name),
     '#machine_name' => array(
       'exists' => 'properties_attribute_load',
+      'source' => array('label'),
     ),
     '#description' => t('A unique machine-readable name for this attribute. It must only contain lowercase letters, numbers, and underscores. This name will be used when using this attribute.'),
   );
@@ -279,6 +280,7 @@ function properties_admin_categories_form($form, &$form_state, $category = NULL)
     '#disabled' => empty($form_state['category']->new),
     '#machine_name' => array(
       'exists' => 'properties_category_load',
+      'source' => array('label'),
     ),
     '#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.'),
   );
diff --git a/properties.module b/properties.module
index 6ac9eee..86ca584 100644
--- a/properties.module
+++ b/properties.module
@@ -181,6 +181,110 @@ function properties_theme() {
 }
 
 /**
+ * Process variables for properties-properties-form.tpl.php
+ *
+ * @see properties-properties-form.tpl.php
+ */
+function template_preprocess_properties_properties_form(&$variables) {
+  $element = &$variables['element'];
+  $table_id = $element['#properties_table_id'] . '-inner';
+
+  if (user_access('add properties attributes')) {
+    $header = array(
+      t('Attribute name'),
+      t('Attribute label'),
+      t('Value'),
+      t('Category'),
+      t('Order'),
+      t('Delete'),
+    );
+  }
+  else {
+    $header = array(
+      t('Attribute'),
+      t('Value'),
+    );
+    if (user_access('add properties categories')) {
+      $header[] = t('Order');
+    }
+  }
+  $rows = array();
+  if (user_access('add properties attributes') || user_access('add properties categories')) {
+    drupal_add_tabledrag($table_id, 'match', 'parent', 'property-category-select', 'property-category-select', 'category-name');
+    drupal_add_tabledrag($table_id, 'order', 'sibling', 'property-weight');
+  }
+
+  foreach (element_children($element['listing']) as $category_name) {
+    $category = $element['listing'][$category_name];
+    $category['_weight']['#attributes']['class'] = array('property-weight');
+    $category['name']['#attributes']['class'] = array('category-name');
+    $category['category']['#attributes']['class'] = array('property-category-select');
+
+    $cells = array();
+    $cells[] = '<strong>' . $category['#label'] . '</strong>';
+    $cells[] = '&nbsp;';
+    if (user_access('add properties attributes')) {
+      $cells[] = '&nbsp;';
+      $cells[] = '&nbsp;';
+    }
+    if (user_access('add properties categories')) {
+      $cells[] = drupal_render($category['category']) . drupal_render($category['name']) . drupal_render($category['_weight']);
+      $cells[] = drupal_render($category['delete']);
+    }
+    $rows[] = array(
+      'data' => $cells,
+      'class' => array('draggable', 'tabledrag-root'),
+    );
+
+    foreach (element_children($category['properties']) as $delta) {
+      $property = $category['properties'][$delta];
+
+      // Set special classes needed for table drag and drop.
+      $property['category']['#attributes']['class'] = array('property-category-select');
+      $property['_weight']['#attributes']['class'] = array('property-weight');
+
+      $cells = array();
+      if (user_access('add properties attributes')) {
+        $cells[] = array('data' => theme('indentation', array('size' => 1)) . drupal_render($property['attribute']), 'class' => array('properties-attribute-row-small'));
+        $cells[] = drupal_render($property['label']);
+      }
+      else {
+        $cells[] = theme('indentation', array('size' => 1)) . drupal_render($property['label']);
+      }
+
+      $cells[] = drupal_render($property['value']);
+      if (user_access('add properties attributes')) {
+        $cells[] = drupal_render($property['category']);
+        $cells[] = drupal_render($property['_weight']);
+        $cells[] = drupal_render($property['delete']);
+      }
+      elseif (user_access('add properties categories')) {
+        $cells[] = '&nbsp;';
+      }
+      $rows[] = array(
+        'data' => $cells,
+        'class' => user_access('add properties attributes') ? array('draggable', 'tabledrag-leaf') : array('properties-tabledrag-children'),
+      );
+    }
+  }
+
+  $variables['table'] = array(
+      '#theme' => 'table',
+      '#header' => $header,
+      '#rows' => $rows,
+      '#attributes' => array(
+        'id' => $table_id,
+        'class' => array('properties-table'),
+      ),
+      '#empty' => t('Add properties to this content. Start by adding categories to it.'),
+    );
+
+  $variables['actions'] = $element['actions'];
+
+  $variables['theme_hook_suggestions'][] = 'properties-properties-form__' . $table_id;
+}
+
+/**
  * Check access to an administration page.
  *
  * @param $type
@@ -525,7 +629,6 @@ function properties_field_widget_form(&$form, &$form_state, $field, $instance, $
     '#type' => 'fieldset',
     '#theme' => 'properties_properties_form',
     '#title' => t('Properties'),
-    '#description' => t('Add properties to this content. Start by adding categories to it.'),
     '#prefix' => '<div id="' . $id . '">',
     '#suffix' => '</div>',
     '#max_delta' => $delta,
@@ -770,7 +873,10 @@ function properties_field_formatter_view($entity_type, $entity, $field, $instanc
           $rows[] = array(check_plain($label), check_plain($item['value']));
         }
       }
-      $element[0]['#markup'] = theme('table', array('rows' => $rows));
+      $element[0] = array(
+        '#theme' => 'table',
+        '#rows' => $rows,
+      );
       break;
   }
 
diff --git a/properties_template/properties_template.admin.inc b/properties_template/properties_template.admin.inc
index bcf9998..657f231 100644
--- a/properties_template/properties_template.admin.inc
+++ b/properties_template/properties_template.admin.inc
@@ -121,6 +121,7 @@ function properties_template_admin_templates_form($form, &$form_state, $template
     '#disabled' => empty($form_state['template']->new),
     '#machine_name' => array(
       'exists' => 'properties_template_load',
+      'source' => array('label'),
     ),
     '#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.'),
   );
-- 
1.7.4.1

