Hi

I am in the process of writing a cafepress module for DR7 and the first part is under review. I now want to extend this module and get the viewing under control. I would like to view the information in the same way as you view a node like www.example.com/cafepress/1. The entity "CPI_PRODUCTS" is working okay and i have a database full of the correct information. I have a cpi_products_load and save function. I also have the permissions setting correct. But there must be something wrong in the code. The code block is large so i will copy in what i think is importation if you want to have the complet code let me know.

****the error I get is page does not exist. *****(added later)

<?php

/**
 * Implements hook_permission().
 */
function cafepress_import_permission() {
  return array(
    'administer cafepress' =>  array(
      'title' => t('Administer Cafepress'),
    ),
    'view cafepress' =>  array(
    'title' => t('View Cafepress'),
    ),
  );
}

/**
 * Implements hook_entity_info().
 */
function cafepress_import_entity_info() {
  $return['cpi_storesection'] = array(
    'label' => t('Store Sections'),
    'controller class' => 'EntityAPIController',
    'base table' => 'cpi_storesection',
    'entity keys' => array(
      'id' => 'aid',
    ),
  );
  $return['cpi_products'] = array(
    'label' => t('Products in the stores'),
    'controller class' => 'EntityAPIController',
    'base table' => 'cpi_products',
    'uri callback' => 'cpi_products_uri',
    'entity keys' => array(
      'id' => 'aid',
    ),
  'view modes' => array(
    'full' => array(
      'label' => t('Full view cafepress'),
      'custom settings' =>  FALSE,
    ),
  )
  
  );
  return $return;
}

/**
 * Implement hook_menu().
 */
function cafepress_import_menu() {
  $items = array();
  $items['admin/config/services/config-cafepress'] = array(
    'title' => 'cafepress import configuration',
    'description' => 'The configuration Page for the cafepress_import Module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('config_cafepress_import_form'),
    'access arguments' => array('administer cafepress'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['cafepress/%cpi_products'] = array(
    'title callback' => 'cpi_products_page_title',
    'title arguments' => array(1),
    'page callback' => 'cpi_products_page_view',
    'page arguments' => array(1),
    'access arguments' => array('view cafepress'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function cpi_products_page_title($cpi_products) {
  return $cpi_products->name;
}

function cpi_products_page_view($cpi_products, $view_mode = 'full') {
  $cpi_products->content = array();

  // Build fields content.
  field_attach_prepare_view('cpi_products', array($cpi_products->name => $cpi_products), $view_mode);
  entity_prepare_view('cpi_products', array($cpi_products->name => $cpi_products));
  $cpi_products->content += field_attach_view('cpi_products', $cpi_products, $view_mode);

  return $cpi_products->content;
}

/**
 * Implementing the uri callback defined
 */
function cpi_products_uri($cpi_products) {
  return array(
    'path' => 'cafepress/' . $cpi_products->aid,
  );
}


/**
 * Load a single record.
 *
 * @param $id
 *    The id representing the record we want to load.
 */
function cpi_products_load($id, $reset = FALSE) {
  return cpi_products_load_multiple(array($id), $reset);
}

/**
 * This function returns a array of product records and has as input a array of AID numbers.
 */
function cpi_products_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('cpi_products', $ids, $conditions, $reset);
}

/**
 * This function saves a product array of information to the database table.
 * If the record already exists it will be over written. This is where the fields AID and CREATED are filled.
 */
function cpi_products_save($product) {

  $result = db_query("SELECT aid FROM {cpi_products} WHERE productid=:proid", array(':proid' => $product->productid,
    ));
  $sid              = $result->fetchField(0);
  $product->created = REQUEST_TIME;
  $product->aid     = $sid;
  $test             = cpi_products_load($sid);
  if ($test[$sid]->productid = $product->productid) {
    entity_delete('cpi_products', $sid);
  }
  entity_save('cpi_products', $product);
  return $product->aid;
}


?>

Any help is welcome.
Marcel

Comments

Anonymous’s picture

You haven't explained what the problem/error you're encountering is, without that no-one's going to be able to help!

mklynx’s picture

Sorry the problem is that I get a error page does not exist.

Marcel

Anonymous’s picture

Thanks for updating that, I'm not sure exactly why you're getting a 404 but there does seem to be an inconsistency in your code. You return an array of entities from cpi_products_load and that value is passed to cpi_products_page_view, which is expecting a single entity. Try changing your code in cpi_products_load to match the pattern used in (for example) the node_load function:

function node_load($nid = NULL, $vid = NULL, $reset = FALSE) {
  $nids = (isset($nid) ? array($nid) : array());
  $conditions = (isset($vid) ? array('vid' => $vid) : array());
  $node = node_load_multiple($nids, $conditions, $reset);
  return $node ? reset($node) : FALSE;
}

That will then return a single entity rather than an array.

Hope that helps

mklynx’s picture

I have changed the node_load function not 100% as above but it will now give a single result and not a array.

<?php
function cpi_products_load($id) {
  $result = entity_load('cpi_products', array($id));
  return $result ? reset($result) : FALSE;
}
?>

Here is also the wording of the error/problem

" The requested URL /cafepress/1 was not found on this server. "

And there is a cafepress item with id 1.

Marcel

Anonymous’s picture

Have you cleared your caches recently? If not you should do it, if you have then the only thing I can of is that there may be a conflict between your menu router item and the uri callback function for your entity. Try removing one of those uri assignment methods, clear your caches and see what happens. I can't see any reason why you'd have a router item and a uri callback for the entity that are the same, there would surely be a conflict. That could be way off though, I haven't really delved into the entity API yet!

mklynx’s picture

Hi
I have cleared the cache and even uninstalled then cleared the cache and reinstalled this module did not help.
My understanding is that you need both of them i found that some where in a description of how this works.
But i will try.

Marcel

Anonymous’s picture

You're right, just looking in node.module both path methods are implemented so whether or not it's required it's definitely not causing a conflict.

I think your best bet at this point is to find out exactly where the problem is happening. Try changing your 'cafepress/%cpi_products' router item to just return some text to test that your router item is active and made it into the db ok, then try building up an stdClass with some expected values in it and return it from cpi_products_load to see if it's the call to entity_load_multiple later on that's causing the problem. From there you'd be digging into the entity code itself to find out where your problem is.

mklynx’s picture

Hi

I have taken your advice and nothing strange has turned up. Not even errors in the logging.
I am not willing to go and dig around in the entity api my self.

There must be something wrong with either the router item or something that i am missing.
The only reason i am say this is because it feels like the router item is never recorded.

I have looked in the database and it is there.

Marcel

mklynx’s picture

HI

I just checked in the menu_links database tabel and i do see admin/config/services/config-cafepress but not the cafepress/%cpi_products recorded in there. It should be in that file so something is wrong with my hook_menu() implementation but i do not see it.

Marcel

Anonymous’s picture

I think because you've defined your menu item as MENU_CALLBACK it won't actually go into the menu_links table, check the menu_router table to make sure it's been registered properly. I honestly think the problem is in your cpi_products_load function though, have you tested that the function definitely works and returns the loaded entity as expected for a given id?

mklynx’s picture

Found in the menu_router table the record of cafepress/% that should be it.
Also tested the CPI_PRODUCTS_LOAD it works fine here are the results from the test.

<?php
$result = cpi_products_load(10);
print_r($result)
?>

Result

stdClass Object ( [aid] => 10 [productid] => 548056435 [name] => Maternity T-Shirt [merchandiseid] => 189 [sellprice] => 31.19 [marketplaceprice] => 20.99 [currencyname] => US Dollar [description] => [storeid] => Yapsalot2 [sectionid] => 7852266 [defaultcaption] => Maternity T-Shirt [categoryid] => 112 [categorycaption] => Shirts (short) [producturl] => http://www.cafepress.com/Yapsalot2.548056435 [imageurl280] => http://images5.cafepress.com/product/548056435v2_350x350_Front_Color-White.jpg [imageurl480] => http://images5.cafepress.com/product/548056435v2_480x480_Front_Color-White.jpg [created] => 1310123767 [rdf_mapping] => Array ( ) )

And if i tried to put in different numbers i get correct results.

Marcel

Anonymous’s picture

Very puzzling. That definitely should work then, there's nothing wrong with your hook_menu implementation as I can see it. The reason I'm taking such an interest is because this has happened to me before and to fix I had to re-implement the module with a different name. My best advice would be to disable your module, un-install it, remove all traces of it from the system table then try to install it again. Sorry I can't be more help, do let me know if you work this out.

mklynx’s picture

I did something even more drastic this morning.
I deleted the complete drupal site and created a complete new drupal site with the latest version.
Then i reinstalled all the modules and it still does not work.

Could it be that one of the functions only works with the module name and not with the cpi_products name ?

Marcel

Anonymous’s picture

Yeah I guess that's about the only thing left now, maybe cpi_products_load should be named cafepress_import_load and similar with the cpi_products_load_multiple function. Maybe defining an entity through the entity API requires that you follow the standard function naming convention, perhaps it would use the _load and _load_multiple functions as hooks in this context so the name would have to that of the module? And maybe having to define uri callbacks in some way overrides the normal menu item, which then fails because it can't find the _load or load_multiple functions?

That's all guess work but hope it helps sort it out!

mklynx’s picture

I just created a completely new module and kept all names the same as the module name.
It gives the same problem. I am missing something and i can not figure out what.

Marcel

mklynx’s picture

When i change the line

$items['cafepress/%cpi_products'] = array(

to

$items['cafepress/test'] = array(

I can get to the page by filling in the following in the url.

http://naa2go:8082/?q=cafepress/test

So there must be something wrong in the rest of the code. It does not seem to be the menu item.
But the load works. And everything else seems to be okay. I have been trying to figure this out for day's now.

Marcel