Calling tainacan_the_metadata function outside the loop

Hello:

In a view that I have created, I want to include, in the items’ list, links with which the user can download a citation RIS file, and an APA reference RTF file for each item. For that purpose, I want to write a script that will generate those files in the server over night for the newly published items. For doing that, I want to call the tainacan_the_metadata function outside the loop, referencing the item ID. I haven’t been able to. My best try gives me the “no script kiddies, please” message. Could you tell me how I can do that?

Best regards,

                    Pedro

I’m glad to see that you are achieving nice results customizing your view mode.The citation feature seems a really nice one to have.

tainacan_the_metadata function is intended to be used as a template tag mostly, so it does expects you to be in the context of a loop, because that is how it figures out which item are you expecting to find the metadata from. So if you want to do something more advanced as you say, you’ll probably want to request individually the item metadata. Here is a section of our Wiki that mentions how it would work:

Notice that to construct an Item Metadata instance you need two objects: the Item and the Metadata one, which can be built using examples described earlier in the same link.

Let me know if this helps!

Dear Mateus:

Thank you for your help. I understand that the commands
$itemMetadada = new \Tainacan\Entities\ItemMetadataEntity($item, $metadatum);
$itemMetadata->set_value(‘Rojo’);
are for setting the value of the metadatum of an item. I am afraid I do not see what the command would be to retrieve the value of a metadatum of an item.

Could you please guide me with that issue?

Best regards,

                    Pedro

Sorry, didn’t check the details… its a getter/setter pattern, in this case

$itemMetadata->get_value();

Should return to you the raw Item Metadatum Value. Another option is to call:

$itemMetadata->get_value_as_html();

Which builds links to Taxonomy terms or Relationships. This is how the tainacan_the_metadata function builds its return, BTW:

¡Great!

I will check it out.

Best regards,

                    Pedro

I am sorry to have some many enquiries. I am trying the enclosed php file that I have put at the top level of the Wordpress folder. When I call it, I get the “No script kiddies please!” message. I suppose that I have to include some php files from Tainacan, but I do not know which and how.

Best regards,

                    Pedro

LlamadaDeFuncionDeTainacan.php.zip (905 Bytes)

That is some important context right there! Hehehe. You cannot do that as it would be a security breach. Your code must be either in a theme or plugin. You can use your view mode main .php file and call the functions from there. Also, no need for including the file from the Tainacan folder, you can call them like this:

 $itemMetadada = new \Tainacan\Entities\ItemMetadataEntity($item, $metadatum);

If I’m not wrong, you should at least do this inside the ‘init’ action. Take a read here:

Thank you Mateus.

All this confusion is a consequence of my ignorance on these issues. I am learning slowly. I will produce the files for the already published items “externally” from an export file and load them to the server. I am exploring the way for writing the files for new items when they are saved in the repository. I am looking at this and will tell you how things go.

Best regards,

                    Pedro

No problema @argeifontesfunes. We’re really short on staff at this moment so it is always great to be able to help newcomers. I know our documentation is not perfect, but hopefully with a bit of guidance you will get there.

Regarding:

We have actions that are fired every time an item is updated. But taking a look at your goal, don’t you think what you are looking to implement looks a lot like an Exposer?

If you have an exposer registered, you can have a link via the API that generates the file given search params. Play a bit with the Links available in the “View as…” button in your items list then you may get a sense of what I am talking about.

Thank you, Mateus. It might be an exposer, but I think that for user experience, it is better that each item in my Referencias view has two links, one for downloading the RIS file with bibliographic information and the RTF file with formatted reference in APA style (many researchers want to copy and paste that in their papers).

I was thinking either to make a new plugin or to include in the plugin I have developed something as follows (this is ChatGPT that helps me)

// Function to write post metadata to a file
function write_metadata_to_file($post_id) {
// Get the post object
$post = get_post($post_id);

// Check if the post type is 'tnc_col_32454_item' and post status is 'publish' or 'future'
if ($post->post_type === 'tnc_col_32454_item' && in_array($post->post_status, array('publish', 'future'))) {
    // Get the RIS string
    //This is where I construct the RIS text based on the post metadata

    // Path to the file where metadata will be written
    $file_path = '/path/to/your/file/metadata.txt';

    // Write metadata to the file
    file_put_contents($file_path, $metadata_string);
}

}

// Hook the function to a WordPress action (e.g., ‘save_post’) to write metadata when a post is saved or updated
add_action(‘save_post’, ‘write_metadata_to_file’);

Best regards,

                    Pedro

Hello, Mateus:

I continue working on the citation and reference project. I have produced a script that creates the RIS citation file and the RTF file for each item from the CSV file exported from Tainacan. I have uploaded those files to the server and my plugin shows the download links for each file in the Referencias view (see here).

Now I need to be able to write those two files each time a draft item is published or a published item is edited and saved. For that purpose, I have added the following lines to the main plugin php file:

// Hook the function to ‘save_post’ to write data when a post is saved or updated
add_action(‘save_post’, ‘tr_write_metadata_to_file’);
Blockquote

The tr_write_metadata_to_file function is as follows:

// Function to write post metadata to a file
function tr_write_metadata_to_file($post_id) {
// Get the post object
$post = get_post($post_id);

// Check if the post type is 'tnc_col_32454_item' and post status is 'publish' or 'future'
if ($post->post_type === 'tnc_col_32454_item' && in_array($post->post_status, array('publish', 'future'))) {
    
    //Get metadata value
    $metadatum = 146288;
    $itemMetadada = new \Tainacan\Entities\ItemMetadataEntity($post_id, $metadatum);
    $itemMetadata->get_value();

    //Get string to write
    $metadata_string = $itemMetadata;

    // Path to the file where metadata will be written
    $file_path = ABSPATH . '/wp-content/uploads/tainacan-exports/_PruebaEscritura.txt';

    // Write metadata to the file
    file_put_contents($file_path, $metadata_string);
}

}

It gives internal server error. I am not being able to get the metadatum value for item. Ideally, I would like to get an array with metadatum title associated to its value. I am not being able to get that.

As always, your help is very much appreciated.

Best regards,

                    Pedro

These days have been a bit harder to follow up things here. Just to not let this go:

When you register an exposer, you can query the API by just passing the exposer=my-exposer-slug and this will return to you the desired file. This means that you could build a link in the view mode like:

<a href="https://funesfrpre.uniandes.edu.co/wp-json/tainacan/v2/collection/32454/items/?exposer=ris&id=12345"

Considering of course the Exposer RIS and an item with ID 12345. The advantage of this strategy is that you don’t have to save files anyhere or worry about updating them. The file is generated under demand via the API. For instance, we do not store CSV, HTML or JSON files to our items, we just render them via API when the user requests it.

Got it?

Mateus:

I have registered the exposer example and made it work, without any difficulty. The problem, as I can see it, is that the link with exposer and item data generates a web page with the text produced by the exposer. It is not a link for downloading a file with the data produced with the exposer. Is this possible? This is essential particularly for the RTF reference file which will have the APA formating (italics).

Best regards,

                    Pedro

You can force the link to provide a download link via the browser side. Check this link.

Regarding formatting, if the content is being exported in RTF, define the file name in the download attribute by adding the .rtf in the end. Now, if you ask me how does RTF works, I really don’t know… your Exposer code should be the one to handle the headers and tags necessary to define what an italic is, just like if you were generating an HTML file you would add the <html>.... <strong>somthing` notation… you should take a look into the RTF documentation, that should give you an idea.

Thank you, Mateus, for all your help.

I am trying to understand the way an exposer works. The first step I have to achieve is to get an array with the fields’ names and values as string. The code below does that, but I am having trouble getting and showing the value of a given field. What am I doing wrong?

    public function rest_request_after_callbacks( $response, $handler, $request ) {
        $response->set_headers( ['Content-Type: text/plain; charset=' . get_option( 'blog_charset' )] );
        
        //Get the array of info
        $data = $response->get_data();
        $new_data = $this->array_to_txt($data, false);

        //Array to text
        //$text = print_r($new_data);
        $text = $new_data['Tipo de documento'];
        
        //Set the response to the text
        $response->set_data($txt);
        return $response;
    }
    
    /**
    * Convierte Array a Txt
    * @param array $data
    * @param string $txt
    * @return string
    */
    protected function array_to_txt( $data, $addlevel = true ) {
        $new_array = [];

        foreach ($data['items'] as $item) {
            foreach ($item['metadata'] as $metadata) {
                $new_array[$metadata['name']] = $metadata['value_as_string'];
            }
        }

        return $new_array;


    }

Best regards,

                    Pedro

Sorry for the previous post. I have discovered that I have to get the fields values in the second function and not in the first. Now that I have the array with fields and values, I can continue the work. I will keep you posted.

Best regards,

                    Pedro
1 curtida

Not sure if I already did that, but one recommendation is to take a look at the existing exposers Source Code:

Dear Mateus:

I have finished the beta version of the plugin I have been working on. It shows a view in the form of references in APA format of the items. I have also included two exposers, so that users can download a RIS file for bibliographic software and a RTF file with the formatted reference of the item in APA format. You can see it working here. I am attaching the plugin.

I am not a computer scientist and I had only experience in writing simple applescripts. I want to thank you profusely for all your help. The code is extremely long, convoluted and inefficient. But it is working.

I have not been able to get the post’s collection name inside the loop and to get the exposers to make the browser to download a file when using them in a page of items as the CSV exposer does. I tried to follow the ideas in the CSV exposer without success.

Best regards,

                    Pedro

tainacan-lista-de-referencias.zip (20.0 KB)

1 curtida

For that you could call: tainacan_the_collection_name()

Wait, but in your link: Funes • Documentos – Funes

The download of the files seems to be working… what is the issue?

Mateus:

returns nothing inside the loop. For the link of a given item, I am getting the collection ID from the post type. I need to get the collection name inside the loop, because the database name is a bibliographic field for the RIS metadata.

When I was talking about the exposer, I meant the situation when in list of items page, a user clicks on “See as” and wnat to get all the items of a page (see photo). I would love the user to be able to download a file with that list as it happens with CSV exposer.

Best regards,

                    Pedro