Fix for CCK field not showing up in field selection dialog

  1. <?php
  2. // $Id$
  3.  
  4. /**
  5.  * @file
  6.  * CCK Field to integrate products to nodes
  7.  *
  8.  * Allow products to be associated with a content type and acced to cart on node creation
  9.  */
  10.  
  11. function uc_product_content_install() {
  12.   drupal_load('module', 'content');
  13.   content_notify('install', 'uc_product_content');
  14. }
  15.  
  16. function uc_product_content_uninstall() {
  17.   drupal_load('module', 'content');
  18.   content_notify('uninstall', 'uc_product_content');
  19. }
  20.  
  21. function uc_product_content_enable() {
  22.   drupal_load('module', 'content');
  23.   content_notify('enable', 'uc_product_content');
  24. }
  25.  
  26. function uc_product_content_disable() {
  27.   drupal_load('module', 'content');
  28.   content_notify('disable', 'uc_product_content');
  29. }
  30.  
  31.  
  32. /**
  33.  * Implementation of hook_field_info().
  34.  */
  35. function uc_product_content_field_info() {
  36.   drupal_set_message("I'm here!!!");
  37.   return array(
  38.     'uc_product_content' => array(
  39.       'label' => t('Product'),
  40.       'description' => t('Create product references that can be purchased at node creation.'),
  41.     ),
  42.   );
  43. }
  44.  
  45.  
  46. /**
  47.  * Implementation of hook_field_settings().
  48.  */
  49. function uc_product_content_field_settings($op, $field) {
  50.   switch ($op) {
  51.     case 'form':
  52.       $products = array();
  53.    
  54.       $data = db_query("SELECT nid, title FROM {uc_products} AS p INNER JOIN {node} AS n ON p.nid = n.nid");
  55.       foreach ($data as $nid => $title) {
  56.         $products[$nid] = $title;
  57.       }
  58.  
  59.       $form = array();
  60.       $form['product_options'] = array(
  61.         '#type' => 'checkboxes',
  62.         '#title' => t('Product Options'),
  63.         '#default_value' => isset($field['product_options']) ? $field['product_options'] : '',
  64.         '#options' => $products,
  65.         '#required' => TRUE,
  66.         '#description' => t('The group of products that may be selected.'),
  67.       );
  68.  
  69.       return $form;
  70.  
  71.     case 'save':
  72.       return array('product_options');
  73.  
  74.     case 'database columns':
  75.       $columns = array(
  76.         'product_options' => array('type' => 'varchar', 'default' => "''"),
  77.       );
  78.       return $columns;
  79.  
  80.     case 'views data':
  81.       $allowed_values = content_allowed_values($field);
  82.       if (count($allowed_values)) {
  83.         $data = content_views_field_views_data($field);
  84.         $db_info = content_database_info($field);
  85.         $table_alias = content_views_tablename($field);
  86.  
  87.         // Filter: Add a 'many to one' filter.
  88.         $copy = $data[$table_alias][$field['field_name'] .'_value'];
  89.         $copy['title'] = t('@label (!name) - Allowed values', array('@label' => t($field['widget']['label']), '!name' => $field['field_name']));
  90.         $copy['filter']['handler'] = 'content_handler_filter_many_to_one';
  91.         unset($copy['field'], $copy['argument'], $copy['sort']);
  92.         $data[$table_alias][$field['field_name'] .'_value_many_to_one'] = $copy;
  93.         // Argument : swap the handler to the 'many to one' operator.
  94.         $data[$table_alias][$field['field_name'] .'_value']['argument']['handler'] = 'content_handler_argument_many_to_one';
  95.         return $data;
  96.       }
  97.   }
  98. }
  99.  
  100.  
  101. /**
  102.  * Implementation of hook_field().
  103.  */
  104. function uc_product_content_field($op, &$node, $field, &$items, $teaser, $page) {
  105.   switch ($op) {
  106.     case 'validate':
  107.       $allowed_values = content_allowed_values($field);
  108.       if (is_array($items)) {
  109.         foreach ($items as $delta => $item) {
  110.           $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
  111.           if (is_array($item) && isset($item['_error_element'])) unset($item['_error_element']);
  112.           if (!empty($item['value'])) {
  113.             if (count($allowed_values) && !array_key_exists($item['product_options'], $allowed_values)) {
  114.               form_set_error($error_element, t('%name: illegal value.', array('%name' => t($field['widget']['label']))));
  115.             }
  116.           }
  117.         }
  118.       }
  119.       return $items;
  120.   }
  121. }
  122.  
  123. /**
  124.  * Implementation of hook_content_is_empty().
  125.  */
  126. function uc_product_content_is_empty($item, $field) {
  127.   if (empty($item['product_options']) && (string)$item['product_options'] !== '0') {
  128.     return TRUE;
  129.   }
  130.   return FALSE;
  131. }
  132.  
  133. /**
  134.  * Implementation of hook_field_formatter_info().
  135.  */
  136. function uc_product_content_field_formatter_info() {
  137.   return array(
  138.     'default' => array(
  139.       'label' => t('Default'),
  140.       'field types' => array('uc_product_content'),
  141.       'multiple values' => CONTENT_HANDLE_CORE,
  142.     ),
  143.   );
  144. }
  145.  
  146. /**
  147.  * Implementation of hook_default_value()
  148.  */
  149. function uc_product_content_default_value(&$form, &$form_state, $field, $delta) {
  150.   return 0;
  151. }
  152.  
  153. /**
  154.  * Implementation of hook_widget().
  155.  *
  156.  * Attach a single form element to the form. It will be built out and
  157.  * validated in the callback(s) listed in hook_elements. We build it
  158.  * out in the callbacks rather than here in hook_widget so it can be
  159.  * plugged into any module that can provide it with valid
  160.  * $field information.
  161.  *
  162.  * Content module will set the weight, field name and delta values
  163.  * for each form element. This is a change from earlier CCK versions
  164.  * where the widget managed its own multiple values.
  165.  *
  166.  * If there are multiple values for this field, the content module will
  167.  * call this function as many times as needed.
  168.  *
  169.  * @param $form
  170.  *   the entire form array, $form['#node'] holds node information
  171.  * @param $form_state
  172.  *   the form_state, $form_state['values'][$field['field_name']]
  173.  *   holds the field's form values.
  174.  * @param $field
  175.  *   the field array
  176.  * @param $items
  177.  *   array of default values for this field
  178.  * @param $delta
  179.  *   the order of this item in the array of subelements (0, 1, 2, etc)
  180.  *
  181.  * @return
  182.  *   the form item for a single element for this field
  183.  */
  184. function uc_product_content_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  185.   $element = array(
  186.     '#type' => $field['widget']['type'],
  187.     '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  188.   );
  189.   return $element;
  190. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.