As you can see from the title, I am using Views to display products from the Commerce module. Each "product" is a download, which required a download field. Upon first install, I re-used the publicly-available "upload" field on the Commerce product (not the node). Built the views to follow the "Tie to Product" relationship and everything worked great.

Now it seems that I may need to restrict access to some of the downloads. I removed the "upload" field and replaced it with a "private" file field. This causes the following errors every time that I access the view as a page, or when saving:

Notice: Undefined index: commerce_product in views_handler_field_field->access() (line 78 of [local-path]/sites/all/modules/views/modules/field/views_handler_field_field.inc).
Notice: Undefined index: commerce_product in views_handler_field_field->access() (line 78 of [local-path]/sites/all/modules/views/modules/field/views_handler_field_field.inc).

I have tried removing the Views relationship and recreating it. That does not seem to help, so for now I am covering up the error messages by re-adding "upload" field to the Commerce product.

Any ideas how to correct the view's dependence on this field? The row format is "grid" so there is no selected use of the "upload" field.

CommentFileSizeAuthor
#1 1477054.patch687 bytesdawehner

Comments

dawehner’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new687 bytes

It's somehow an interesting question why commerce_product is not added to the entity_tables defined there.

This patch fixes the issue, though this doesn't allow to understand the original issue and it's definitive a bit slower.

tim.plunkett’s picture

Status: Active » Postponed (maintainer needs more info)

Can you provide an exported view to test this with? Paste it in on http://paste.pocoo.org and copy a link here.

I've tried to reproduce as best I can, but I can't. Clean install of commerce, it worked fine.
field_views_field_default_views_data() adds this information in, and I see no reason it would break like this.

Also, views_handler_field_field::query() uses this same pattern, so if there was a real reason for this, $this->definition['entity_tables'] should just be abandoned altogether.

johhan’s picture

Status: Needs review » Postponed (maintainer needs more info)

Hi,

on page with View definition (the "SQLSTATE..." is down on the page):

Notice: Undefined index: commerce_product ve funkci views_handler_field_field->access() (řádek: 78 v souboru D:\drupal\www\sites\all\modules\views\modules\field\views_handler_field_field.inc).

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS commerce_product_, '' AS field_data_commerce_product__entity_type FROM vp_co' at line 1

and on page with View:

Notice: Undefined index: commerce_product ve funkci views_handler_field_field->access() (řádek: 78 v souboru D:\drupal\www\sites\all\modules\views\modules\field\views_handler_field_field.inc).
Notice: Undefined index: commerce_product ve funkci views_handler_field_field->query() (řádek: 134 v souboru D:\drupal\www\sites\all\modules\views\modules\field\views_handler_field_field.inc).
Notice: Undefined index: entity keys ve funkci views_handler_field_field->query() (řádek: 146 v souboru D:\drupal\www\sites\all\modules\views\modules\field\views_handler_field_field.inc).

This error messages started after adding "Commerce Line item: Product" field into Field(s) part of the View definition.

This View exported here:
http://paste.pocoo.org/show/587441/

Thank you
John

P.S. Patch 1477054.patch not tried for now

uking’s picture

I got the same error and patch the function access() and query() with success.

In function query () there is this addional change necessary
$base_table_data = views_fetch_data($base_table);
to
$base_table_data = views_fetch_data($this->base_table);

dawehner’s picture

Status: Postponed (maintainer needs more info) » Needs work

@johhan Sorry pocoo is dead :(

@uking
Would you like to redo the patch, based on your change?

nmc’s picture

I have the same issue. Here is the View export available at http://pastebin.com/P2qza7Ys

Applying patch #1 fixes the errors of:
Notice: Undefined index: commerce_product in views_handler_field_field->access() (line 78 of [local-path]/sites/all/modules/views/modules/field/views_handler_field_field.inc).

But SQL error remains:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS commerce_product_field_data_commerce_product_, 'commerce_product' AS field_da' at line 1

dydave’s picture

Hi guys,

I had the exact same error after updating views from 3.3 to 3.5.
Right after running the update.php (which actually did nothing, btw, but just in case), prompted the error message from this post (Notice/Warning).

I went in the module as indicated by the warning and did a:
if (!isset($this->definition['entity_tables'][$base_table])) dpm($this);
[/views/modules/field/views_handler_field_field.inc around line 127]
which allowed me to trace down the view name, display and *problematic field*.
Then I went in the corresponding view/display, removed the field and added it again exactly as it was before (default config, actually), then finally saved.

It seems this was enough to make the warning go away and after flushing cache or running update.php again, there is no more notice/error like this one.
I would assume that there was some bad/deprecated/old/corrupt data in my views and removing/adding/saving did the trick in this case.
However, I haven't investigated exactly what could be the cause of the problem and would be interested to know if anyone figures this out.

I hope this comment will help others that would encounter the same issue as we did.

Feel free to let me know what you think about this approach and I would be glad to hear if you would have any other recommendations or advice.
Thanks in advance.

Lars Bo Jensen’s picture

Thank you for your post, DYdave, it was informative. I got the issue after deleting the field_image from a product type. I added it again, and everything seems fine. Ca it be related to this (old) issue: http://drupal.org/node/1265152 ?

dydave’s picture

Thanks a lot for your reply Lars and the track you suggested.
Let's see if we can get other developers in the Drupal Commerce tracker involved.
I have posted a ticket at: http://drupal.org/node/1802924 with a quick summary of links to this ticket.

Perhaps we'll mark new ticket as duplicate and move this ticket over to Drupal Commerce tracker. First, let's see what developers have to say about this new ticket and if anyone is interested in working on that.

Until then, I guess we'll have to use some kind of workaround as suggested in this ticket.

Any further feedback would be greatly appreciated.
Thanks in advance.

summit’s picture

Hi,
I tried all this in this thread but I have still:

    Notice: Undefined index: commerce_product in views_handler_field_field->access() (line 127 of /sites/all/modules/views/modules/field/views_handler_field_field.inc).
    Notice: Undefined index: commerce_product in views_handler_field_field->query() (line 186 of /sites/all/modules/views/modules/field/views_handler_field_field.inc).
    Notice: Undefined index: entity keys in views_handler_field_field->query() (line 198 of /sites/all/modules/views/modules/field/views_handler_field_field.inc).

This happens when I make a relation between a content: referenced product and the product within the showing of the field.
Greetings, Martijn

dydave’s picture

Hi @Summit,

Did you try the approach suggested in #7, removing and adding the field again?

Try to remove the product reference field and the relationship, then save the view, then add them again exactly as they were before and try saving again.

Have you tested that already?

Thanks in advance for your reporting, testing and comments.
Cheers!

summit’s picture

Hi,

I tried all that, but adding this field: (Product) Content: Product reference with relation Content: Products.
I got these errors.

Greetings, Martijn

dydave’s picture

@Summit:
Thanks again for your prompt feedback.
Just a quick thing to confirm:
Did you make sure you flushed the cache also after/during doing all this?
Could you maybe try again and flush the cache?

Otherwise:
Ideally, it would be great if you could do some debugging with devel:
Have you tried:

I went in the module as indicated by the warning and did a:
if (!isset($this->definition['entity_tables'][$base_table])) dpm($this);
[/views/modules/field/views_handler_field_field.inc around line 127]
which allowed me to trace down the view name, display and *problematic field*.

On top of that, it would really help if you could also paste here (with some formatting) the debug backtrace from Commerce Devel (just need to install/enable the module and the backtrace can easily be copied from the Recent log messages detail view of the error log message).
Based on that we could perhaps give you further guidance on how to place more traces in the code.

Otherwise it could be quite cumbersome (for all of us) to help you on this, since we would need to be able to reproduce the issue entirely on our local environments which would mean that we would need all your modules versions, plus all the objects/fields/views that would be defined on your site (to be able to touch the code and debug/backtrace/trace down quickly)....

Any additional help, comments, feedback, advice, suggestions, recommendations, questions or issues would certainly be highly appreciated.

Thanks very much to all in advance for your comments, suggestions and testing.
Cheers!

summit’s picture

Hi, Will try to do this tomorrow, otherwise it stalls a week because EasternHolidays!
greetings, Martijn

summit’s picture

Hi,
I know which field it is: (Product) Content: Product reference
Devel and Commerce Devel logging gave me:

MESSAGE	Notice: Undefined index: entity keys in views_handler_field_field->query() (line 198 of /sites/all/modules/views/modules/field/views_handler_field_field.inc). Backtrace:
views_handler_field_field->query() view.inc:1098
view->_build('field') view.inc:1007
view->build(NULL) view.inc:1116
view->execute(NULL) view.inc:1184
view->render() views_plugin_display_block.inc:54
views_plugin_display_block->execute() view.inc:1337
view->execute_display('block_2') views.module:703
views_block_view('taxonomy_term-block_2') 
call_user_func_array('views_block_view', Array) module.inc:826
module_invoke('views', 'block_view', 'taxonomy_term-block_2') block.module:854
_block_render_blocks(Array) block.module:674
block_list('content') block.module:318
block_get_blocks_by_region('content') block.module:268
block_page_build(Array) common.inc:5621
drupal_render_page('

MESSAGE	Notice: Undefined index: commerce_product in views_handler_field_field->query() (line 186 of /sites/all/modules/views/modules/field/views_handler_field_field.inc). Backtrace:
views_handler_field_field->query() view.inc:1098
view->_build('field') view.inc:1007
view->build(NULL) view.inc:1116
view->execute(NULL) view.inc:1184
view->render() views_plugin_display_block.inc:54
views_plugin_display_block->execute() view.inc:1337
view->execute_display('block_2') views.module:703
views_block_view('taxonomy_term-block_2') 
call_user_func_array('views_block_view', Array) module.inc:826
module_invoke('views', 'block_view', 'taxonomy_term-block_2') block.module:854
_block_render_blocks(Array) block.module:674
block_list('content') block.module:318
block_get_blocks_by_region('content') block.module:268
block_page_build(Array) common.inc:5621
drupal_render_page('

Thanks for helping me!
Greetings, Martijn

rszrama’s picture

I'm not sure this is an issue with Commerce - it may just be some very oddly constructed Views. ; )

I tested with the one linked in comment #6, and the problem was with adding a field to render the product reference field of a line item as an Add to Cart form - but riddle me this... why in the world would you ever do that? Especially when the View in question already has a relationship directly to the product?

And let's just look at the purpose of this View... well, no, I can't even divine it:

  1. The base table of node
  2. Requires a relationship to products referenced through field_product{_reference
  3. With an optional relationship to the line item table (note: this is the relationship causing issues)
  4. And an optional relationship back to the node referencing the product on the line item

And all that just to show a set of products with Add to Cart forms. The relationship to line items was completely unnecessary, and if the goal was to only turn up products that have been added to orders (perhaps owned by the viewer), then you would just start with a base table of line items (or even orders!) and join from there through to the product displays.

In other words: the relationships on this are screwed up.

Now, that said, the code is allowing people to go crazy by defining a relationship from the product table to "Commerce Line item: Referenced product", ostensibly because there's a product reference field that comes by default on all product line item types. However, it should only be showing a reverse relationship if anything (line items referencing the product), not a relationship from the product to the line item through the product reference field.

This relationship should not exist. The reason we're getting errors with missing base tables is the LEFT table isn't known to the relationship in the context it's being used. Whoops.

What I don't know is what exactly this code should be doing differently:

function commerce_product_reference_field_views_data($field) {
  $data = field_views_field_default_views_data($field);

  // Add a join to commerce_product so that it can be used by the inverse relationship.
  $data[_field_sql_storage_tablename($field)]['table']['join']['commerce_product'] = array(
    'left_field' => 'product_id',
    'field' => $field['field_name'] . '_product_id',
    'extra' => array(
      array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
    ),
  );

  // Build an array of bundles the product reference field appears on.
  $bundles = array();

  foreach ($field['bundles'] as $entity => $entity_bundles) {
    $bundles[] = $entity . ' (' . implode(', ', $entity_bundles) . ')';
  }

  foreach ($data as $table_name => $table_data) {
    foreach ($table_data as $field_name => $field_data) {
      if (isset($field_data['filter']['field_name']) && $field_name != 'delta') {
        $data[$table_name][$field_name]['relationship'] = array(
          'title' => t('Referenced product'),
          'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $bundles))),
          'base' => 'commerce_product',
          'base field' => 'product_id',
          'handler' => 'views_handler_relationship',
          'label' => t('Product'),
        );
      }
    }
  }

  return $data;
}

Somehow this code results in the relationship from the line item's product reference field being available when the base table of the View (or a joined in table) is commerce_product. Here's the final data array:

Array(
  [field_data_commerce_product] => Array(
    [table] => Array(
      [join] => Array(
        [commerce_line_item] => Array(
          [left_field] => line_item_id
          [field] => entity_id
          [extra] => Array(
            [0] => Array(
              [field] => entity_type
              [value] => commerce_line_item
            )
            [1] => Array(
              [field] => deleted
              [value] => 0
              [numeric] => 1
            )
          )
        )
        [commerce_product] => Array(
          [left_field] => product_id
          [field] => commerce_product_product_id
          [extra] => Array(
            [0] => Array(
              [field] => deleted
              [value] => 0
              [numeric] => 1
            )
          )
        )
      )
    )
    [entity_id] => Array(
      [field] => Array(
        [moved to] => Array(
          [0] => field_data_commerce_product
          [1] => commerce_product
        )
      )
    )
    [commerce_product] => Array(
      [group] => Commerce Line item
      [title] => Product
      [title short] => Product
      [help] => Appears in: commerce_line_item:product.
      [field] => Array(
        [table] => field_data_commerce_product
        [handler] => views_handler_field_field
        [click sortable] => 1
        [field_name] => commerce_product
        [real field] => commerce_product_product_id
        [additional fields] => Array(
          [0] => delta
          [1] => language
          [2] => bundle
          [3] => commerce_product_product_id
        )
        [entity_tables] => Array(
          [commerce_line_item] => commerce_line_item
        )
        [element type] => div
        [is revision] => 
      )
    )
    [commerce_product_product_id] => Array(
      [group] => Commerce Line item
      [title] => Product (commerce_product)
      [title short] => Product
      [help] => Appears in: commerce_line_item:product.
      [argument] => Array(
        [field] => commerce_product_product_id
        [table] => field_data_commerce_product
        [handler] => views_handler_argument_numeric
        [additional fields] => Array(
          [0] => commerce_product_product_id
        )
        [field_name] => commerce_product
        [empty field name] => - No value -
      )
      [filter] => Array(
        [field] => commerce_product_product_id
        [table] => field_data_commerce_product
        [handler] => views_handler_filter_numeric
        [additional fields] => Array(
          [0] => commerce_product_product_id
        )
        [field_name] => commerce_product
        [allow empty] => 1
      )
      [sort] => Array(
        [field] => commerce_product_product_id
        [table] => field_data_commerce_product
        [handler] => views_handler_sort
        [additional fields] => Array(
          [0] => commerce_product_product_id
        )
        [field_name] => commerce_product
      )
      [relationship] => Array(
        [title] => Referenced product
        [help] => Appears in: commerce_line_item (product).
        [base] => commerce_product
        [base field] => product_id
        [handler] => views_handler_relationship
        [label] => Product
      )
    )
  )
)

If anything sticks out in here, lemme know. fwiw, this loop looks to have come from the taxonomy term reference field code, which uses a similar loop:

  $data = field_views_field_default_views_data($field);
  foreach ($data as $table_name => $table_data) {
    foreach ($table_data as $field_name => $field_data) {
      if (isset($field_data['filter']) && $field_name != 'delta') {
        $data[$table_name][$field_name]['filter']['handler'] = 'views_handler_filter_term_node_tid';
        $data[$table_name][$field_name]['filter']['vocabulary'] = $field['settings']['allowed_values'][0]['vocabulary'];
      }
    }

    // Add the relationship only on the tid field.
    $data[$table_name][$field['field_name'] . '_tid']['relationship'] = array(
      'handler' => 'views_handler_relationship',
      'base' => 'taxonomy_term_data',
      'base field' => 'tid',
      'label' => t('term from !field_name', array('!field_name' => $field['field_name'])),
    );
  }

Funny enough, that bit where it adds "the relationship only on the tid field." gets executed twice. ; )

rszrama’s picture

Also, here's a much simpler View to test with that requires no additional fields:

$view = new view();
$view->name = 'broken_relationship';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'commerce_product';
$view->human_name = 'Broken relationship';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Broken relationship';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Commerce Line item: Referenced product */
$handler->display->display_options['relationships']['commerce_product_product_id']['id'] = 'commerce_product_product_id';
$handler->display->display_options['relationships']['commerce_product_product_id']['table'] = 'field_data_commerce_product';
$handler->display->display_options['relationships']['commerce_product_product_id']['field'] = 'commerce_product_product_id';
/* Field: Commerce Product: Product ID */
$handler->display->display_options['fields']['product_id']['id'] = 'product_id';
$handler->display->display_options['fields']['product_id']['table'] = 'commerce_product';
$handler->display->display_options['fields']['product_id']['field'] = 'product_id';
/* Field: Commerce Line item: Product */
$handler->display->display_options['fields']['commerce_product']['id'] = 'commerce_product';
$handler->display->display_options['fields']['commerce_product']['table'] = 'field_data_commerce_product';
$handler->display->display_options['fields']['commerce_product']['field'] = 'commerce_product';
$handler->display->display_options['fields']['commerce_product']['type'] = 'commerce_product_reference_sku_plain';
$handler->display->display_options['fields']['commerce_product']['settings'] = array(
  'show_quantity' => 0,
  'default_quantity' => '1',
  'combine' => 1,
  'show_single_product_attributes' => 0,
  'line_item_type' => 0,
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'broken-relationship';
madelyncruz’s picture

Issue summary: View changes

#1 fixed the issue I have right now. Thank you!!!

darrellduane’s picture

The patch in issue https://www.drupal.org/node/2331209 will likely fix the error on line 127 listed in #7, #10, #13.

argiepiano’s picture

The approach in #7 helped me figure out the problematic view and field. Thanks @DYdave

zalak.addweb’s picture

Issue tags: +views
riddhi.addweb’s picture

Issue tags: +commerce module