Skip to content

Commit

Permalink
Improve meta field rendering (#324)
Browse files Browse the repository at this point in the history
* improved meta field rendering when Custom Meta is not supported in post type.

* build

* fixed non unique control notice keys in array

* changed error message to warning with helpful link

---------

Co-authored-by: Nikita <[email protected]>
  • Loading branch information
Fellan-91 and nk-o authored Nov 4, 2024
1 parent d4bfafa commit 744bf38
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
37 changes: 33 additions & 4 deletions assets/components/render-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import { cloneDeep } from 'lodash';
import { Component, Fragment, RawHTML } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
import { PanelBody, Notice } from '@wordpress/components';
import { PanelBody, ExternalLink, Notice } from '@wordpress/components';
import { select } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies.
Expand Down Expand Up @@ -316,14 +318,41 @@ export default class RenderControls extends Component {

if (controlResult) {
const val = controlRenderData.getValue();
let controlNotice = '';
const controlNotice = [];

// Display an error for meta controls when the post type doesn't support custom fields and the control is configured to save_in_meta fields.
if (controlData.save_in_meta === 'true') {
const postType = select('core/editor').getCurrentPostType();
const postTypeObject = select('core').getPostType(postType);

const postTypeSupportMeta =
postTypeObject?.supports?.['custom-fields'] || false;
if (!postTypeSupportMeta) {
controlNotice.push(
<Notice
key={`notice-meta-${controlData.name}`}
status="warning"
isDismissible={false}
className="lzb-constructor-notice"
>
{__(
'Custom fields are not enabled for this post type. Enable "custom-fields" support to use this control.',
'lazy-blocks'
)}{' '}
<ExternalLink href="https://developer.wordpress.org/reference/functions/add_post_type_support/">
{__('Learn how', 'lazy-blocks')}
</ExternalLink>
</Notice>
);
}
}

// show error for required fields
const requiredError = checkControlValidity(val, controlData);
if (allowErrorNotice && requiredError) {
controlNotice = (
controlNotice.push(
<Notice
key={`notice-${controlData.name}`}
key={`notice-required-${controlData.name}`}
status="error"
isDismissible={false}
className="lzb-constructor-notice"
Expand Down
2 changes: 1 addition & 1 deletion build/editor.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'b36b0e13cc3ba48939d3');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'c748fc0fe9b6034cc671');
Loading

0 comments on commit 744bf38

Please sign in to comment.