Jump to content

Required field error showing when add images via API


alexcapes
 Share

Recommended Posts

I have made the following module that automatically grabs images from Youtube and Vimeo URLs and adds them to an image field ('article_visual_snippet').

	    public function init() {
        $this->pages->addHookBefore('save', $this, 'getvideoimage');
    }

    public function getvideoimage($event) {
        $page = $event->arguments[0];
        $url = $page->article_snippet_video;

        if ($page->parent_id == 1023) {

            if (strpos($url, 'youtube') > 0) {
                parse_str( parse_url( $url, PHP_URL_QUERY ), $id );
                $ytURL = 'http://img.youtube.com/vi/' . $id['v'] . '/maxresdefault.jpg';
                if($page->article_visual_snippet->count() < 1){
                    $page->of(false);
                    $page->article_visual_snippet = 'http://img.youtube.com/vi/' . $id['v'] . '/maxresdefault.jpg';
                    $page->save();
                }
            }
            if (strpos($url, 'vimeo') > 0) {
                $id = substr( parse_url( $url, PHP_URL_PATH ), $id );
                if($page->article_visual_snippet->count() < 1){
                    $page->of(false);
                    $response = file_get_contents("https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com$id");
                    $jsonobj = json_decode($response);
                    $page->article_visual_snippet = $jsonobj->thumbnail_url;
                    $page->save();
                }
            }
        }
    }
}

It's all working perfectly but for one slight problem.

The image field ('article_visual_snippet') is a required field and when the page is saved I get 'Missing required value' error even though the image has successfully uploaded to the field.

Does anyone know how I can circumvent this error?

Link to comment
Share on other sites

Have you tried $page->article_visual_snippet->add("path or URL to image") instead of $page->article_visual_snippet = URL. Not sure if it will make a difference, but maybe worth a try.

And you can try to hook on saveReady instead of save.

Is article_visual_snippet a single image field or should it hold multiple images. If only 1 image, you could set Maximum files allowed to 1 and Formatted value to 'Single Item (null if empty) and do

if($page->article_visual_snippet){...

instead of if($page->article_visual_snippet->count() < 1)

Link to comment
Share on other sites

Have you tried $page->article_visual_snippet->add("path or URL to image") instead of $page->article_visual_snippet = URL. Not sure if it will make a difference, but maybe worth a try.

It should make a difference, as files and images fields are always multi value fields only the output formatting does provide the necessary shortcuts when accessing single image/file fields. As of() is false when saving things you need to handle the field as multi value field.

  • Like 2
Link to comment
Share on other sites

Have you tried $page->article_visual_snippet->add("path or URL to image") instead of $page->article_visual_snippet = URL. Not sure if it will make a difference, but maybe worth a try.

And you can try to hook on saveReady instead of save.

Is article_visual_snippet a single image field or should it hold multiple images. If only 1 image, you could set Maximum files allowed to 1 and Formatted value to 'Single Item (null if empty) and do

if($page->article_visual_snippet){...

instead of if($page->article_visual_snippet->count() < 1)

$page->article_visual_snippet->add("path or URL to image") also works but Missing required value error still appears after the page has saved and the image is in the field.

It should make a difference, as files and images fields are always multi value fields only the output formatting does provide the necessary shortcuts when accessing single image/file fields. As of() is false when saving things you need to handle the field as multi value field.

Thanks for reminder of this - I always seem to forget that this outputting formatting changes things to multi-field value.

Link to comment
Share on other sites

You stated you received the following error:

  

$page->article_visual_snippet->add("path or URL to image") also works but Missing required value error still appears after the page has saved and the image is in the field.

I gave a link that talks about possibly using the wire() function when you call the ProcessWire API.

If you, for what ever reason, cannot pass a API variable to a function, you always can get there handles via the wire("nameofapivar") function. This works everywhere, but has a (very little) overhead. Also I don't think it is measurable with a few calls, (less than 100).

I'm sorry if I didn't elaborate, as connecting to the ProcessWire Forum today is taking awhile (at least for me) for pages to display.

I can also admit that my understanding of the error and it's relation to how to call the ProcessWire API within a function may not be 100 percent correct.  If I'm wrong, then finding out the correct answer to why the error is still there will be a great learning experience for myself and hopefully others.

Link to comment
Share on other sites

You stated you received the following error:

I gave a link that talks about possibly using the wire() function when you call the ProcessWire API.

I'm sorry if I didn't elaborate, as connecting to the ProcessWire Forum today is taking awhile (at least for me) for pages to display.

I can also admit that my understanding of the error and it's relation to how to call the ProcessWire API within a function may not be 100 percent correct.  If I'm wrong, then finding out the correct answer to why the error is still there will be a great learning experience for myself and hopefully others.

No worries and thank you for your help :) Was trying to work out if I needed to be calling the wire function but can't see that's a solution (or at least I can't see where it would be required).

Just to be clear the module is working correctly, it's just that Processwire is throwing a 'Missing required field' error even though the field is not missing (the image has been correctly uploaded). It's probably something I could live with but the client will definitely get confused by the incorrect error.

Link to comment
Share on other sites

Hey guys, I'm not 100% sure, but hooking before_save and than saving within the module looks not completly clean to me. Maybe I'm not right, but I would try it once without saveing in the module, because saving is done directly after the modules hook function is executed.

Edited by horst
Link to comment
Share on other sites

Hey guys, I'm not 100% sure, but hooking before_save and than saving within the module looks not completly clean to me. Maybe I'm not right, but I would try it once without saveing in the module, because saving is done directly after the modules hook function is executed.

I think you're right here - removed any saving from the module itself and the image still saves correctly.

Did you already try to change your hook event from 'save' to 'saveReady' ?

... and added 'saveReady' instead of 'save' but it's still throwing up the 'Missing required field' error.

Link to comment
Share on other sites

Did you already try to change your hook event from 'save' to 'saveReady' ?

@gebeer: That's "the same in green" :)  - When saving in a method that  is triggered before saving (regardless if before_saving or if save_ready) every save action triggers it once more.

Link to comment
Share on other sites

  • 3 weeks later...
  • 9 months later...

I think you're right here - removed any saving from the module itself and the image still saves correctly.

... and added 'saveReady' instead of 'save' but it's still throwing up the 'Missing required field' error.

Hi, did you find the error? I'm having the exact same problem.

Link to comment
Share on other sites

A few comments -

  1. I think this is a PW bug and should be reported.
     
  2. Do you really need to make the field required? If you are populating the image automatically, then how could it be empty?
     
  3. You should only save the field, not the entire page - this way the save hook won't be triggered again, so do this: $page->save('article_visual_snippet');
     
  4. Just looking for maxresdefault.jpg will bite you at some point because not all YouTube videos have this image - this is why in my module (http://modules.processwire.com/modules/process-get-video-thumbs/) I look through all the possible options so you will always get something.
  • Like 1
Link to comment
Share on other sites

A few comments -

  1. I think this is a PW bug and should be reported.

     

  2. Do you really need to make the field required? If you are populating the image automatically, then how could it be empty?

     

  3. You should only save the field, not the entire page - this way the save hook won't be triggered again, so do this: $page->save('article_visual_snippet');

     

  4. Just looking for maxresdefault.jpg will bite you at some point because not all YouTube videos have this image - this is why in my module (http://modules.processwire.com/modules/process-get-video-thumbs/) I look through all the possible options so you will always get something.

1. Would you mind pointing me to the process of reporting bugs?

2. Good point although from what I remember (it was a while ago!) it was needed tobe required so the client didn't publish without an image.

3. Noted on this as a better approach.

4. The module was for a specific client who uploads videos in HD only so I was pretty confident no video would be without maxresdefault.jpg.

Link to comment
Share on other sites

Would you mind pointing me to the process of reporting bugs?

https://github.com/ryancramerdesign/ProcessWire/issues

Good point although from what I remember (it was a while ago!) it was needed to be required so the client didn't publish without an image.

Why not make article_snippet_video required? Your hook will take care of the image if the video field is required.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...