'value', '#value' => $item, ); $form['#attributes']['enctype'] = 'multipart/form-data'; $form['image_title_upload'] = array( '#type' => 'fieldset', '#title' => t('Title image'), '#collapsible' => TRUE, '#collapsed' => $collapsed, ); $form['image_title_upload']['image_title_upload'] = array( '#type' => 'file', '#title' => t('Attach title image'), '#size' => 40, ); if ($picture) { //show the image $form['image_title_upload']['current_title_image'] = array('#value' => $picture); //set the delete flag $form['image_title_upload']['image_title_delete'] = array( '#type' => 'checkbox', '#default_value'=> 0, '#title' => t('Delete title image.'), ); } $form['image_title_upload']['submit'] = array( '#value' => t('Save title image settings'), '#type' => 'submit', ); return $form; } /** * Validation for title image upload form on pages that are not nodes */ function image_title_menu_router_form_validate($form, &$form_state) { if(!empty($_FILES['files']['name']['image_title_upload'])) { $fullpath = file_directory_path().'/'.variable_get('image_title_directory', 'img-title'); //file_check_directory() creates a form error automatically file_check_directory($fullpath, 0, 'image_title_upload'); } } /** * Submit function for title image upload form on hook_menu provided pages */ function image_title_menu_router_form_submit($form, &$form_state) { //load any existing file data $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_menu} WHERE path = "%s"', $form_state['values']['menu_router_item']['path'])); //set delete flag according to form entry $delete = $form_state['values']['image_title_delete']; //if delete is requested, delete the unwanted image if ($delete == 1) { if (file_delete($image_title['image'])) { drupal_set_message(t('Title image deleted.')); } //we don't want the database record either db_query('DELETE FROM {image_title_menu} WHERE path = "%s"', $form_state['values']['menu_router_item']['path']); } //print_r($_FILES); //now handle the upload if(!empty($_FILES['files']['name']['image_title_upload'])) { //set upload parameters $fullpath = file_directory_path().'/'.variable_get('image_title_directory', 'img-title'); $validators = array( 'file_validate_is_image' => array(), ); $overwrite = 0; if ($file = file_save_upload('image_title_upload', $validators, $fullpath, $overwrite)) { file_set_status($file, FILE_STATUS_PERMANENT); //we don't want the old image - delete it //but make sure we don't delete the image we just uploaded if (!($file->filepath === $image_title['image'])) { file_delete($image_title['image']); } db_query('DELETE FROM {image_title_menu} WHERE path = "%s"', $form_state['values']['menu_router_item']['path']); db_query('INSERT INTO {image_title_menu} (path, image) VALUES ("%s", "%s")', $form_state['values']['menu_router_item']['path'], $file->filepath); drupal_set_message(t('Title image saved.')); } else { drupal_set_message(t('Failed to upload the title image.'), 'error'); } } } /** * Implementation of hook_form_alter() to add title image field to node forms */ function image_title_form_alter(&$form, $form_state, $form_id) { //check user is permitted to create image titles if (user_access('create image titles')) { //load an existing picture $node = $form['#node']; $picture = theme('image_title', $node->image_title); $collapsed = TRUE; if ($picture) { $collapsed = FALSE; } // We're only modifying node forms, if the type field isn't set we don't need // to bother; otherwise, store it for later retrieval. if (isset($form['type'])) { $type = $form['type']['#value']; } elseif (isset($form['orig_type'])) { $type = $form['orig_type']['#value']; } else { return; } // check node type and match settings if($form_id == $type .'_node_form' ){ $form['image_title'] = array( '#type' => 'fieldset', '#title' => t('Title image'), '#weight' => 40, '#collapsible' => TRUE, '#collapsed' => $collapsed, ); $form['image_title']['image_title_upload'] = array( '#type' => 'file', '#title' => t('Attach title image'), '#size' => 40, ); if ($picture) { //show the image $form['image_title']['current_title_image'] = array('#value' => $picture); // set the delete flag $form['image_title']['image_title_delete'] = array( '#type' => 'checkbox', '#default_value'=> 0, '#title' => t('Delete title image.'), ); } $form['#attributes']['enctype'] = 'multipart/form-data'; } } } /** * theming image field for title for node form .. * * @param $path * a string representing the full Drupal path to the image */ function theme_image_title($path) { if ($path && file_exists($path)) { $picture = theme('image', $path, t('Title image preview.'), t('Title image preview.'), '', FALSE); return "
$picture
"; } } /** * Implementation of hook_theme() */ function image_title_theme(){ return array( 'image_title' => array( 'arguments' => array('node' => NULL), ), ); } /** * Implementation of hook_nodeapi() to manage title images on nodes * (save, update, delete, etc.) */ function image_title_nodeapi(&$node, $op, $teaser, $page) { switch ($op) { case 'validate': if(!empty($_FILES['files']['name']['image_title_upload'])) { $path = file_directory_path(); $fullpath = $path.'/'.variable_get('image_title_directory', 'img-title'); if (!file_check_directory($fullpath)) { form_set_error('image_title_upload', t('Failed to upload the image title; the %directory directory doesn\'t exist or is not writable.', array('%directory' => $fullpath))); } } break; case 'update': case 'insert': //load any existing file data $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_node} WHERE nid = %d', $node->nid)); //set delete flag according to node form entry $delete = 0; if($node->image_title_delete) { $delete = $node->image_title_delete; } //if delete is requested, delete the unwanted image if ($delete == 1) { if (file_delete($image_title['image'])) { drupal_set_message(t('Title image deleted.')); } //we don't want the database record either db_query('DELETE FROM {image_title_node} WHERE nid = %d', $node->nid); } //now handle the upload if(!empty($_FILES['files']['name']['image_title_upload'])) { //set upload parameters $fullpath = file_directory_path().'/'.variable_get('image_title_directory', 'img-title'); $validators = array( 'file_validate_is_image' => array(), ); $overwrite = 0; if ($file = file_save_upload('image_title_upload', $validators, $fullpath, $overwrite)) { file_set_status($file, FILE_STATUS_PERMANENT); //we don't want the old image - delete it if (!($file->filepath === $image_title['image'])) { file_delete($image_title['image']); } db_query('DELETE FROM {image_title_node} WHERE nid = %d', $node->nid); db_query('INSERT INTO {image_title_node} (nid, image) VALUES (%d, "%s")', $node->nid, $file->filepath); } else { drupal_set_message(t('Failed to upload the title image.'), 'error'); } } break; case 'load': $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_node} WHERE nid = %d', $node->nid)); return array('image_title' => $image_title['image']); break; case 'delete': //clean up any orphan title images $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_node} WHERE nid = %d', $node->nid)); if (file_exists($image_title['image'])) { file_delete($image_title['image']); } //delete the record db_query('DELETE FROM {image_title_node} WHERE nid = %d', $node->nid); break; } } /** * function extending the available page variables * * replaces the text title with the title image, where available, and * offers the form for title image upload as a variable for theming */ function image_title_preprocess_page(&$variables) { if (arg(0) === 'node' && is_numeric(arg(1))) { $thisnode = node_load($variables['node']->nid); if($thisnode->image_title){ $image = theme('image', $thisnode->image_title, $thisnode->title, $thisnode->title, '', FALSE); $title = '' . $thisnode->title . '' . $image; $variables['title'] = $title; } } else { if (user_access('create image titles')) { //form for title image upload optionally available in a variable $variables['image_title_form'] = drupal_get_form('image_title_menu_router_form'); } //load menu item page data $item = menu_get_item(); //check for an image for this page if ($image_title = db_fetch_array(db_query('SELECT image FROM {image_title_menu} WHERE path = "%s"', $item['path']))) { $image = theme('image', $image_title['image'], $item['title'], $item['title'], '', FALSE); $title = '' . $item['title'] . '' . $image; $variables['title'] = $title; //we set another variable here because some modules may override $title //later on, so you can in turn override their values in your theme //template.php file with an appropriate preprocess containing simply: // $variables['title'] = $variables['image_title']; $variables['image_title'] = $title; } } } /** * Implementation of hook_block. * * offers title image upload form as a block */ function image_title_block($op = 'list', $delta = 0) { switch ($op) { case 'list': $block[0]['info'] = t('Title image upload'); return $block; case 'view': if (user_access('create image titles')) { switch ($delta) { case 0: $block['subject'] = t('Title image upload'); $block['content'] = drupal_get_form('image_title_menu_router_form'); break; } } return $block; } } /** * Implementation of hook_menu(). */ function image_title_menu() { //the admin settings page $items['admin/settings/image-title'] = array( 'title' => t('Image title'), 'description' => t('Settings for Image title module.'), 'page callback' => 'drupal_get_form', 'page arguments' => array('image_title_admin_form'), 'access arguments' => array('administer image titles'), 'type' => MENU_NORMAL_ITEM, 'file' => 'image_title.admin.inc', ); return $items; } /** * Implementation of hook_cron() * cron tasks to tidy up images for deleted pages outside the scope of nodeapi */ function image_title_cron() { $images = db_fetch_array(db_query('SELECT * FROM {image_title_menu}')); if (is_array($images)) { foreach ($images as $image) { if (!$menu_path = db_fetch_array(db_query('SELECT path FROM {menu_router} WHERE path = "%s"', $image['path']))) { file_delete($image['image']); db_query('DELETE FROM {image_title_menu} WHERE path = "%s"', $image['path']); } } } }