From c2147722d86339575ee2f06adff517f714e382ce Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Wed, 6 May 2020 18:58:10 -0300 Subject: [PATCH] Create a metadata with empty value to parent compound metadata --- .../repositories/class-tainacan-item-metadata.php | 12 +++++++++++- .../compound/class-tainacan-compound.php | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 58957a4ea..3b35a5938 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -56,7 +56,17 @@ class Item_Metadata extends Repository { if ( $metadata_type->get_primitive_type() == 'term' ) { $this->save_terms_metadatum_value( $item_metadata ); } elseif ( $metadata_type->get_primitive_type() == 'compound' ) { - // do nothing. Compound values are updated when its child metadata are updated + // Create a post_metadata with value [] to compound metadata or return if it already exists + // to use as parent_meta_id for child metadata + global $wpdb; + $compounds = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s and meta_value = ''", $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id() ), ARRAY_A ); + if( is_array($compounds) && !empty($compounds) ) { + $meta_id = $compounds[0]['meta_id']; + $item_metadata->set_parent_meta_id($meta_id); + } else { + $meta_id = add_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), '' ); + $item_metadata->set_parent_meta_id($meta_id); + } return $item_metadata; } else { if ( $unique ) { diff --git a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php index 0c3cbd513..2a0209d26 100644 --- a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php +++ b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php @@ -169,6 +169,8 @@ class Compound extends Metadata_Type { public function get_value_as_html(Item_Metadata_Entity $item_metadata) { $value = $item_metadata->get_value(); $return = ''; + if ( empty($value) ) + return $return; $separator = $item_metadata->get_multivalue_separator(); if ( $item_metadata->is_multiple() ) {