FALSE, 'handler' => array( 'parent' => 'UcAddressesFieldHandler', 'class' => 'UcAddressesTextFieldHandler', 'file' => 'uc_addresses.handlers.inc', 'path' => $path_uc_addresses_node, ), ); /* //zakomentoval jsem to protože, tento handler je již načtený přes modul uc_addresses! $info['UcAddressesHiddenFieldHandler'] = array( 'hidden' => FALSE, 'handler' => array( 'parent' => 'UcAddressesFieldHandler', 'class' => 'UcAddressesHiddenFieldHandler', 'file' => 'uc_addresses.handlers.inc', 'path' => $path_uc_addresses, ), ); */ return $info; } /** * Implements hook_uc_addresses_fields(). * * Registers all extra address fields for Ubercart Addresses. * * @return array */ function uc_addresses_node_uc_addresses_fields() { module_load_include('fields.inc', 'uc_addresses_node'); return _uc_addresses_node_schema_fields(); } // --------------------------------------------------------------------------- // CORE NODE HOOKS // --------------------------------------------------------------------------- /** * Implementation of hook_node_info() */ function uc_addresses_node_node_info() { return array( 'uc_addresses_node' => array( 'name' => t('Address'), 'module' => 'uc_addresses_node', 'description' => "An Address content type.", ) ); } /** * Implementation of hook_perm() */ function uc_addresses_node_perm() { return array('create address', 'edit own address', 'edit any address', 'delete own address', 'delete any address'); } /** * Implementation of hook_access() */ function uc_addresses_node_access($op, $node, $account) { $is_author = $account->uid == $node->uid; switch ($op) { case 'create': // Allow if user's role has 'create address' permission. return user_access('create address', $account); case 'update': // Allow if user's role has 'edit own address' permission and user is the author; // or if the user's role has 'edit any address' permission. return user_access('edit own address', $account) && $is_author || user_access('edit any address', $account); case 'delete': // Allow if user's role has 'delete own address' permission and user is the author; // or if the user's role has 'delete any address' return user_access('delete own address', $account) && $is_author || user_access('delete any address', $account); } } /** * Implementation node edit form */ function uc_addresses_node_form(&$node, $form_state) { global $user; $type = node_get_types('type', $node); // We need to define form elements for the node's title and body. $form['title'] = array( '#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5 ); // We want the body and filter elements to be adjacent. We could try doing // this by setting their weights, but another module might add elements to the // form with the same weights and end up between ours. By putting them into a // sub-array together, we're able force them to be rendered together. $form['body_filter']['body'] = array( '#type' => 'textarea', '#title' => check_plain($type->body_label), '#default_value' => $node->body, '#required' => FALSE ); $form['body_filter']['filter'] = filter_form($node->format); if (!empty($node->aid)) { // Example 1: create an edit form for address ID 2 from user 1. $address = UcAddressesAddressBook::get($user->uid)->getAddressById($node->aid); $form['address'] = array( '#type' => 'uc_addresses_address', '#uc_addresses_address' => $address, ); } else { // Example 2: create an edit form for a new address for user 1. $address = UcAddressesAddressBook::get($user->uid)->addAddress(); $form['address'] = array( '#type' => 'uc_addresses_address', '#uc_addresses_address' => $address ); } //$form['#submit'] = array('uc_addresses_node_node_form_submit_handler'); return $form; } /** * Validation handler for uc_addresses_node_form(). * * Validates address field e-mail address */ function uc_addresses_node_form_validate($form, &$form_state) { /* $mail = trim($form_state['values']['uc_store_email']); if (!valid_email_address($mail)) { form_set_error('uc_store_email', t('The e-mail address %mail is not valid.', array('%mail' => $mail))); } */ } function array_object_merge(&$object, $array) { foreach ($array as $key => $value) $object->{$key} = $value; } /** * Implementation of hook_insert() * * The $object can have more properties than the table has columns * * You can use an object such as a node or form results - which may have more columns than your table. * Only the matching properties will be inserted into the table. * As long as your schema matches your form setup in a custom node type you make a hook_insert as simple as */ function uc_addresses_node_insert($node) { if (!empty($node->address['aid'])) { $node->aid = $node->address['aid']; drupal_write_record('uc_addresses', $node, 'aid'); } //dsm($node); } /** * Implementation of hook_load() */ function uc_addresses_node_load($node) { //return db_fetch_object(db_query('SELECT * FROM {uc_addresses_node} ucan INNER JOIN {uc_addresses} uca ON ucan.aid = uca.aid WHERE ucan.vid = %d', $node->vid)); //return db_fetch_object(db_query('SELECT * FROM {uc_addresses} WHERE vid = %d', $node->vid)); return db_fetch_object(db_query('SELECT * FROM {uc_addresses} WHERE nid = %d', $node->nid)); } /** * Implementaton of hook_view() */ function uc_addresses_node_view($node, $teaser = FALSE, $page = FALSE) { // If $teaser is FALSE, the entire node is being displayed. if (!$teaser) { // Use Drupal's default node view $node = node_prepare($node, $teaser); module_load_include('pages.inc', 'uc_addresses'); $address_user = user_load(array('uid' => $node->uid)); $address = UcAddressesAddressBook::get($node->uid)->getAddressById($node->aid); $address_output = uc_addresses_list_one_address($address_user, $address); // Add custom fields to the node object. $node->content['store'] = array ( '#value' => $address_output, '#weight' => 2 ); } // If $teaser is TURE, node is being displayed as a teaser, // such as on a node listing page. We omit the address fields in this case. if ($teaser) { // Use Drupal's default node view. $node = node_prepare($node, $teaser); } return $node; } /** * Implementation of hook_update() */ function uc_addresses_node_update($node) { if (!empty($node->revision)) { // New revision; treat it as a new record uc_addresses_node_insert($node); } else { //$result = drupal_write_record('uc_addresses', $node, 'nid'); //if ($result == SAVED_UPDATED && db_affected_rows() > 0) { // stuff to do if the record was really updated //drupal_set_message('node has been updated.',"warning"); //} //else { // stuff to do if it wasn't //drupal_set_message('node has not been updated.',"warning"); //} } } /** * Implementation of hook_delete() */ function uc_addresses_node_delete($node) { // Delete the related information we were saving for this node //db_query('DELETE FROM {uc_addresses_node} WHERE nid = %d', $node->nid); db_query('DELETE FROM {uc_addresses} WHERE aid = %d', $node->aid); } /** * Implementation of hook_nodeapi() * When a revision rather than the entire node is deleted, Drupal fires this hook * with the $opnset to 'delete revision', and the entire node object is passed in. * Your module is then able to delete its data for that revision using $node->vid as the key. */ function uc_addresses_node_nodeapi(&$node, $op) { switch ($op) { case 'delete revision': db_query('DELETE FROM {uc_addresses} WHERE vid = %d', $node->vid); break; } } /* * Implementation of hook_form_alter() */ function uc_addresses_node_form_alter(&$form, &$form_state, $form_id){ // target a single form if($form_id == "uc_addresses_node_node_form"){ // dsm($form); // Add my extra submition & validation functions $form['#submit'][] = 'uc_addresses_node_node_form_submit_handler'; } } function uc_addresses_node_node_form_submit_handler($form, &$form_state) { $address = $form['address']['#uc_addresses_address']; $address->save(); $form_state['values']['address']['aid'] = $address->getId(); } function uc_addresses_node_init(){ drupal_add_css(drupal_get_path('module', 'uc_addresses_node') .'/uc_addresses_node.css'); }