We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46168
    • 3 Posts
    Hao.
    I'm trying to create a module for modx.
    Lessons was an example of creating an object through the admin panel
    modx/core/components/doodles/processors/mgr/doodle/create.class.php
    <?php
    /**
     * @package doodles
     * @subpackage processors
     */
    class DoodleCreateProcessor extends modObjectCreateProcessor {
        public $classKey = 'Doodle';
        public $languageTopics = array('doodles:default');
        public $objectType = 'doodles.doodle';
    
        public function beforeSave() {
            $name = $this->getProperty('name');
    
            $mail = $this->getProperty('mail');
    
            if (empty($name)) {
                $this->addFieldError('name',$this->modx->lexicon('doodles.doodle_err_ns_name'));
            } else if ($this->doesAlreadyExist(array('name' => $name))) {
                $this->addFieldError('name',$this->modx->lexicon('doodles.doodle_err_ae'));
            }
            if (empty($mail)) {
                $this->addFieldError('mail',$this->modx->lexicon('doodles.doodle_err_ns_name'));
            } else if ($this->doesAlreadyExist(array('mail' => $mail))) {
                $this->addFieldError('mail',$this->modx->lexicon('doodles.doodle_err_ae'));
            }
    
            return parent::beforeSave();
        }
    }
    return 'DoodleCreateProcessor';


    But I need to create an object through a snippet.
    found an example:
    snippet
    //check for duplication
    $count = $modx->getCount('Doodle', array('name' => 'NAME'));
    if($count <= 0){
        $persone = $modx->newObject('Doodle');
        $persone->set('name', 'NAME');
        $persone->set('mail', 'MAIL');
        
        if($persone->save()){
            echo "true";
        }else{
            echo "false";
        }
    }else{
        echo "false";
    }


    create.class.php has a valid input check and duplication. But a snippet must duplicate all checks. Can I apply to a snippet of the methods in create.class.php?
    Sorry for my english.

    This question has been answered by wiegers. See the first response.

    • discuss.answer
      • 37110
      • 72 Posts
        • 46168
        • 3 Posts
        thank you very much!

        //array parameters
        $processorProps = array(
            'name' => 'test11',
           'mail' => 'test11',
        );
        // array options for runProcessor
        $otherProps = array(
            // where processors
            'processors_path' => $modx->getOption('core_path') . 'components/doodles/processors/'
        );
        
        $response = $modx->runProcessor('mgr/doodle/create', $processorProps, $otherProps);
        
        if ($response->isError()) {
            $errors = $modx->error->failure($response->getMessage());
            // echo errors mesages
            /*foreach($errors['errors'] as $error){
                echo $error['msg'];
            }*/
            return $modx->error->failure($response->getMessage());
        }
        //echo id
        return $response->response['object']['id'];
        [ed. note: kurojneko last edited this post 12 years, 9 months ago.]