I want to have overview of all product variants from all products in separate view. But when I created it, view show error: TypeError: Argument 1 passed to Drupal\Core\Entity\EntityRepository::getTranslationFromContext() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /web/modules/contrib/commerce/modules/product/src/ProductVariationListBuilder.php on line 70 Drupal\Core\Entity\EntityRepository->getTranslationFromContext()

I tracked it down to this:

public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityRepositoryInterface $entity_repository, RouteMatchInterface $route_match, FormBuilderInterface $form_builder) {
    parent::__construct($entity_type, $storage);

    $this->formBuilder = $form_builder;
    $this->product = $route_match->getParameter('commerce_product');
    $this->product = $entity_repository->getTranslationFromContext($this->product);
  }

And the issue is because variable $route_match->getParameter('commerce_product') is not available here (cannot be because we haven't it in URL address).

uuid: 10f50108-27e8-4e2d-8e1a-be3989188377
langcode: cs
status: true
dependencies:
  config:
    - field.storage.commerce_product_variation.field_product_stock
    - system.menu.admin
  module:
    - commerce_price
    - commerce_product
    - options
    - user
id: admin_variations
label: 'Admin: Variations'
module: views
description: ''
tag: ''
base_table: commerce_product_variation_field_data
base_field: variation_id
core: 8.x
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    position: 0
    display_options:
      access:
        type: perm
        options:
          perm: 'access commerce administration pages'
      cache:
        type: tag
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: full
        options:
          items_per_page: 25
          offset: 0
          id: 0
          total_pages: null
          tags:
            previous: ‹‹
            next: ››
            first: '« First'
            last: 'Last »'
          expose:
            items_per_page: false
            items_per_page_label: 'Items per page'
            items_per_page_options: '5, 10, 25, 50'
            items_per_page_options_all: false
            items_per_page_options_all_label: '- All -'
            offset: false
            offset_label: Offset
          quantity: 9
      style:
        type: table
        options:
          grouping: {  }
          row_class: ''
          default_row_class: true
          override: true
          sticky: false
          caption: ''
          summary: ''
          description: ''
          columns:
            status: status
            sku: sku
            title: title
            product_id: product_id
            list_price__number: list_price__number
            price__number: price__number
            field_product_stock: field_product_stock
            operations: operations
          info:
            status:
              sortable: true
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
            sku:
              sortable: true
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
            title:
              sortable: true
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
            product_id:
              sortable: true
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
            list_price__number:
              sortable: true
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
            price__number:
              sortable: true
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
            field_product_stock:
              sortable: true
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
            operations:
              align: ''
              separator: ''
              empty_column: false
              responsive: ''
          default: '-1'
          empty_table: false
      row:
        type: fields
      fields:
        status:
          id: status
          table: commerce_product_variation_field_data
          field: status
          relationship: none
          group_type: group
          admin_label: ''
          label: Zveřejněno
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: boolean
          settings:
            format: unicode-yes-no
            format_custom_true: ''
            format_custom_false: ''
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: commerce_product_variation
          entity_field: status
          plugin_id: field
        sku:
          id: sku
          table: commerce_product_variation_field_data
          field: sku
          relationship: none
          group_type: group
          admin_label: ''
          label: 'Kód zboží'
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: false
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: commerce_product_variation
          entity_field: sku
          plugin_id: field
        title:
          id: title
          table: commerce_product_variation_field_data
          field: title
          relationship: none
          group_type: group
          admin_label: ''
          label: 'Název položky'
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: false
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: null
          entity_field: title
          plugin_id: field
        product_id:
          id: product_id
          table: commerce_product_variation_field_data
          field: product_id
          relationship: none
          group_type: group
          admin_label: ''
          label: Produkt
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: target_id
          type: entity_reference_label
          settings:
            link: true
          group_column: target_id
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: commerce_product_variation
          entity_field: product_id
          plugin_id: field
        list_price__number:
          id: list_price__number
          table: commerce_product_variation_field_data
          field: list_price__number
          relationship: none
          group_type: group
          admin_label: ''
          label: 'Katalogová cena'
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: number
          type: commerce_price_default
          settings:
            strip_trailing_zeroes: false
            currency_display: symbol
          group_column: ''
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: commerce_product_variation
          entity_field: list_price
          plugin_id: field
        price__number:
          id: price__number
          table: commerce_product_variation_field_data
          field: price__number
          relationship: none
          group_type: group
          admin_label: ''
          label: Cena
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: number
          type: commerce_price_default
          settings:
            strip_trailing_zeroes: false
            currency_display: symbol
          group_column: ''
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: commerce_product_variation
          entity_field: price
          plugin_id: field
        field_product_stock:
          id: field_product_stock
          table: commerce_product_variation__field_product_stock
          field: field_product_stock
          relationship: none
          group_type: group
          admin_label: ''
          label: Dostupnost
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: list_default
          settings: {  }
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
        operations:
          id: operations
          table: commerce_product_variation
          field: operations
          relationship: none
          group_type: group
          admin_label: ''
          label: Akce
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          destination: true
          entity_type: commerce_product_variation
          plugin_id: entity_operations
      filters:
        status:
          id: status
          table: commerce_product_variation_field_data
          field: status
          relationship: none
          group_type: group
          admin_label: ''
          operator: '='
          value: All
          group: 1
          exposed: true
          expose:
            operator_id: ''
            label: Zveřejněno
            description: ''
            use_operator: false
            operator: status_op
            identifier: status
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
              anonymous: '0'
              editor: '0'
              administrator: '0'
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          plugin_id: boolean
          entity_type: commerce_product_variation
          entity_field: status
        sku:
          id: sku
          table: commerce_product_variation_field_data
          field: sku
          relationship: none
          group_type: group
          admin_label: ''
          operator: contains
          value: ''
          group: 1
          exposed: true
          expose:
            operator_id: sku_op
            label: 'Kód zboží'
            description: ''
            use_operator: false
            operator: sku_op
            identifier: sku
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
              anonymous: '0'
              editor: '0'
              administrator: '0'
            placeholder: ''
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          entity_type: commerce_product_variation
          entity_field: sku
          plugin_id: string
        langcode:
          id: langcode
          table: commerce_product_variation_field_data
          field: langcode
          relationship: none
          group_type: group
          admin_label: ''
          operator: in
          value:
            '***LANGUAGE_language_interface***': '***LANGUAGE_language_interface***'
          group: 1
          exposed: false
          expose:
            operator_id: ''
            label: ''
            description: ''
            use_operator: false
            operator: ''
            identifier: ''
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
            reduce: false
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          entity_type: commerce_product_variation
          entity_field: langcode
          plugin_id: language
      sorts:
        changed:
          id: changed
          table: commerce_product_variation_field_data
          field: changed
          relationship: none
          group_type: group
          admin_label: ''
          order: DESC
          exposed: false
          expose:
            label: ''
          granularity: second
          entity_type: commerce_product_variation
          entity_field: changed
          plugin_id: date
      title: 'Skladové položky'
      header: {  }
      footer: {  }
      empty: {  }
      relationships: {  }
      arguments: {  }
      display_extenders: {  }
      use_ajax: true
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - url.query_args
        - user.permissions
      tags:
        - 'config:field.storage.commerce_product_variation.field_product_stock'
  page_1:
    display_plugin: page
    id: page_1
    display_title: Page
    position: 1
    display_options:
      display_extenders: {  }
      path: admin/eshop/variations
      menu:
        type: normal
        title: 'Skladové položky'
        description: ''
        expanded: false
        parent: custom_admin.admin_eshop
        weight: 0
        context: '0'
        menu_name: admin
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - url.query_args
        - user.permissions
      tags:
        - 'config:field.storage.commerce_product_variation.field_product_stock'

Comments

parisek created an issue. See original summary.

parisek’s picture

Issue summary: View changes
parisek’s picture

Workaround is check if $this->product exists and only than call $this->product = $entity_repository->getTranslationFromContext($this->product);

andyg5000’s picture

Status: Active » Needs review
StatusFileSize
new799 bytes

This looks to be tripped whenever the method is called without a product id. An example of this is in layout builder when you drop the block on a a layout, but there's no product context. The patch in #2952529: Support for Layout Builder module handles this, but that's not ready to go.

Perhaps we can use this patch to move things along since it's not a big change.

parisek’s picture

Status: Needs review » Needs work

4andyg5000 Thanks but this will not solve my issue

andyg5000’s picture

Does your fix in #3 solve it?

parisek’s picture

Yes, but I dont think its right solution its just hint for someone with better knowlegde of DC2

lisastreeter’s picture

StatusFileSize
new707 bytes

Created a little patch that makes it possible to add operations links to a View of product variations. Confirmed that it does not negatively affect the default list builder. I'm not using on a multilingual site, but I can't imagine the patch would cause problems there either.

lisastreeter’s picture

Status: Needs work » Needs review
bojanz’s picture

Title: Creating view of product variants not working » "Product variation: Operations links" causes a crash in ProductVariationListBuilder

Retitling for clarity.

Lisa's fix is correct. We should add a small comment before commit:

    if (!empty($this->product)) {
      // The product might not be available when the list builder is
      // instantiated by Views to build the list of operations.
      $this->product = $entity_repository->getTranslationFromContext($this->product);
    }

  • bojanz committed 4f1633f on 8.x-2.x authored by lisastreeter
    Issue #3029969 by lisastreeter, andyg5000, bojanz: "Product variation:...
bojanz’s picture

Status: Needs review » Fixed

Added the comment and committed. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

chi’s picture

if (!empty($this->product)) {
    // The product might not be available when the list builder is
    // instantiated by Views to build the list of operations.
    $this->product = $entity_repository->getTranslationFromContext($this->product);
}

This doesn't solve the problem when a view has contextual filter. Because in that case the value of $this->product would be just a product ID not a fully loaded Product entity. I think it should check if the product is instance of ProductInterface.

Overall this issue exposes two structural problems.
1. Entity operations are coupled with entity list builders. For this reason Views module has to instantiate the variation list builder in a preprocess hook.
2. The the variation list builder is doing to much in constructor. It should not probably store product instance in class property but load it lazily on demand.

Here is a workaround I've come to.

$parameters['commerce_product']['type'] = 'entity:commerce_product';
$collection->get('view.product_variations.page')
  ->setOption('parameters', $parameters);

The route subscriber should be executed after the \Drupal\views\EventSubscriber\RouteSubscriber.