Adding data to what's submitted by posting.php

Discussion forum for Extension Writers regarding Extension Development.
tig_
Registered User
Posts: 31
Joined: Fri Aug 23, 2024 2:42 pm

Adding data to what's submitted by posting.php

Post by tig_ »

My extension is a photo uploader. In posting_layout_include_panel_body.html I am successfully uploading the images and it's pretty slick:

Image

However, users have found that when they press Preview or Save Draft all the already uploaded photos and metadata is lost.

I'm trying to find the best way to ensure it gets persisted when Preview or Save Draft is pressed and reloaded.

I figured I could add a hidden input field like this:

Code: Select all

<!-- hidden input field to the form to track uploaded files, for when Preview is pressed -->
<input type="hidden" name="uploaded_files" id="uploaded-files-field" value="">
But "submit" on Preview or Save Draft is not passing that on so when the page gets reloaded it's empty.

So that seems like a dead end.

I'm wondering if there's some way of explicitly adding to the post data?

Suggestions? Thanks.
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 689
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: Adding data to what's submitted by posting.php

Post by danieltj »

I’m on a phone right now so I can’t really look into this much but I know that the posting UI states that saving as a draft only saves the post content and nothing else so that’s at least one issue. Previewing is harder though, I’m not actually sure if the post is saved while previewing or it just posts the post content to itself and displays what was in the editor without saving to the database. That would need a more thorough investigation.
MY EXTENSIONS:
Verified Profiles | API | Awesome Payments

Available for paid extension work.
tig_
Registered User
Posts: 31
Joined: Fri Aug 23, 2024 2:42 pm

Re: Adding data to what's submitted by posting.php

Post by tig_ »

danieltj wrote: Sat Jan 18, 2025 12:37 am I’m on a phone right now so I can’t really look into this much but I know that the posting UI states that saving as a draft only saves the post content and nothing else so that’s at least one issue. Previewing is harder though, I’m not actually sure if the post is saved while previewing or it just posts the post content to itself and displays what was in the editor without saving to the database. That would need a more thorough investigation.
Both Options and Polls get saved with Preview along with the subject, message text, ect... They do not get written to the DB when this happens.

I don't understand how variables work. Down around line 1639 of posting.php, there's a block of code

Code: Select all

// Preview
if (!count($error) && $preview)
{
...
	if (!count($error))
	{
		$template->assign_vars(array(
			'PREVIEW_SUBJECT'		=> $preview_subject,
			'PREVIEW_MESSAGE'		=> $preview_message,
			'PREVIEW_SIGNATURE'		=> $preview_signature,

			'S_DISPLAY_PREVIEW'		=> !empty($preview_message),
		));
	}
It seems that I could subscribe to some event like core.posting_modify_submit_post_before and set my own vars like this. But I'm not groking how that all works.
tig_
Registered User
Posts: 31
Joined: Fri Aug 23, 2024 2:42 pm

Re: Adding data to what's submitted by posting.php

Post by tig_ »

Easy-peasy!

Code: Select all

      // in posting_layout_include_panel_body.html
      <!-- hidden input field to the form to track uploaded files, for when Preview is pressed -->
     <input type="hidden" name="tig_blobuploader_uploaded_files" id="uploaded-files-field" value="{TIG_BLOBUPLOADER_UPLOADED_FILES}">

...

      // in the .js behind posting_layout_include_panel_body.html
      hiddenField.value = JSON.stringify(files);

...

        // in main_listener
	public static function getSubscribedEvents()
	{
		return [
			'core.posting_modify_message_text' 	=> 'handle_preview'
		];
	}

	
    public function handle_preview ($event) 
    {
        error_log('handle_preview');
        $preview = $event['preview'];
        error_log('preview: ' . $preview);

        $uploadedFiles = $this->request->variable('tig_blobuploader_uploaded_files', '', true);

		if ($preview === false) {
            return;
        }
        $this->template->assign_vars([
            'TIG_BLOBUPLOADER_UPLOADED_FILES' => $uploadedFiles ,
        ]);
       
   }
Yay!

Image
User avatar
Steve
Registered User
Posts: 1620
Joined: Tue Apr 07, 2009 7:48 pm
Location: Co. Durham, England
Name: Steven Clark

Re: Adding data to what's submitted by posting.php

Post by Steve »

What function you using to handle file uploads?
@ The Chief Medical Officers guideline for men is that: You are safest not to drink regularly more than 14 units per week.
- I drank that today++ :lol: 🍺
tig_
Registered User
Posts: 31
Joined: Fri Aug 23, 2024 2:42 pm

Re: Adding data to what's submitted by posting.php

Post by tig_ »

Steve wrote: Sat Jan 18, 2025 10:35 pm What function you using to handle file uploads?
It's custom.

I have built a hybrid uploader that

- In the browser: converts HEIC to jpg and reduces to "max" size
- On the server (blobuploader.php controller): Calls into an Azure Function that does the heavy lifting of further resizing and creating other images. Dedupes, etc...

Return to “Extension Writers Discussion”