Creates backend save function to item metadata block. #566.

This commit is contained in:
mateuswetah 2022-10-25 09:38:53 -03:00
parent 375a17697f
commit 6dee6f980c
6 changed files with 93 additions and 11 deletions

View File

@ -1553,7 +1553,7 @@ class Theme_Helper {
return '';
$args['p'] = $args['metadata'];
$args['posts_per_page'] = 1;
//$args['posts_per_page'] = 1;
$collection = \Tainacan\Repositories\Collections::get_instance()->fetch($collection_id);
$metadata = \Tainacan\Repositories\Metadata::get_instance()->fetch_by_collection($collection, $args);

View File

@ -26,6 +26,10 @@
"type": "boolean",
"default": false
},
"isDynamic": {
"type": "boolean",
"default": false
},
"collectionId": {
"type": "integer"
},
@ -41,7 +45,7 @@
"default": false
},
"itemMetadataRequestSource": {
"type": "string",
"type": "object",
"default": ""
},
"itemMetadata": {

View File

@ -1,8 +1,9 @@
const { __ } = wp.i18n;
const { Button, Spinner, Placeholder } = wp.components;
const { Button, Spinner, ToggleControl, Placeholder, PanelBody } = wp.components;
const { useBlockProps, InnerBlocks } = (tainacan_blocks.wp_version < '5.2' ? wp.editor : wp.blockEditor );
const ServerSideRender = wp.serverSideRender;
const { useBlockProps, InnerBlocks, InspectorControls } = (tainacan_blocks.wp_version < '5.2' ? wp.editor : wp.blockEditor );
import SingleItemModal from '../../js/selection/single-item-modal.js';
import getCollectionIdFromPossibleTemplateEdition from '../../js/template/tainacan-blocks-single-item-template-mode.js';
@ -23,12 +24,14 @@ export default function ({ attributes, setAttributes, className, isSelected }) {
metadata,
itemMetadataTemplate,
dataSource,
templateMode
templateMode,
isDynamic
} = attributes;
// Gets blocks props from hook
const blockProps = tainacan_blocks.wp_version < '5.6' ? { className: className } : useBlockProps();
const currentWPVersion = (typeof tainacan_blocks != 'undefined') ? tainacan_blocks.wp_version : tainacan_plugin.wp_version;
function setContent() {
if ( dataSource === 'parent' && templateMode) {
@ -197,6 +200,24 @@ export default function ({ attributes, setAttributes, className, isSelected }) {
: (
<div { ...blockProps }>
<InspectorControls>
<PanelBody
title={ __('Data source', 'tainacan') }
initialOpen={ true }
>
<ToggleControl
label={ __('Dynamic sync from Tainacan', 'tainacan') }
help={ __( 'Check this if you want the item metadata values to be always sync with its source from Tainacan. If disabled, however, you will be able to change order of inner blocks, delete and wrap them inside other blocks.', 'tainacan' ) }
checked={ isDynamic }
onChange={ ( isChecked ) => {
isDynamic = isChecked;
setAttributes({ isDynamic: isDynamic });
}
}
/>
</PanelBody>
</InspectorControls>
{ isSelected ?
(
<div>
@ -269,9 +290,18 @@ export default function ({ attributes, setAttributes, className, isSelected }) {
</div> :
<div className={ 'item-metadata-edit-container' }>
{ itemMetadataTemplate.length ?
<InnerBlocks
( isDynamic ?
<ServerSideRender
block="tainacan/item-metadata"
attributes={ attributes }
httpMethod={ currentWPVersion >= '5.5' ? 'POST' : 'GET' }
/>
:
<InnerBlocks
allowedBlocks={ true }
template={ itemMetadataTemplate } />
template={ itemMetadataTemplate }
templateInsertUpdatesSelection={ true } />
)
: null
}
</div>

View File

@ -1,7 +1,7 @@
const { useBlockProps, InnerBlocks } = (tainacan_blocks.wp_version < '5.2' ? wp.editor : wp.blockEditor );
export default function({ className }) {
export default function({ className, attributes }) {
const blockProps = tainacan_blocks.wp_version < '5.6' ? { className: className } : useBlockProps.save();
return <div { ...blockProps }><InnerBlocks.Content /></div>
return attributes.isDynamic ? null : <div { ...blockProps }><InnerBlocks.Content /></div>
};

View File

@ -0,0 +1,48 @@
<?php
/**
* Renders the content of the item metadata
* using Tainacan template functions
*/
function tainacan_blocks_render_item_metadata( $block_attributes, $content, $block ) {
$is_dynamic = isset($block_attributes['isDynamic']) ? ($block_attributes['isDynamic'] === 'true' || $block_attributes['isDynamic'] == 1) : false;
if ( $is_dynamic) {
// Basic check, otherwise we don't have nothing to render here.
$item_id = !empty($block->context['tainacan/itemId']) ? $block->context['tainacan/itemId'] : (isset($block_attributes['itemId']) ? $block_attributes['itemId'] : false);
$collection_id = isset($block_attributes['collectionId']) ? $block_attributes['collectionId'] : false;
$metadata = isset($block_attributes['metadata']) ? $block_attributes['metadata'] : [];
$template_mode = isset($block_attributes['templateMode']) ? $block_attributes['templateMode'] : false;
// Builds args from backend query
$args = [
'metadata' => array_map(function($metadatum) { return $metadatum['id']; }, $metadata)
];
// Checks if we are in the edit page or in the published
$current_post = get_post();
if ( $template_mode && $collection_id ) {
$collection_pt_pattern = '/' . \Tainacan\Entities\Collection::$db_identifier_prefix . '\d+' . \Tainacan\Entities\Collection::$db_identifier_sufix . '/';
if ( $current_post === NULL )
return \Tainacan\Theme_Helper::get_instance()->get_tainacan_item_metadata_template($args, $collection_id );
else if ( $current_post->post_type !== false && preg_match($collection_pt_pattern, $current_post->post_type) )
return tainacan_get_the_metadata( $args, $current_post->ID );
} else if ( $item_id ) {
return tainacan_get_the_metadata( $args, $item_id );
}
} else {
$inner_blocks = $block->inner_blocks;
$inner_blocks_html = '';
foreach ( $inner_blocks as $inner_block ) {
$inner_blocks_html .= $inner_block->render();
}
return $inner_blocks_html;
}
}

View File

@ -19,7 +19,7 @@ const TAINACAN_BLOCKS = [
'item-gallery' => ['render_callback' => 'tainacan_blocks_render_items_gallery'],
'item-metadata-sections' => ['render_callback' => 'tainacan_blocks_render_metadata_sections'],
'item-metadata-section' => ['render_callback' => 'tainacan_blocks_render_metadata_section'],
'item-metadata' => [],
'item-metadata' => ['render_callback' => 'tainacan_blocks_render_item_metadata'],
'item-metadatum' => ['render_callback' => 'tainacan_blocks_render_item_metadatum'],
'metadata-section-name' => [],
'metadata-section-description' => []