Adds class side of new feature: term link filtered by collections #423

This commit is contained in:
mateuswetah 2020-09-01 11:49:24 -03:00
parent 4a48d0f0c8
commit 5d53c3b1ae
2 changed files with 91 additions and 40 deletions

View File

@ -91,6 +91,19 @@
:title="$i18n.getHelperTitle('tainacan-taxonomy', 'allow_new_terms')"
:message="$i18n.getHelperMessage('tainacan-taxonomy', 'allow_new_terms')"/>
</b-field>
<!-- <b-field
:addons="false"
:label="$i18n.getHelperTitle('tainacan-taxonomy', 'link_filtered_by_collection')">
<b-switch
size="is-small"
v-model="link_filtered_by_collection"
@input="emitValues()"
true-value="yes"
false-value="no" />
<help-button
:title="$i18n.getHelperTitle('tainacan-taxonomy', 'link_filtered_by_collection')"
:message="$i18n.getHelperMessage('tainacan-taxonomy', 'link_filtered_by_collection')"/>
</b-field> -->
</section>
</template>
@ -111,6 +124,7 @@
taxonomy_id: '',
loading: true,
allow_new_terms: 'yes',
link_filtered_by_collection: 'no',
visible_options_list: false,
input_type: 'tainacan-taxonomy-radio',
multiple_types: {},

View File

@ -13,20 +13,21 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
class Taxonomy extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_primitive_type('term');
$this->set_repository( \Tainacan\Repositories\Terms::get_instance() );
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_primitive_type('term');
$this->set_repository( \Tainacan\Repositories\Terms::get_instance() );
$this->set_default_options([
'allow_new_terms' => 'no',
'link_filtered_by_collection' => []
]);
$this->set_default_options([
'allow_new_terms' => 'no'
]);
$this->set_form_component('tainacan-form-taxonomy');
$this->set_form_component('tainacan-form-taxonomy');
$this->set_component('tainacan-taxonomy');
$this->set_name( __('Taxonomy', 'tainacan') );
$this->set_description( __('A metadatum to use a taxonomy in this collection', 'tainacan') );
$this->set_description( __('A metadatum to use a taxonomy in this collection', 'tainacan') );
$this->set_preview_template('
<div>
<div>
@ -71,36 +72,42 @@ class Taxonomy extends Metadata_Type {
<a class="add-new-term">'. __('View all') . '</a>
</div>
');
add_filter( 'tainacan-term-to-html', [$this, 'term_to_html'], 10, 2 );
}
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'taxonomy_id' => [
'title' => __( 'Related Collection', 'tainacan' ),
'description' => __( 'Select the collection to fetch items', 'tainacan' ),
],
'input_type' => [
'title' => __( 'Input type', 'tainacan' ),
'description' => __( 'The html type of the terms list ', 'tainacan' ),
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'taxonomy_id' => [
'title' => __( 'Related Collection', 'tainacan' ),
'description' => __( 'Select the collection to fetch items', 'tainacan' ),
],
'input_type' => [
'title' => __( 'Input type', 'tainacan' ),
'description' => __( 'The html type of the terms list ', 'tainacan' ),
],
'visible_options_list' => [
'title' => __( 'Always visible options list', 'tainacan' ),
'description' => __( 'Check this option if you are displaying a checkbox or radio input type and wish the options list to always be visible.', 'tainacan' ),
],
'allow_new_terms' => [
'title' => __( 'Allow new terms', 'tainacan' ),
'description' => __( 'Allows to create new terms directly on the item form.', 'tainacan' ),
]
];
}
'title' => __( 'Always visible options list', 'tainacan' ),
'description' => __( 'Check this option if you are displaying a checkbox or radio input type and wish the options list to always be visible.', 'tainacan' ),
],
'allow_new_terms' => [
'title' => __( 'Allow new terms', 'tainacan' ),
'description' => __( 'Allows to create new terms directly on the item form.', 'tainacan' ),
],
'link_filtered_by_collection' => [
'title' => __( 'Link filtered by collection', 'tainacan' ),
'description' => __( 'Links to items list filtered by current collection instead of repository level term items page.', 'tainacan' ),
]
];
}
public function validate_options( Metadatum $metadatum) {
if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
return true;
if (empty($this->get_option('taxonomy_id')))
return ['taxonomy_id' => __('Please select a taxonomy', 'tainacan')];
@ -169,7 +176,6 @@ class Taxonomy extends Metadata_Type {
}
}
}
return true;
@ -182,16 +188,16 @@ class Taxonomy extends Metadata_Type {
*
* @return bool Valid or not
*/
public function validate( Item_Metadata_Entity $item_metadata) {
public function validate( Item_Metadata_Entity $item_metadata) {
$item = $item_metadata->get_item();
$item = $item_metadata->get_item();
if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
$valid = true;
if ('no' === $this->get_option('allow_new_terms') || false === $this->get_option('allow_new_terms')) { //support legacy bug when it was saved as false
if ('no' === $this->get_option('allow_new_terms') || false === $this->get_option('allow_new_terms')) { //support legacy bug when it was saved as false
$terms = $item_metadata->get_value();
if (false === $terms)
@ -217,7 +223,38 @@ class Taxonomy extends Metadata_Type {
return $valid;
}
}
public function term_to_html($return, $term) {
$collections = $this->get_option( 'link_filtered_by_collection' );
if ( !empty( $collections ) ) {
$return = '';
$id = $term->get_id();
if ( $id ) {
$link = get_term_link( (int) $id );
if (is_string($link)) {
$meta_query = [
'metaquery' => [
[
'key' => 'collection_id',
'compare' => 'IN',
'value' => $collections
]
]
];
$link = $link . '?' . http_build_query( $meta_query );
$return = "<a data-linkto='term' data-id='$id' href='$link'>";
$return.= $term->get_name();
$return .= "</a>";
}
}
return $return;
}
return $return;
}
/**
* Return the value of an Item_Metadata_Entity using a metadatum of this metadatum type as an html string