Skip to content

Commit 45310be

Browse files
committed
Display warning next to 'Query Type' control
1 parent 7a8f632 commit 45310be

File tree

2 files changed

+52
-64
lines changed

2 files changed

+52
-64
lines changed

packages/block-library/src/query/edit/inspector-controls/index.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import {
55
TextControl,
66
SelectControl,
77
RangeControl,
8-
__experimentalToggleGroupControl as ToggleGroupControl,
9-
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
108
Notice,
9+
__experimentalVStack as VStack,
1110
__experimentalToolsPanel as ToolsPanel,
1211
__experimentalToolsPanelItem as ToolsPanelItem,
12+
__experimentalToggleGroupControl as ToggleGroupControl,
13+
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
1314
} from '@wordpress/components';
1415
import { useSelect } from '@wordpress/data';
1516
import { store as coreStore } from '@wordpress/core-data';
@@ -113,8 +114,7 @@ export default function QueryInspectorControls( props ) {
113114
}, [ querySearch, onChangeDebounced ] );
114115

115116
const orderByOptions = useOrderByOptions( postType );
116-
const showInheritControl =
117-
! isSingular && isControlAllowed( allowedControls, 'inherit' );
117+
const showInheritControl = isControlAllowed( allowedControls, 'inherit' );
118118
const showPostTypeControl =
119119
! inherit && isControlAllowed( allowedControls, 'postType' );
120120
const postTypeControlLabel = __( 'Post type' );
@@ -185,6 +185,10 @@ export default function QueryInspectorControls( props ) {
185185
const showDisplayPanel =
186186
showPostCountControl || showOffSetControl || showPagesControl;
187187

188+
// The block cannot inherit a default WordPress query in singular content (e.g., post, page, 404, blank).
189+
// Warn users but still permit this type of query for exceptional cases in Classic and Hybrid themes.
190+
const hasInheritanceWarning = isSingular && inherit;
191+
188192
return (
189193
<>
190194
{ showSettingsPanel && (
@@ -208,36 +212,48 @@ export default function QueryInspectorControls( props ) {
208212
onDeselect={ () => setQuery( { inherit: true } ) }
209213
isShownByDefault
210214
>
211-
<ToggleGroupControl
212-
__next40pxDefaultSize
213-
__nextHasNoMarginBottom
214-
label={ __( 'Query type' ) }
215-
isBlock
216-
onChange={ ( value ) => {
217-
setQuery( {
218-
inherit: value === 'default',
219-
} );
220-
} }
221-
help={
222-
inherit
223-
? __(
224-
'Display a list of posts or custom post types based on the current template.'
225-
)
226-
: __(
227-
'Display a list of posts or custom post types based on specific criteria.'
228-
)
229-
}
230-
value={ !! inherit ? 'default' : 'custom' }
231-
>
232-
<ToggleGroupControlOption
233-
value="default"
234-
label={ __( 'Default' ) }
235-
/>
236-
<ToggleGroupControlOption
237-
value="custom"
238-
label={ __( 'Custom' ) }
239-
/>
240-
</ToggleGroupControl>
215+
<VStack spacing={ 4 }>
216+
<ToggleGroupControl
217+
__next40pxDefaultSize
218+
__nextHasNoMarginBottom
219+
label={ __( 'Query type' ) }
220+
isBlock
221+
onChange={ ( value ) => {
222+
setQuery( {
223+
inherit: value === 'default',
224+
} );
225+
} }
226+
help={
227+
inherit
228+
? __(
229+
'Display a list of posts or custom post types based on the current template.'
230+
)
231+
: __(
232+
'Display a list of posts or custom post types based on specific criteria.'
233+
)
234+
}
235+
value={ !! inherit ? 'default' : 'custom' }
236+
>
237+
<ToggleGroupControlOption
238+
value="default"
239+
label={ __( 'Default' ) }
240+
/>
241+
<ToggleGroupControlOption
242+
value="custom"
243+
label={ __( 'Custom' ) }
244+
/>
245+
</ToggleGroupControl>
246+
{ hasInheritanceWarning && (
247+
<Notice
248+
status="warning"
249+
isDismissible={ false }
250+
>
251+
{ __(
252+
'Cannot inherit the current template query when placed inside the singular content (e.g., post, page, 404, blank).'
253+
) }
254+
</Notice>
255+
) }
256+
</VStack>
241257
</ToolsPanelItem>
242258
) }
243259

packages/block-library/src/query/edit/query-content.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import { useEffect, useCallback } from '@wordpress/element';
77
import {
88
BlockControls,
99
InspectorControls,
10-
Warning,
1110
useBlockProps,
1211
store as blockEditorStore,
1312
useInnerBlocksProps,
1413
} from '@wordpress/block-editor';
15-
import { Button, SelectControl } from '@wordpress/components';
14+
import { SelectControl } from '@wordpress/components';
1615
import { __ } from '@wordpress/i18n';
1716
import { store as coreStore } from '@wordpress/core-data';
1817

@@ -132,10 +131,6 @@ export default function QueryContent( {
132131
displayLayout: { ...displayLayout, ...newDisplayLayout },
133132
} );
134133

135-
// The block cannot inherit a default WordPress query in singular content (e.g., post, page, 404, blank).
136-
// Warn users but still permit this type of query for exceptional cases in Classic and Hybrid themes.
137-
const hasInheritanceWarning = isSingular && inherit;
138-
139134
return (
140135
<>
141136
<EnhancedPaginationModal
@@ -180,30 +175,7 @@ export default function QueryContent( {
180175
clientId={ clientId }
181176
/>
182177
</InspectorControls>
183-
{ hasInheritanceWarning ? (
184-
<div { ...blockProps }>
185-
<Warning
186-
actions={ [
187-
<Button
188-
__next40pxDefaultSize
189-
key="switch"
190-
onClick={ () =>
191-
updateQuery( { inherit: false } )
192-
}
193-
variant="primary"
194-
>
195-
{ __( 'Switch to Custom' ) }
196-
</Button>,
197-
] }
198-
>
199-
{ __(
200-
'Cannot inherit the default WordPress query when placed inside the singular content (e.g., post, page, 404, blank).'
201-
) }
202-
</Warning>
203-
</div>
204-
) : (
205-
<TagName { ...innerBlocksProps } />
206-
) }
178+
<TagName { ...innerBlocksProps } />
207179
</>
208180
);
209181
}

0 commit comments

Comments
 (0)