Jump to content

Hook: adding an image to an existing image gallery


rooofl
 Share

Recommended Posts

Hi! I am trying to create a hook that takes the value of a form file field, and add it to an image gallery of an existing page.

$forms->addHookBefore('FormBuilderProcessor::processInputDone', function($e) {
  $form = $e->arguments(0);
  if($form->name == 'new-inuse') {
	// related_font is a page selector
    $font = $form->getChildByName('related_font');
	// getting the page name
    $fontName = $font->attr('value')->name;
	// finds the page from its name
    $fontPage = wire('pages')->find("name=$fontName");
	// in_use_image is a file upload field
    $image = $form->getChildByName('in_use_image');
	// trying to add the image to the existing image gallery in_use field
    $fontPage->in_use->add('$image'); // error here

    $fontPage->save();
  }
});

This outputs the following error: `Call to a member function add() on null`. Any idea what’s wrong with my code?

Link to comment
Share on other sites

Thank you for your answer and for pointing my mistakes!

Here is an updated code with a new error. Somehow add() refuse the $image object:

$forms->addHookBefore('FormBuilderProcessor::processInputDone', function($e) {
  $form = $e->arguments(0);
  if($form->name == 'new-inuse') {
    $font = $form->getChildByName('related_font');
    $fontName = $font->attr('value')->name;
    $fontPage = wire('pages')->findOne("name=$fontName");
    $image = $form->getChildByName('in_use_image');
    $fontPage->of(false);
    $fontPage->in_use->add($image);
    $fontPage->save();
  }
});
Error: Exception: Item added to Pageimages is not an allowed type (in wire/core/WireArray.php line 286)

How can I make sure $image is a valid image object? I tried to echo $image->url but nothing is happens.

Link to comment
Share on other sites

I think $image is likely an array of file paths. What I like to do in such cases is to dump the variable in question to a PW log.

wire('log')->save('form-builder-debug', print_r($image, true));

This should give you a clue when you look into Setup -> Logs -> form-builder-debug.

  • Like 1
Link to comment
Share on other sites

Thank you for letting me know about wire('log'), it is much more handful that the way I found to debug.
This is what my $image object looks like. I can’t find any documentation about this object and how to find the image url (for instance), or anything else to give to add().

InputfieldFormBuilderFile Object ( [data] => Array ( [label] => In use image [description] => Test desc [icon] => [notes] => [detail] => [head] => [required] => 1 [requiredIf] => [collapsed] => 0 [showIf] => [columnWidth] => [skipLabel] => [wrapClass] => [headerClass] => [contentClass] => [textFormat] => 4 [renderValueFlags] => 0 [prependMarkup] => [appendMarkup] => [formID] => 1 [entryID] => 0 [processor] => FormBuilderProcessor Object ( [data] => Array ( [id] => 1 [saveFlags] => 9 [skipSessionKey] => [formName] => new-inuse [submitText] => Submit [honeypot] => [turingTest] => Array ( ) [emailTo] => [emailFrom] => [emailFrom2] => [emailSubject] => Form Submission [responderTo] => [responderFrom] => [responderSubject] => Auto-Response [responderBody] => [successUrl] => [successMessage] => Thank you, your form has been submitted. [errorMessage] => One or more errors prevented submission of the form. Please correct and try again. [action2] => [action2_add] => Array ( ) [action2_remove] => Array ( ) [action2_rename] => Array ( ) [akismet] => [allowPreset] => [savePageParent] => 1006 [savePageTemplate] => 43 [savePageFields] => Array ( [name] => related_font [1] => title [114] => in_use_image ) [savePageStatus] => 0 [framework] => Basic [fbForm] => FormBuilderForm Object ( [forms:protected] => FormBuilderMain Object ( [formNames:protected] => Array ( [1] => new-inuse ) [database:protected] => WireDatabasePDO Object ( ) [filesPath:protected] => /site/assets/cache/form-builder/ [config:protected] => Config Object ( [data] => Array ( [dbName] => velvetyndev [internal] => 1 [urls] => Paths Object ( [data] => Array ( [wire] => wire/ [site] => site/ [modules] => wire/modules/ [siteModules] => site/modules/ [core] => wire/core/ [assets] => site/assets/ [cache] => site/assets/cache/ [logs] => site/assets/logs/ [files] => site/assets/files/ [tmp] => site/assets/tmp/ [templates] => site/templates/ [fieldTemplates] => site/templates/fields/ [adminTemplates] => wire/templates-admin/ [AdminThemeUikit] => wire/modules/AdminTheme/AdminThemeUikit/ [AdminThemeReno] => wire/modules/AdminTheme/AdminThemeReno/ [AdminThemeDefault] => wire/modules/AdminTheme/AdminThemeDefault/ [FileCompilerTags] => wire/modules/ [PageFrontEdit] => wire/modules/Page/PageFrontEdit/ [ProcessPageView] => wire/modules/Process/ [ProcessPageSort] => wire/modules/Process/ [ProcessPageSearch] => wire/modules/Process/ProcessPageSearch/ [ProcessPagesExportImport] => wire/modules/Process/ProcessPagesExportImport/ [ProcessRecentPages] => wire/modules/Process/ProcessRecentPages/ [ProcessHome] => wire/modules/Process/ [ProcessModule] => wire/modules/Process/ProcessModule/ [ProcessPageEdit] => wire/modules/Process/ProcessPageEdit/ [ProcessTemplate] => wire/modules/Process/ProcessTemplate/ [ProcessPageLister] => wire/modules/Process/ProcessPageLister/ [ProcessLogger] => wire/modules/Process/ProcessLogger/ [ProcessForgotPassword] => wire/modules/Process/ [ProcessPageAdd] => wire/modules/Process/ProcessPageAdd/

 

Link to comment
Share on other sites

2 hours ago, rooofl said:

This is what my $image object looks like. I can’t find any documentation about this object and how to find the image url (for instance), or anything else to give to add().

But we can at least see that it's an InputfieldFormBuilderFile object. Since FormBuilder Inputfields inherit (I took a peek into the code) from the regular Inputfield class, you can refine your debug output to only show its value attribute (you're already reading $font->attr('value') - it's the way to go with all form fields). I'm pretty sure it's going to be some kind of array.

wire('log')->save('form-builder-debug', print_r($image->attr('value'), true));

 

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By joe_ma
      Sorry for asking this question here, but I cannot ask it in the VIP section because I use an version of FormBuilder that is expired.
      I have a fairly simple form on a website done with FormBuilder (PW 3.0.148, FormBuilder 0.2.4) that seems to work quite well. I've tested it with Firefox (88.0.1), Chrome, Opera, Edge and it works in all browsers as expected. The form is embedded in the page by pasting the name of the form in the body field.
      Now my customer tells me, that her Firefox (88.0.1) is blocking the page where the form is embedded: "Firefox may not open this page" for safety reasons.
      I have no clue as to what seems to be the problem here, since the form is on the same server and has the same domain.
    • By picarica
      So hello i am trying to get a .png file from file field and put it automatically to image field, why png image is in the file field is because i already have a hook that extracts .zip and uploads all content into file field, but i just realizes i cant use size() function on image in file field so i am tryin got reupload it to images field
      i already have something like  this in ready.php
      $word = ".png"; foreach($page->subor_hry as $file) { if(strpos($file, $word) !== false){ $page->images_thumb = $file->url; } } by my logic it should work but it dosnt i get error ProcessPageEdit: Unable to read: /site-hry/assets/files/1027/flash_fishy_screenshot.png
      when i remove url from $file->url i just get ProcessPageEdit: Item added to ProcessWire\Pageimages is not an allowed type
      so what am i doing wrong? is there some other way to do this ?
      also can i have all this in
      $this->addHookAfter('Pages::saveReady', function(HookEvent $event) { whats the correct function to have it apply on all pages ?
    • By picarica
      so what i am trying to do is that i uploded some files into file field, and then i want hook to get MD5sum and other stuff from .xml and put it into text field into it coresponding pages, pages like this are gonna be many, for each one i want it to output it into its fields
    • By Paul Greinke
      Hi there. I wrote a custom module for one of my projects. In fact I maybe want to use my module in other projects too. In order to be variable and customizable  I need to implement some custom hooks into my module. So I can afterwards hook into the my functions in order to modify them to match the needs of the new project.
      I tried simply defining functions with the '__' prefix. But that did not work. I'm imagining something like the following:
      <?php class MyClass { public function ___someFunction() { // Do something } } // ready.php $this->addHookBefore('MyClass::someFunction', function($event) { // some customization }); Is there a way to accomplish that? 
    • By picarica
      Hello so am trying to make a hook so that all checkbox field is defaul;ty checked when making new page, soo i made hook in init.php and the contents are
      <?php $wire->addHookAfter("Pages::added", function($event) { $page = $event->arguments(0); // check for page template if necessary here $page->checkboxfield('check', 1); }); ?> and what i get
      which is weird because "table" is a repeat field where checkboxfield is nowhere present, but table repeat field and checkboxfield are in the same template, so why does it outputs error like that? any ideas ?
×
×
  • Create New...