Error in my new flickr module that i can't see how to fix, need some help.

paulbooker - November 25, 2008 - 17:18

I am writing some code for the flickr module (please see below) which allows the possibility to create flickr nodes but when i try to edit a flickr node i get errors like ....

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, '0_node_form' was given in /Users/paulbooker/Subversion/Development/drupal6/includes/form.inc on line 366.

or
warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, '1_node_form' was given in /Users/paulbooker/Subversion/Development/drupal6/includes/form.inc on line 366.

etc

Can't see what is causing this error at the moment . Can anyone help me ?
Thanks Paul

<?php
// $Id: $
define ('FLICKR_NODE_TYPE_RECENTLY_ADDED', 0);
define ('FLICKR_NODE_TYPE_RANDOMLY_SELECTED', 1);
define ('FLICKR_NODE_SCOPE_ALL_ACCOUNTS', 0);
define ('FLICKR_NODE_SCOPE_SITE_ACCOUNT', 1);

require_once(drupal_get_path('module', 'flickr') .'/flickr.inc');

/**
* Implementation of hook_node_info().
*/
function flickr_node_node_info(){
  return array(
    'flickr_node' => array(
      'name' => t('Flickr node'),
      'has_title' => TRUE,
      'title_label' => t('Flickr node name'),
      'has_body' => TRUE,
      'body_label' => t('Flickr tags'),
      'module' => 'flickr_node',
    )   
  );
}

/**
* Implementation of hook_perm().
*/
function flickr_node_perm() {
  return array(
    'create a flickr node',
    'edit own flickr node',
    'edit all flickr entries',
    'administer flickr entries',
  );
}

/**
* Implementation of hook_menu().
*/
function flickr_node_menu() {
  $items['flickr_node/admin'] = array(
    'title' => 'Flickr nodes',
    'page callback' => 'flickr_node_admin',
    'access arguments' => array('administer flickr entries'),
  );
  $items['flickr_node/admin/denied_nsid'] = array(
    'title' => 'Flickr NSID users that are denied. ',
    'page callback' => 'flickr_node_admin_denied_nsid',
    'access arguments' => array('administer flickr entries'),
  );
  $items['flickr_node/admin/denied_id'] = array(
    'title' => 'Flickr ID images that are denied. ',
    'page callback' => 'flickr_node_admin_denied_id',
    'access arguments' => array('administer flickr entries'),
  );
  return $items;
}

function flickr_node_admin() {
   return  drupal_get_form('flickr_admin_entries_form');
}

function flickr_admin_entries_form(&$form_state) {
  $form['flickr-admin-entries']['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Submit'),
  );
  $form['flickr-admin-entries']['header'] = array('#type' => 'value', '#value' => array(
    array('data' => t('Name')),
    array('data' => t('Tags (AND only)')),
    array('data' => t('Select')),
    array('data' => t('From')),
    array('data' => NULL),
  ));
   $flickr_nodes = flickr_node_nodes();
   foreach ($flickr_nodes as $nid=>$flickr_node){
    $form['flickr-admin-entries']['name'][$nid] = array('#value' => $flickr_node['name']);
    $form['flickr-admin-entries']['tags'][$nid] = array('#value' => $flickr_node['tags']);
    $form['flickr-admin-entries']['select'][$nid] = array('#value' => $flickr_node['type'] == FLICKR_NODE_TYPE_RECENTLY_ADDED ? 'most recent' : 'at random');
    $form['flickr-admin-entries']['from'][$nid] = array('#value' => $flickr_node['scope'] == FLICKR_NODE_SCOPE_ALL_ACCOUNTS ?  'all accounts' : 'site account');
    $form['flickr-admin-entries']['block'][$nid] = array('#value' => l('edit flickr node', "node/".$nid.'/edit'));
  }  
  return $form['flickr-admin-entries'];
}

function theme_flickr_admin_entries_form($form) {
  if (isset($form['name']) && is_array($form['name'])) {
    foreach (element_children($form['name']) as $key) {
      $row = array();
      $row[] = drupal_render($form['name'][$key]);
      $row[] = drupal_render($form['tags'][$key]);
      $row[] = drupal_render($form['select'][$key]);
      $row[] = drupal_render($form['from'][$key]);
      $row[] = drupal_render($form['block'][$key]);    
      $rows[] = $row;
    }
  }
  else {
    $rows[] = array(array('data' => l('Create a flickr node', 'node/add/flickr-node'), 'colspan' => '6'));
  }
  $output .= theme('table', $form['header']['#value'], $rows, array(), t('Flickr nodes'));
  if ($form['pager']['#value']) {
    $output .= drupal_render($form['pager']);
  }
  $output .= drupal_render($form);
  return $output;
}

/**
* Implementation of hook_theme().
*/
function flickr_node_theme() {
  return array(
    'flickr_admin_entries_form' => array(
      'arguments' => array(),
    ),
  );
}

/**
* Implementation of hook_form().
*/
function flickr_node_form(&$node,  $form_state) {  
  // The site admin can decide if this node type has a title and body, and how
  // the fields should be labeled. We need to load these settings so we can
  // build the node form correctly.
  $type = node_get_types('type', $node);
  // Existing node fields:  title (Flickr node name) and body (Flickr tags)
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' =>  check_plain($type->title_label),
      '#required' => TRUE,
      '#default_value' => $node->title,
      '#weight' => -5,
    );
  }
  if ($type->has_body) {
    $form['body_field'] = node_body_field(
      $node,
      $type->body_label,
      $type->min_word_count
    );
  }
  // Now we define the form elements specific to our node type.
  $form['flickr_node_type'] = array(
    '#type' => 'select',
    '#title' => t('Flickr photos that are'),
    '#options' => array(FLICKR_NODE_TYPE_RECENTLY_ADDED => 'recently added', FLICKR_NODE_TYPE_RANDOMLY_SELECTED => 'randomly selected'),
    '#description' => t("Flickr photos that are either recently added or randomly selected"),
    '#default_value' => isset($node->flickr_node_type) ? $node->flickr_node_type : "",
  );
  $form['flickr_node_scope'] = array(
    '#type' => 'select',
    '#title' => t('Flickr photos that are'),  
    '#options' => array(FLICKR_NODE_SCOPE_SITE_ACCOUNT => 'collected from the default site account', FLICKR_NODE_SCOPE_ALL_ACCOUNTS => 'collected from all flickr accounts'),
    '#description' => t("Flickr photos that are collected from all accounts or only the default site account"),
    '#default_value' => isset($node->flickr_node_scope) ? $node->flickr_node_scope : "",
  );
  $form['flickr_node_implementations'] = array(
  '#type' => 'checkboxes',
  '#title' => t('Implementation options'),
  '#options' => array(
    'block' => t('Block'), 'photoset' => t('Photoset'), 'filter' => t('Filter'),
   ),
   '#default_value' => isset($node->flickr_node_implementations) ? $node->flickr_node_implementations : array(),
   '#description' => t('Select how you would like to have this flickr node implemented.'),
);

return $form;   
}

/**
* Implementation of hook_insert().
*/
function flickr_node_insert($node) {
   if (!isset($node->flickr_node_type)) {
     $node->flickr_node_type = '';
   }
   if (!isset($node->flickr_node_scope)) {
     $node->flickr_node_scope = '';
   }
   $implementations = implode(',', array_filter($node->flickr_node_implementations));
   db_query(
      'INSERT INTO {flickr_node} (nid, type, scope, implementations) '
       ."VALUES (%d, %d, %d,'%s')",
       $node->nid,
       $node->flickr_node_type,
       $node->flickr_node_scope,
       $implementations
   );
}

/**
* Implementation of hook_load().
*/
function flickr_node_load($node) {
  $result = db_query("SELECT * FROM {flickr_node} WHERE  nid = %d",$node->nid);
  return db_fetch_object($result);
}

/**
* Implementation of hook_update().
*/
function flickr_node_update($node) {
   if ($node->revision) {
     flickr_node_insert($node);
   }
   else {
     db_query("UPDATE {flickr_node} SET type = %d, scope =%d,  active=%d, block=%d WHERE nid = %d",
       $node->nid,
       $node->flickr_node_type,
       $node->flickr_node_scope,
       $implementations
     );
  }
}

/**
* Implementation of hook_block().
*/
function flickr_node_block($op = 'list', $delta = 0, $edit = array()) {
  $flickr_nodes = flickr_node_nodes();
  switch ($op) {
  case 'list':
      foreach ($flickr_nodes as $delta => $flickr_node) { 
         $blocks[$delta]['info']  = "Flickr node : " . $flickr_node['name'];
      }
   return $blocks;

  case 'configure':
     $count_options = array(1 => '1', 2 => '2', 3 => '3',  4 => '4', 5 => '5', 6 => '6', 7 => '7',  8 => '8', 9 => '9', 10 => '10', 15 => '15', 20 => '20', 25 => '25', 30 => '30');
    // remove the large and original sizes
    $size_options = array();
    foreach (flickr_photo_sizes() as $size => $info) {
      $size_options[$size] = $info['label'] .' - '. $info['description'];
    }
    unset($size_options['b']);
    unset($size_options['o']);
    $settings = variable_get('flickr_block_'. $delta, array('user_id' => '', 'show_n' => 4, 'size' => 's'));
    $form = array();
    $form["flickr_block_{$delta}_user_id"] = array(
      '#type' => 'textfield',
      '#title' => t('Flickr User Id'),
      '#default_value' => $settings['user_id'],
      '#description' => t("The user id of a Flickr user. If this is left blank, the sites's default user will be used. Current default id is " . variable_get('flickr_default_userid', '')),
    );
    $form["flickr_block_{$delta}_show_n"] = array(
      '#type' => 'select',
      '#options' => $count_options,
      '#title' => t('Show the last <em>n</em> photos'),
      '#default_value' => $settings['show_n'],
      '#description' => t("The block will display this many photos.")
    );
    $form["flickr_block_{$delta}_size"] = array(
      '#type' => 'select',
      '#options' => $size_options,
      '#title' => t('Size of photos'),
      '#default_value' => $settings['size'],
      '#description' => t("Select the size of photos you'd like to display in the block.")
    );
    return $form;
  case 'save':
      variable_set('flickr_block_'. $delta, array(
        'user_id' => $edit["flickr_block_{$delta}_user_id"],
        'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
        'size' => $edit["flickr_block_{$delta}_size"],
      ));
    break;
  case 'view': default:
drupal_add_css(drupal_get_path('module', 'flickr') .'/flickr.css');
    $settings = variable_get('flickr_block_'. $delta, array(
      'user_id' => '',
      'show_n' => 4,
      'size' => 's',
    )); 
$block['subject'] = t('Flickr node : ' . $flickr_nodes[$delta]['name']);
$flickr_node_node = flickr_node_node($delta);
if ( $flickr_node_node['scope'] == FLICKR_NODE_SCOPE_SITE_ACCOUNT) {
    $flickr_username_default = variable_get('flickr_default_userid','');
    $settings['user_id'] = _flickr_node_get_nsid_from_flickr_username($flickr_username_default);

    $block['content'] = _flickr_node_block($settings['user_id'], $settings['show_n'], $settings['size'],  $flickr_node_node);   
    return $block;
  }
}

function flickr_node_nodes() {
  $result = db_query("SELECT n.nid, nr.title, nr.body, fn.type, fn.scope  FROM {node_revisions} nr INNER JOIN {node} n ON nr.nid = n.nid INNER JOIN {flickr_node} fn  ON n.nid = fn.nid  WHERE n.type = 'flickr_node'");
  while ($node = db_fetch_object($result)) {
    $flickr_node = array(
       'name' => $node->title,
       'tags' => $node->body, 
       'type' => $node->type,
       'scope' => $node->scope,
    );
    $flickr_nodes[$node->nid] = $flickr_node;
  }  
  return count($flickr_nodes) ? $flickr_nodes : array() ;
}

function flickr_node_node($nid) {
  $result = db_query("SELECT n.nid, nr.title, nr.body, fn.type, fn.scope, fn.active  FROM {node_revisions} nr INNER JOIN {node} n ON nr.nid = n.nid INNER JOIN {flickr_node} fn  ON n.nid = fn.nid  WHERE n.type = 'flickr_node' AND  n.nid = %d",$nid);
  $node = db_fetch_object($result);
  $flickr_node = array(
       'name' => $node->title,
       'tags' => $node->body, 
       'type' => $node->type,
       'scope' => $node->scope,
       'active' => $node->active,
    );
  return $flickr_node;
}

function _flickr_node_block($user_id, $show_n, $size, $flickr_node) {
  $tags = $flickr_node['tags'];
  if (($flickr_node['type'] == FLICKR_NODE_TYPE_RANDOMLY_SELECTED) && ($flickr_node['scope'] == FLICKR_NODE_SCOPE_ALL_ACCOUNTS)) {  
    return  _flickr_node_block_random(NULL, $show_n, $size, $tags);
  }
  if (($flickr_node['type'] == FLICKR_NODE_TYPE_RECENTLY_ADDED) && ($flickr_node['scope'] == FLICKR_NODE_SCOPE_ALL_ACCOUNTS)) {
    return _flickr_node_block_recent(NULL, $show_n, $size, $tags);
  }
  if (($flickr_node['type'] == FLICKR_NODE_TYPE_RANDOMLY_SELECTED) && ($flickr_node['scope'] == FLICKR_NODE_SCOPE_SITE_ACCOUNT)) { 
    return _flickr_node_block_random($user_id, $show_n, $size, $tags);
  }
  if (($flickr_node['type'] == FLICKR_NODE_TYPE_RECENTLY_ADDED) && ($flickr_node['scope'] == FLICKR_NODE_SCOPE_SITE_ACCOUNT)) {
    return _flickr_node_block_recent($user_id, $show_n, $size, $tags);
  }
}

function _flickr_node_block_random($nsid, $show_n, $size, $tags = "") { 
  $request =  array('user_id' => $nsid,'per_page' => $show_n ,'tags' => $tags, 'tag_mode' => 'all');
  $result = flickr_request('flickr.photos.search', $request);
  $page_count = $result['photos']['pages'];
  $to = min($show_n, $result['photos']['total']);
  $output = '';
  $photos = array();
  for ($i = 0; $i < $to; $i++) {
    sleep(0.125);
    // request a random page
    $request['page'] = rand(1, $page_count - 1);
    $result = flickr_request('flickr.photos.search', $request);
    // then select a random photo  
    $index = rand(0, count($result['photos']['photo']) - 1); 
    $photo_id = $result['photos']['photo'][$index]['id'];
    if (in_array($photo_id, $photos)) {
      $i--; // photo already added
    }
    else {
       if (_flickr_nsid_denied($result['photos']['photo'][$index]['owner']) || _flickr_id_denied($result['photos']['photo'][$index]['id']) ) {
        $i--; // photo was denied
        continue;
      }
      else {
        $photos[] = $photo_id;
        $output .= theme('flickr_photo', $result['photos']['photo'][$index], $size); 
      }
    }
  }
  return $output ;
}

function _flickr_node_block_recent($nsid, $show_n, $size, $tags = "") { 
  $request = array('user_id' => $nsid, 'per_page' => $show_n, 'tags' => $tags,  'tag_mode' => 'all'); 
  $result = flickr_request('flickr.photos.search', $request);  
  $output = ''; 
  foreach ((array)$result['photos']['photo'] as $photo) {
    if (_flickr_nsid_denied($photo['owner']) || _flickr_id_denied($photo['id'])) {
      continue; 
    }
    $output .= theme('flickr_photo', $photo, $size);
  }
  return $output;
}

function _flickr_nsid_denied($nsid) { 
  $result = db_result(db_query("SELECT COUNT(nsid)  FROM  {flickr_nsid_denied}  WHERE nsid = '%s'", $nsid));
  return $result;
}

function _flickr_id_denied($id) { 
  $result = db_result(db_query("SELECT COUNT(id)  FROM  {flickr_id_denied}  WHERE id = '%s'", $id));
  return $result;
}

function flickr_node_admin_denied_nsid() {
   return drupal_get_form('flickr_node_admin_denied_nsid_form');
}

function flickr_node_admin_denied_id() {
  return drupal_get_form('flickr_node_admin_denied_id_form');
}


function flickr_node_admin_denied_nsid_form(&$form_state) {
  $form['#validate'] = array('flickr_node_admin_denied_nsid_form_validate' => array());
  $form['flickr_denied_nsid'] = array(   
    '#type' => 'textfield',
    '#title' => t('Enter an flickr user NSID that is to be denied'),
    '#required' => TRUE,
  );
  $form['flickr_denied_nsid_submit'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
  );
   return $form;
}

function flickr_node_admin_denied_nsid_form_submit($form, &$form_state) {
  $form_values = $form_state['values'];
  db_query( "INSERT INTO {flickr_nsid_denied} (nsid) VALUES ('%s')", $form_values['flickr_denied_nsid']);
  drupal_set_message("Flickr user NSID " . $form_values['flickr_denied_nsid'] . " is now denied");
}

function flickr_node_admin_denied_id_form(&$form_state) {
  $form['#validate'] = array('flickr_node_admin_denied_id_form_validate' => array());
  $form['flickr_denied_id'] = array(   
    '#type' => 'textfield',
    '#title' => t('Enter an flickr image ID that is to be denied'),
    '#required' => TRUE,
  );
   $form['flickr_denied_id_submit'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
  );
  return $form;
}

function flickr_node_admin_denied_id_form_submit($form, &$form_state) {
  $form_values = $form_state['values'];
  $form_values['flickr_denied_id'] ;
  db_query( 'INSERT INTO {flickr_id_denied} (id) VALUES (%d)', $form_values['flickr_denied_id']);
  drupal_set_message("Flickr image ID " . $form_values['flickr_denied_id'] . " is now denied");
}

function _flickr_node_get_nsid_from_flickr_username($flickr_username) {
$result = db_result(db_query("SELECT nsid FROM {flickr_users} WHERE identifier = '%s'",$flickr_username));
return $result ? $result : NULL;
}

It looks as though

paulbooker - November 25, 2008 - 17:41

It looks as though hook_form() is not being called at all when i try to edit a node of flickr-node content type

--
Paul Booker
Drupal Developer

Thank you to Glaxstar for sponsoring all the work done on modules that we develop and maintain

http://www.glaxstar.com

Fixed.

paulbooker - November 26, 2008 - 08:57

The problem was that in my flickr_node table i had a field called "type" which took numeric values and which was overriding the "type" field in the node table when the node object was being created for a flickr_node content type.

Hope that saves other people some time.

--
Paul Booker
Drupal Developer

Thank you to Glaxstar for sponsoring all the work done on modules that we develop and maintain

http://www.glaxstar.com

Thanks!

wdmartin - February 18, 2009 - 02:51

That did in fact save me some time. Thanks!

Note to self: never re-use column names from the node table in a custom content type.

 
 

Drupal is a registered trademark of Dries Buytaert.