We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46434
    • 22 Posts
    I've configured a MyComponent project with several chunks, resources, and property sets, as well as a new system event, and a plugin to which I am binding the event...the name of my event is OnFormsInstall. I am trying to invoke that event in my resource.resolver.php script, for use by the plugin, however the event doesn't appear to be firing because the plugin is not being executed. Every other aspect of the package install is successful. In addition to the project config setup, my ModX install also has the correct event bound to the plugin.

    FormsInstall.plugin.php
    ...
    $eventName = $modx->event->name;
    switch($eventName) {
    	case 'OnFormsInstall':
    			$modx->log(modX::LOG_LEVEL_INFO,'id: '.$resource->get('id').' | '.'pagetitle: '.$resource->get('pagetitle').' | '.'mode: '.$mode);
    		break; 
    }
    


    project config
    ...
    'newSystemEvents' => array(
    	'OnMyEvent1' => array(
    		'name' => 'OnFormsInstall',
    		'groupname' => 'FormsInstall',
    		'service' => 1,
    	),
    ),
    ...
    

    ...
    'elements' => array(
    	'plugins' => array(
    		'FormsInstall' => array(
    			'category' => 'FormsInstall',
    			'description' => '',
    			'static' => true,
    			'static_file' => 'assets/plugins/FormsInstall/FormsInstall.plugin.php',
    			'events' => array(
    				'OnFormsInstall' => array(
    					'priority' => '0', /* priority of the event -- 0 is highest priority */
    					'group' => 'plugins', /* should generally be set to 'plugins' */
    				),
    			),
    		),
    	),
    	
    	'chunks' => array(
    ...
    


    resource.resolver.php
    ...
    $resource->save(); // AUTO-GENERATED BY MYCOMPONENT
    
    // CUSTOM CODE
    $resourceId = $resource->get('id');
    $pagetitle = $resource->get('pagetitle');
    
    $result = $modx->invokeEvent('OnFormsInstall',array(
    	'mode' => 'new',
    	'resource' => $resource,
    	'id' => $resourceId,
    ));
    
    $modx->log(modX::LOG_LEVEL_INFO,'id: '.$resourceId.' | '.'pagetitle: '.$pagetitle);
    ...
    


    Ideally I would prefer to invoke the OnDocFormSave event after $resource->save(), however when I try that I am given an Array to String Conversion error.

    Does anybody know why this is not working, or if invoking a custom event on install is even possible?

    Thanks in advance

    ModX Install
    ModX Revolution 2.3.4-dev
    MyComponent 3.2.2
      • 3749
      • 24,544 Posts
      I've never tried invoking an event during install that's created by the package, though it should be possible. The resource resolver runs fairly early and the plugin and/or the plugin event may not be installed at that point.

      If you create a default resolver for your package, it will run last and the pieces of the package will all be installed ahead of it, so you may not actually need your custom event and plugin (unless they'll be needed later).

      You should be able to get the resource object by pagetitle or alias with getObject() and do what you want in the default resolver. You might be able to invoke the custom event there, though I don't think you'll need to.

      Maybe I'm misunderstanding what you're trying to do.
        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
        • 46434
        • 22 Posts
        That is essentially what I'm trying to do, however I would like to only run the selected action on the resources created by the component, and would prefer to use the id...however after the resource is created the id is different then what is specified in the project config. Access to the resource creation loop is nice in that way. Do you know why invoking the OnDocFormSave from the resource resolver doesn't work?

        UPDATE
        After some more testing, it appears the new event is not created yet when the default resolver runs. The event only seems to be invoked on reinstall.
        I am able do what I need by copying the resource resolver to the default resolver, and modifying the code slightly.

        Can you confirm my findings on new events not being created when the default resolver runs? [ed. note: ahaller last edited this post 11 years, 6 months ago.]
          • 3749
          • 24,544 Posts
          Now that I think about it, I think they events probably created, but not registered with MODX, so MODX doesn't know they exist yet.

          You're correct about the Resource IDs being different. It's easy to alter that in build.transport.php, but then you run the risk of collisions with existing Resources. That said, the pagetitle is just as good as the ID, since there's no chance it has changed since you saved it a few seconds earlier.

          I don't know why OnDocFormSave doesn't work. Possibly it's a permission issue, but more likely the resource ID is wrong. You could try getting the resource again by pagetitle after the save() with $modx->getObject(). You might have to call $modx->reloadContext() first.

          Another option would be to try calling the resource/update processor rather than calling save(). If it works, the processor will invoke OnDocFormSave for you, but you'll need the correct ID.

          $fields = array(
              'id' => $id,
              'pagetitle' => 'My Page',
              'alias' => 'my-page',
              'published' => '1',
              // etc.
          );
          $modx->runProcessor('resource/update', $fields);
            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
            • 46434
            • 22 Posts
            I tried running the resource update processor from the default resolver using the same fields array that was created in the resources resolver, however I get the same array to string conversion notice as when I attempt to directly invoke OnDocFormSave along with some additional debug output.

            Here is my default resolver: forms.resolver.php
            <?php
            if (!function_exists('checkFields')) {
                function checkFields($required, $objectFields) {
                    global $modx;
                    $fields = explode(',', $required);
                    foreach ($fields as $field) {
                        if (! isset($objectFields[$field])) {
                            $modx->log(modX::LOG_LEVEL_ERROR, '[Forms Resolver] Missing field: ' . $field);
                            return false;
                        }
                    }
                    return true;
                }
            }
            if($object->xpdo) {
                $modx =& $object->xpdo;
                switch ($options[xPDOTransport::PACKAGE_ACTION]) {
                    case xPDOTransport::ACTION_INSTALL:
                    case xPDOTransport::ACTION_UPGRADE:
            
                        $intersects = array (
                            0 =>  array (
                              'pagetitle' => 'Forms',
                              'parent' => '0',
                              'template' => 'Default Template [12]',
                            ),
                            1 =>  array (
                              'pagetitle' => 'Business Account',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            2 =>  array (
                              'pagetitle' => 'Business ACH',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            3 =>  array (
                              'pagetitle' => 'Business Advice',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            4 =>  array (
                              'pagetitle' => 'Business Loan',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            5 =>  array (
                              'pagetitle' => 'Business Payroll',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            6 =>  array (
                              'pagetitle' => 'Solutions Cart',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            7 =>  array (
                              'pagetitle' => 'Business Services Request',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            8 =>  array (
                              'pagetitle' => '100% Satisfaction',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            9 =>  array (
                              'pagetitle' => 'Relationship Rewards Login',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            10 =>  array (
                              'pagetitle' => 'Credit Dispute',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                            11 =>  array (
                              'pagetitle' => 'UCF Alumni',
                              'parent' => 'Forms',
                              'template' => 'Default Template [12]',
                            ),
                        );
            
                        if (is_array($intersects)) {
                            $modx->log(modX::LOG_LEVEL_INFO,'<br />Updating Resources:');
                            foreach ($intersects as $k => $fields) {
                                /* make sure we have all fields */
                                if (! checkFields('pagetitle,parent,template', $fields)) {
                                    continue;
                                }
                                $resource = $modx->getObject('modResource', array('pagetitle' => $fields['pagetitle']));
                                if (! $resource) {
                                    continue;
                                }
                                $resourceId = $resource->get('id');
                                $pagetitle = $resource->get('pagetitle');
                                $fields['id'] = $resourceId;
                                $fields['context_key'] = 'mgr';
                                
                                $response = $modx->runProcessor('resource/update',$fields);
                                if ($response->isError()) {
                                    $modx->log(MODX_LOG_LEVEL_INFO,'<pre>'.print_r($response->getMessage(),true).'</pre>');
                                    continue;
                                }
                                $docArray = $response->getObject();
                                $modx->log(MODX_LOG_LEVEL_INFO,'The resource "'.$docArray['pagetitle'].' was updated with ID '.$docArray['id']);
                            }
                            
                            $modx->log(modX::LOG_LEVEL_INFO,'<br />');
                            
                        }
                        break;
            
                    case xPDOTransport::ACTION_UNINSTALL:
                        break;
                }
            }
            
            return true;
            


            And here is the output from attempting the package install:
            Console running...
            Attempting to install package with signature: forms-2015.03.10-pl
            Package found...now preparing to install.
            Grabbing package workspace...
            Workspace environment initiated, now installing package...
            Attempting to preserve files at [hidden for privacy]
            
            Updating Resources:
            PHP notice: Array to string conversion
            DELETE FROM `modx_register_messages` WHERE ( `modx_register_messages`.`topic` = :0 AND `modx_register_messages`.`id` = :1 ) Array ( [/:0\b/] => 1 [/:1\b/] => '996009f2374006606f4c0b0fda878af1' ) 
            
              • 3749
              • 24,544 Posts
              The only place I could find that deals with modx_register_messages is the resource/locks/release processor. I don't suppose you have that resource open in the Manager during the install I don't see anything wrong with your code.

              It's a notice, not an error, so turning off E_NOTICE errors would make it go away.

              Does the code still do what it's supposed to in spite of the warning?
                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
                • 46434
                • 22 Posts
                I started writing a reply a week ago, and may have closed the session before it sent.

                No the code doesn't do what it's supposed to, and the package install fails.

                I ended up solving the problem by running the plugin code directly in the default resolver.

                It would be nice if a future MyComponent update had the ability to run a processor or trigger an event.