<?php
// $Id$
/**
* @file
* CCK Field to integrate products to nodes
*
* Allow products to be associated with a content type and acced to cart on node creation
*/
function uc_product_content_install() {
drupal_load('module', 'content');
content_notify('install', 'uc_product_content');
}
function uc_product_content_uninstall() {
drupal_load('module', 'content');
content_notify('uninstall', 'uc_product_content');
}
function uc_product_content_enable() {
drupal_load('module', 'content');
content_notify('enable', 'uc_product_content');
}
function uc_product_content_disable() {
drupal_load('module', 'content');
content_notify('disable', 'uc_product_content');
}
/**
* Implementation of hook_field_info().
*/
function uc_product_content_field_info() {
drupal_set_message("I'm here!!!");
return array(
'uc_product_content' => array(
'label' => t('Product'),
'description' => t('Create product references that can be purchased at node creation.'),
),
);
}
/**
* Implementation of hook_field_settings().
*/
function uc_product_content_field_settings($op, $field) {
switch ($op) {
case 'form':
$products = array();
$data = db_query("SELECT nid, title FROM {uc_products} AS p INNER JOIN {node} AS n ON p.nid = n.nid");
foreach ($data as $nid => $title) {
$products[$nid] = $title;
}
$form = array();
$form['product_options'] = array(
'#type' => 'checkboxes',
'#title' => t('Product Options'),
'#default_value' => isset($field['product_options']) ? $field['product_options'] : '',
'#options' => $products,
'#required' => TRUE,
'#description' => t('The group of products that may be selected.'),
);
return $form;
case 'save':
return array('product_options');
case 'database columns':
$columns = array(
'product_options' => array('type' => 'varchar', 'default' => "''"),
);
return $columns;
case 'views data':
$allowed_values = content_allowed_values($field);
if (count($allowed_values)) {
$data = content_views_field_views_data($field);
$db_info = content_database_info($field);
$table_alias = content_views_tablename($field);
// Filter: Add a 'many to one' filter.
$copy = $data[$table_alias][$field['field_name'] .'_value'];
$copy['title'] = t('@label (!name) - Allowed values', array('@label' => t($field['widget']['label']), '!name' => $field['field_name']));
$copy['filter']['handler'] = 'content_handler_filter_many_to_one';
unset($copy['field'], $copy['argument'], $copy['sort']);
$data[$table_alias][$field['field_name'] .'_value_many_to_one'] = $copy;
// Argument : swap the handler to the 'many to one' operator.
$data[$table_alias][$field['field_name'] .'_value']['argument']['handler'] = 'content_handler_argument_many_to_one';
return $data;
}
}
}
/**
* Implementation of hook_field().
*/
function uc_product_content_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
$allowed_values = content_allowed_values($field);
if (is_array($items)) {
foreach ($items as $delta => $item) {
$error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
if (is_array($item) && isset($item['_error_element'])) unset($item['_error_element']);
if (!empty($item['value'])) {
if (count($allowed_values) && !array_key_exists($item['product_options'], $allowed_values)) {
form_set_error($error_element, t('%name: illegal value.', array('%name' => t($field['widget']['label']))));
}
}
}
}
return $items;
}
}
/**
* Implementation of hook_content_is_empty().
*/
function uc_product_content_is_empty($item, $field) {
if (empty($item['product_options']) && (string)$item['product_options'] !== '0') {
return TRUE;
}
return FALSE;
}
/**
* Implementation of hook_field_formatter_info().
*/
function uc_product_content_field_formatter_info() {
return array(
'default' => array(
'label' => t('Default'),
'field types' => array('uc_product_content'),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
/**
* Implementation of hook_default_value()
*/
function uc_product_content_default_value(&$form, &$form_state, $field, $delta) {
return 0;
}
/**
* Implementation of hook_widget().
*
* Attach a single form element to the form. It will be built out and
* validated in the callback(s) listed in hook_elements. We build it
* out in the callbacks rather than here in hook_widget so it can be
* plugged into any module that can provide it with valid
* $field information.
*
* Content module will set the weight, field name and delta values
* for each form element. This is a change from earlier CCK versions
* where the widget managed its own multiple values.
*
* If there are multiple values for this field, the content module will
* call this function as many times as needed.
*
* @param $form
* the entire form array, $form['#node'] holds node information
* @param $form_state
* the form_state, $form_state['values'][$field['field_name']]
* holds the field's form values.
* @param $field
* the field array
* @param $items
* array of default values for this field
* @param $delta
* the order of this item in the array of subelements (0, 1, 2, etc)
*
* @return
* the form item for a single element for this field
*/
function uc_product_content_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
return $element;
}