Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering min and max number of columns in blocks #12861

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/editor": "file:../editor",
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
"@wordpress/keycodes": "file:../keycodes",
Expand Down
28 changes: 25 additions & 3 deletions packages/block-library/src/columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import memoize from 'memize';
import { __ } from '@wordpress/i18n';
import { PanelBody, RangeControl, G, SVG, Path } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
import { createBlock } from '@wordpress/blocks';
import {
InspectorControls,
Expand Down Expand Up @@ -65,6 +66,27 @@ function getDeprecatedLayoutColumn( originalContent ) {
}
}

/**
* Filters the default number of columns of the core/columns block.
*
* @param {Number} number The column count. Defaults to 2.
*/
const DEFAULT_COLUMNS = applyFilters( 'blocks.columns.defaultColumns', 2 );

/**
* Filters the minimum number of allowed columns of the core/columns block.
*
* @param {Number} number The column count. Defaults to 2.
*/
const MIN_COLUMNS = applyFilters( 'blocks.columns.minColumns', 2 );

/**
* Filters the maximum number of allowed columns of the core/columns block.
*
* @param {Number} number The column count. Defaults to 6.
*/
const MAX_COLUMNS = applyFilters( 'blocks.columns.maxColumns', 6 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to mention that in #core-js chats we agreed to limit the use of hooks and filters. We noticed that for the JavaScript code, hooks and filters are not the best extensibility APIs.

It might be ok for these particular numbers but let's be very careful before adding new ones.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's very good to know, thanks! Do you happen to have a link to that discussion or a summary of it by chance? I'd be curious to learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


export const name = 'core/columns';

export const settings = {
Expand All @@ -77,7 +99,7 @@ export const settings = {
attributes: {
columns: {
type: 'number',
default: 2,
default: parseInt( DEFAULT_COLUMNS, 10 ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one might be confusing as you are also able to update this value using the existing blocks.registerBlockType filter.

},
},

Expand Down Expand Up @@ -170,8 +192,8 @@ export const settings = {
columns: nextColumns,
} );
} }
min={ 2 }
max={ 6 }
min={ Math.max( parseInt( MIN_COLUMNS, 10 ), 1 ) }
max={ parseInt( MAX_COLUMNS, 10 ) }
/>
</PanelBody>
</InspectorControls>
Expand Down
20 changes: 17 additions & 3 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { filter, pick, map, get } from 'lodash';
*/
import { Component, Fragment } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { applyFilters } from '@wordpress/hooks';
import {
IconButton,
DropZone,
Expand All @@ -32,7 +33,20 @@ import {
*/
import GalleryImage from './gallery-image';

const MAX_COLUMNS = 8;
/**
* Filters the minimum number of allowed columns of the core/gallery block.
*
* @param {Number} number The column count. Defaults to 2.
*/
const MIN_COLUMNS = applyFilters( 'blocks.gallery.minColumns', 2 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I want to change the min columns of a gallery, that has certain attributes or that is nested in a specific block? I feel we should pass more information at least the block client id.


/**
* Filters the maximum number of allowed columns of the core/gallery block.
*
* @param {Number} number The column count. Defaults to 6.
*/
const MAX_COLUMNS = applyFilters( 'blocks.gallery.maxColumns', 6 );

const linkOptions = [
{ value: 'attachment', label: __( 'Attachment Page' ) },
{ value: 'media', label: __( 'Media File' ) },
Expand Down Expand Up @@ -243,8 +257,8 @@ class GalleryEdit extends Component {
label={ __( 'Columns' ) }
value={ columns }
onChange={ this.setColumnsNumber }
min={ 1 }
max={ Math.min( MAX_COLUMNS, images.length ) }
min={ Math.max( parseInt( MIN_COLUMNS, 10 ), 1 ) }
max={ Math.min( parseInt( MAX_COLUMNS, 10 ), images.length ) }
/> }
<ToggleControl
label={ __( 'Crop Images' ) }
Expand Down