We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34039
    • 6 Posts
    Hi all, I need to use the formit2resource (from http://ttyl.modx.com/thread/?thread=54204.0%3Bwap&i=1&page=1) snippet on a site, but I've been encountering some strange problems as of late.

    I made some slight modifications and have a working instance of it on a separate site, using the same version of FormIt and MODX (currently 2.0.1 and 2.1.3 trad, respectively).

    Anyway, the error I'm getting on the site it's not working on happens after you've filled all the fields and hit submit; it goes to a new page and says:
    Fatal error: Call to a member function getOption() on a non-object in /home/*snip*/core/model/modx/modresource.class.php on line 681

    This happens no matter how many fields I have (I tried reducing it to one simple text field and it still did not work), and no matter how I make the FormIt call (I tried using a FormIt call with only the absolute necessary fields for it to work).

    Here is the formit2resource snippet I am using on both sites:
    <?php
    $doc = NULL;
    $doc = $modx->newObject('modResource');
    $doc->set('parent', $fi2ResParent);
    $doc->set('alias', "event-".time());
    $doc->set('context_key', "events");
    $doc->set('createdby', $modx->user->get('id'));
    
    $allFormFields = $hook->getValues(); 
    foreach ($allFormFields as $field=>$value)
    {
    	if ($field == 'pagetitle' || $field == 'content')
    	{
    		$doc->set($field, $value);
    	}
    }
    
    $doc->set('template', '26');
    $doc->save();
    foreach ($allFormFields as $field=>$value)
    {
    	if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
    	{
    		/* handles checkboxes & multiple selects elements */
    		if (is_array($value))
    		{
    			$featureInsert = array();
    			while (list($featureValue, $featureItem) = each($value))
    			{
    				$featureInsert[count($featureInsert)] = $featureItem;
    			}
    			$value = implode('||',$featureInsert);
    		}	
    		$tv->setValue($doc->get('id'), $value);
    		$tv->save();
    	}
    }
    return true;


    A few lines of code were added to the top to attempt to debug, but it didn't change anything.

    Here's the full FormIt call I am using:
    [[!FormIt? &hooks=`spam,file2formit,formit2resource,redirect` &redirectTo=`3665` &validate=`nospam:blank, pagetitle:required, content:required:stripTags, event-date:required, event-addy:stripTags:required, event-country:required, event-time:required, event-fees:stripTags:required, event-artists:stripTags:required` &fi2ResParent=`3691` &path=`*snip*` &extensions=`jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF` &maxsize=`786432`]]


    Any help or ideas is greatly appreciated, and I thank you in advance.
    • Is the name of the context you want the resource in exactly "events", all lower case and plural?

      That is the context_key value you are setting in your line 6.

      Line 681 of the modResource class is this:
      $containerSuffix= $workingContext->getOption('container_suffix', '');


      It implies that $workingContext is not a valid modContext object as would be expected (your error: "call to a function on a non-object"). Looking back to where $workingContext is defined (line 667):
      $workingContext = $this->xpdo->getContext($fields['context_key']);



      Pretty sure you're context_key is off, and that would explain why it could work in your other installation.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 33988
        • 110 Posts
        Thanks Mark, this is the code for the snippet, as far as I can tell it is setting the context_key correctly. The exact key is "events".
        <?php
        $doc = NULL;
        $doc = $modx->newObject('modResource');
        $doc->set('parent', $fi2ResParent);
        $doc->set('alias', "event-".time());
        $doc->set('context_key', "events");
        $doc->set('createdby', $modx->user->get('id'));
        
        $allFormFields = $hook->getValues(); 
        foreach ($allFormFields as $field=>$value)
        {
        	if ($field == 'pagetitle' || $field == 'content')
        	{
        		$doc->set($field, $value);
        	}
        }
        
        $doc->set('template', '26');
        $doc->save();
        foreach ($allFormFields as $field=>$value)
        {
        	if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
        	{
        		/* handles checkboxes & multiple selects elements */
        		if (is_array($value))
        		{
        			$featureInsert = array();
        			while (list($featureValue, $featureItem) = each($value))
        			{
        				$featureInsert[count($featureInsert)] = $featureItem;
        			}
        			$value = implode('||',$featureInsert);
        		}	
        		$tv->setValue($doc->get('id'), $value);
        		$tv->save();
        	}
        }
        return true;
          Sal Sodano
          President & Co-Founder of SkyToaster
          My Website Salscode.
        • From that line it definitely seems to be an issue with fetching the context though.

          No ACL stuff on the events context that may be preventing you from handling it?
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 3749
            • 24,544 Posts
            getOption() is a member of the $xpdo object (and, by extension, the $modx object) so the error implies that the $xpdo or $modx object is not available.

            That suggests that the context is not being properly initialized in index.php or a gateway plugin.

            BTW, you should use $doc->setContent($content) to set the content and some sanity checks to make sure the pagetitle and content are not empty might be a good idea.

              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 33988
              • 110 Posts
              Thank you for replying everyone. We am using the index.php method, which I have used on other sites without any problems, and made sure to change the code in the index file to
              $modx->initialize('events');


              PS: Thank you for the note about sanity checks, didn't occur to us.
                Sal Sodano
                President & Co-Founder of SkyToaster
                My Website Salscode.
                • 3749
                • 24,544 Posts
                The only thing I can think of to suggest is to make sure that code is actually executing (maybe write something to a file inside the "if" statement and put an else that writes something else that might be diagnostic).
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 34039
                  • 6 Posts
                  Hey everyone, thanks again for the replies. I did check to see if the code is executing, and it seems to work up until the:
                  $doc->save()

                  at which point the error starts occurring. (just put return statements every line until it threw the error)

                  Like I mentioned, I used essentially the exact same snippet (without a few of the diagnostic changes we made here) perfectly on another site. The only difference is that this site is using a separate context, so it's likely that something may be wrong there.

                  Sal did make the change that was mentioned regarding the context that Bob mentioned, and it didn't seem to change anything.
                  • Out of the blue my site starts to throw me that error when saving a resource through the manager UI o.O

                    Heavily using ACLs but the last time I worked on a blog I didn't have this issue and according to the manager log nothing has been changed since. Full admin permissions on my group for web and mgr contexts, as well as "resource". I'm stumped.

                    Did you guys manage to sort out what is the problem for your situation?

                    EDIT: Huh.. and now it works again. Okay, only in Chrome. WTF.
                    EDIT2: Frick'n hell! I got it! I've updated VersionX on this install to the latest dev version which adds a Versions tab to the resource update screen. Which has a filter on context_key. Which is probably getting posted along with the save, resulting in this error stemming from the fact context_key is empty (while the hidden fields in the normal form have the regular value). #face #desk [ed. note: markh last edited this post 12 years, 3 months ago.]
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.