array( 'arguments' => array('element'), ), 'asin_lookup' => array( 'arguments' => array('element'), ), 'asin_formatter_default' => array( 'arguments' => array('element'), ), 'asin_formatter_thumbnail' => array( 'arguments' => array('element'), ), 'asin_formatter_details' => array( 'arguments' => array('element'), ), // added for Haris - start 'asin_formatter_fornodes' => array( 'arguments' => array('element'), ), // added for Haris - end 'asin_formatter_inline' => array( 'arguments' => array('element'), ), ); } /** * Implementation of hook_field_info(). * * Here we indicate that the content module will use its default * handling for the view of this field. * * Callbacks can be omitted if default handing is used. * They're included here just so this module can be used * as an example for custom modules that might do things * differently. */ function asin_field_info() { return array( 'asin' => array( 'label' => t('Amazon item'), 'description' => t('Store the id of a product listed on Amazon.com.'), ), ); } /** * Implementation of hook_field_settings(). */ function asin_field_settings($op, $field) { switch ($op) { case 'database columns': $columns['asin'] = array('type' => 'varchar', 'length' => 32, 'not null' => FALSE); return $columns; case 'views data': $data = content_views_field_views_data($field); $db_info = content_database_info($field); $table_alias = content_views_tablename($field); // Swap the filter handler to the 'in' operator. $data[$table_alias][$field['field_name'] .'_asin']['filter']['handler'] = 'views_handler_filter_many_to_one_content'; // Add a relationship for related node. $data[$table_alias][$field['field_name'] .'_asin']['relationship'] = array( 'base' => 'amazon_item', 'field' => $db_info['columns']['asin']['column'], 'handler' => 'views_handler_relationship', ); return $data; } } /** * Implementation of hook_field(). */ function asin_field($op, &$node, $field, &$items, $teaser, $page) { switch ($op) { case 'validate': $results = _asin_load_items($items); foreach ($items as $delta => $item) { if (is_array($item)) { if (!empty($item['asin']) && empty($results[$item['asin']])) { form_set_error($field['field_name'] .']['. $delta .'][asin', t('%name : No Amazon product with the ID %id could be located.', array('%name' => t($field['widget']['label']), '%id' => $item['asin']))); } } } return $items; case 'load': $results = _asin_load_items($items); foreach ($items as $delta => $item) { if (!empty($item['asin'])) { $items[$delta] = $results[$item['asin']]; } } return $items; } } function _asin_load_items($items) { $asins = array(); foreach ($items as $delta => $item) { if (!empty($item['asin'])) { $asins[] = $item['asin']; } } return amazon_item_lookup($asins); } /** * Implementation of hook_content_is_empty(). */ function asin_content_is_empty($item, $field) { if (empty($item['asin'])) { return TRUE; } return FALSE; } /** * Implementation of hook_field_formatter_info(). */ function asin_field_formatter_info() { return array( 'default' => array( 'label' => t('Small image and basic info'), 'field types' => array('asin'), 'multiple values' => CONTENT_HANDLE_CORE, ), 'details' => array( 'label' => t('Small image and full info'), 'field types' => array('asin'), 'multiple values' => CONTENT_HANDLE_CORE, ), // added for Haris - start 'fornodes' => array( 'label' => t('Large image and full info'), 'field types' => array('asin'), 'multiple values' => CONTENT_HANDLE_CORE, ), // added for Haris - end 'thumbnail' => array( 'label' => t('Large image'), 'field types' => array('asin'), 'multiple values' => CONTENT_HANDLE_CORE, ), 'inline' => array( 'label' => t('Title as link'), 'field types' => array('asin'), 'multiple values' => CONTENT_HANDLE_CORE, ), ); } /** * Theme function for 'default' asin field formatter, appropriate for * general use and product listings. */ function theme_asin_formatter_default($element) { if ($asin = $element['#item']['asin']) { $asins = amazon_item_lookup(array($asin)); return theme('amazon_item', $asins[$asin]); } } /** * Theme function for 'thumbnail' asin field formatter, appropriate for * product hilighting and display. */ function theme_asin_formatter_thumbnail($element) { if ($asin = $element['#item']['asin']) { $asins = amazon_item_lookup(array($asin)); return theme('amazon_item', $asins[$asin], 'thumbnail'); } } /** * Theme function for 'full' asin field formatter, appropriate for * product hilighting and display. */ function theme_asin_formatter_details($element) { if ($asin = $element['#item']['asin']) { $asins = amazon_item_lookup(array($asin)); return theme('amazon_item', $asins[$asin], 'details'); } } // added for Haris - start /** * Theme function for 'fornodes' asin field formatter, appropriate for * product hilighting and display. */ function theme_asin_formatter_fornodes($element) { if ($asin = $element['#item']['asin']) { $asins = amazon_item_lookup(array($asin)); return theme('amazon_item', $asins[$asin], 'fornodes'); } } // added for Haris - end /** * Theme function for 'inline' asin field formatter, appropriate for * quick title lists, etc. */ function theme_asin_formatter_inline($element) { if ($asin = $element['#item']['asin']) { $asins = amazon_item_lookup(array($asin)); return theme('amazon_inline_item', $asins[$asin]); } } /** * Implementation of hook_widget_info(). * * We need custom handling of multiple values for the asin_text * widget because we need to combine them into a options list rather * than display multiple elements. * * We will use the content module's default handling for default value. * * Callbacks can be omitted if default handing is used. * They're included here just so this module can be used * as an example for custom modules that might do things * differently. */ function asin_widget_info() { return array( 'asin_text' => array( 'label' => t('Text field'), 'field types' => array('asin'), 'multiple values' => CONTENT_HANDLE_CORE, 'callbacks' => array( 'default value' => CONTENT_CALLBACK_DEFAULT, ), ), 'asin_lookup' => array( 'label' => t('Lookup by title'), 'field types' => array('asin'), 'multiple values' => CONTENT_HANDLE_CORE, 'callbacks' => array( 'default value' => CONTENT_CALLBACK_DEFAULT, ), ), ); } /** * Implementation of FAPI hook_elements(). * * Any FAPI callbacks needed for individual widgets can be declared here, * and the element will be passed to those callbacks for processing. * * Drupal will automatically theme the element using a theme with * the same name as the hook_elements key. */ function asin_elements() { return array( 'asin_text' => array( '#input' => TRUE, '#process' => array('asin_text_process'), ), 'asin_lookup' => array( '#input' => TRUE, '#process' => array('asin_lookup_process'), ), ); } /** * 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 asin_widget(&$form, &$form_state, $field, $items, $delta = 0) { $element = array( '#type' => $field['widget']['type'], '#default_value' => isset($items[$delta]) ? $items[$delta] : '', ); return $element; } /** * Process an individual element. * * Build the form element. When creating a form using FAPI #process, * note that $element['#value'] is already set. * * The $fields array is in $form['#field_info'][$element['#field_name']]. */ function asin_text_process($element, $edit, $form_state, $form) { $field = $form['#field_info'][$element['#field_name']]; $delta = $element['#delta']; $asin_key = $element['#columns'][0]; $element[$asin_key] = array( '#type' => 'textfield', '#title' => $element['#title'], '#description' => $element['#description'], '#required' => $element['#required'], '#default_value' => isset($element['#value'][$asin_key]) ? $element['#value'][$asin_key] : NULL, ); return $element; } /** * Validate an select element. * * Remove the wrapper layer and set the right element's value. */ function asin_text_validate($element, &$form_state) { array_pop($element['#parents']); form_set_value($element, $form_state['values'][$element['#field_name']], $form_state); } function theme_asin_text($element) { drupal_add_css(drupal_get_path('module', 'asin') .'/asin.css', 'module', 'all', FALSE); $output = $element['#children']; if (!empty($element['#value']) && !empty($element['#value']['asin'])) { $asin = $element['#value']['asin']; if ($data = amazon_item_lookup(array($asin))) { $output .= '

'. check_plain($data[$asin]['title']) .'

'; } } return $output; } /** * Process an individual element. * * Build the form element. When creating a form using FAPI #process, * note that $element['#value'] is already set. * * The $fields array is in $form['#field_info'][$element['#field_name']]. */ function asin_lookup_process($element, $edit, $form_state, $form) { $field = $form['#field_info'][$element['#field_name']]; $asin_key = $element['#columns'][0]; $element[$asin_key] = array( '#type' => 'hidden', '#required' => $element['#required'], '#default_value' => isset($element['#value'][$asin_key]) ? $element['#value'][$asin_key] : NULL, '#delta' => $element['#delta'], ); $element[''] = array( '#type' => 'textfield', '#title' => $element['#title'], '#description' => $element['#description'], '#required' => $element['#required'], '#field_name' => $element['#field_name'], '#type_name' => $element['#type_name'], ); // Used so that hook_field('validate') knows where to flag an error. $element['_error_element'] = array( '#type' => 'value', '#value' => implode('][', array_merge($element['#parents'], array($asin_key))), ); return $element; } /** * Validate an select element. * * Remove the wrapper layer and set the right element's value. */ function asin_lookup_validate($element, &$form_state) { array_pop($element['#parents']); form_set_value($element, $form_state['values'][$element['#field_name']], $form_state); } function theme_asin_lookup($element) { drupal_add_css(drupal_get_path('module', 'asin') .'/asin.css', 'module', 'all', FALSE); $output = $element['#children']; if (!empty($element['#value']) && !empty($element['#value']['asin'])) { $asin = $element['#value']['asin']; if ($data = amazon_item_lookup(array($asin))) { $output .= '

'. check_plain($data[$asin]['title']) .'

'; } } return $output; }