Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Products Block: fix grid view with Gutenberg 16 (#9916)
Browse files Browse the repository at this point in the history
* Revert "fix products block layout on gutenberg 16 (#9886)"

This reverts commit f1e5dd7.

* add post_template_has_support_for_grid_view setting
  • Loading branch information
gigitux committed Jun 20, 2023
1 parent b62828c commit 1b7a4b9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
14 changes: 12 additions & 2 deletions assets/js/blocks/product-query/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* External dependencies
*/
import { getSetting } from '@woocommerce/settings';
import { getSetting, getSettingWithCoercion } from '@woocommerce/settings';
import { objectOmit } from '@woocommerce/utils';
import type { InnerBlockTemplate } from '@wordpress/blocks';

import { isBoolean } from '@woocommerce/types';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -69,6 +69,13 @@ export const QUERY_DEFAULT_ATTRIBUTES: QueryBlockAttributes = {
},
};

// This is necessary to fix https://github.com/woocommerce/woocommerce-blocks/issues/9884.
const postTemplateHasSupportForGridView = getSettingWithCoercion(
'post_template_has_support_for_grid_view',
false,
isBoolean
);

export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
[
'core/post-template',
Expand All @@ -78,6 +85,9 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
* This class is used to add default styles for inner blocks.
*/
className: 'products-block-post-template',
...( postTemplateHasSupportForGridView && {
layout: { type: 'grid', columnCount: 3 },
} ),
},
[
[
Expand Down
15 changes: 14 additions & 1 deletion assets/js/blocks/product-query/variations/related-products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Icon } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { stacks } from '@woocommerce/icons';
import { registerBlockSingleProductTemplate } from '@woocommerce/atomic-utils';
import { getSettingWithCoercion } from '@woocommerce/settings';
import { isBoolean } from '@woocommerce/types';

/**
* Internal dependencies
Expand Down Expand Up @@ -43,6 +45,12 @@ export const BLOCK_ATTRIBUTES = {
},
};

const postTemplateHasSupportForGridView = getSettingWithCoercion(
'post_template_has_support_for_grid_view',
false,
isBoolean
);

export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
[
'core/heading',
Expand All @@ -53,7 +61,12 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
],
[
'core/post-template',
{ __woocommerceNamespace: PRODUCT_TEMPLATE_ID },
{
__woocommerceNamespace: PRODUCT_TEMPLATE_ID,
...( postTemplateHasSupportForGridView && {
layout: { type: 'grid', columnCount: 3 },
} ),
},
[
[
'woocommerce/product-image',
Expand Down
32 changes: 32 additions & 0 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ protected function initialize() {
add_filter( 'rest_product_collection_params', array( $this, 'extend_rest_query_allowed_params' ), 10, 1 );
}

/**
* Extra data passed through from server to client for block.
*
* @param array $attributes Any attributes that currently are available from the block.
* Note, this will be empty in the editor context when the block is
* not in the post content on editor load.
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );

$gutenberg_version = '';

if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) {
if ( defined( 'GUTENBERG_VERSION' ) ) {
$gutenberg_version = GUTENBERG_VERSION;
}

if ( ! $gutenberg_version ) {
$gutenberg_data = get_file_data(
WP_PLUGIN_DIR . '/gutenberg/gutenberg.php',
array( 'Version' => 'Version' )
);
$gutenberg_version = $gutenberg_data['Version'];
}
}

$this->asset_data_registry->add(
'post_template_has_support_for_grid_view',
version_compare( $gutenberg_version, '16.0', '>=' )
);
}

/**
* Check if a given block
*
Expand Down

0 comments on commit 1b7a4b9

Please sign in to comment.