We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38713
    • 91 Posts
    Hi Bruno, Hi Guys,

    Was just wondering if anyone would know how i could make a script that fires when i save a new row in my custom migxdb table to generate a friendly url and stores it in the alias field for that row.

    Really the bit i need to know mainly is how do i assign a script to the save function for migxdb.

    Hopefully someone will be able to help me out.

    Thanks [ed. note: alexmercenary last edited this post 11 years, 4 months ago.]
      Twitter @alexmercenary
      • 4172
      • 5,888 Posts
      normally I have something like that in my custom-update-processor:

              //handle alias
              if (isset($postvalues['changealias']) && $postvalues['changealias'] == 'auto') {
                  $aliasresource = $modx->newObject('modResource');
                  $object->set('alias', $aliasresource->cleanAlias($postvalues['name']));
              }


      I have a listbox-field 'changealias' where I can choose between no||auto||input

      On new records default of this listbox is 'auto', on records with allready existing alias default is 'no'
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 38713
        • 91 Posts
        Ok awesome i'll give that a try and report back. Just one more thing:

        On new records default of this listbox is 'auto', on records with already existing alias default is 'no'

        How do you change the default setting of the listbox for new/existing records.

        Thanks very much
          Twitter @alexmercenary
          • 4172
          • 5,888 Posts
          in my custom-fields.php - processor I have:

          $record = $object->toArray();
          $record['changealias'] = empty($record['alias']) ? 'auto' : '-';
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 38713
            • 91 Posts
            Your a bloody legend! Thanks Bruno,

            Gonna give this all a try.
              Twitter @alexmercenary
              • 38713
              • 91 Posts
              Hmm,

              So i've copied the update.php and fields.php files to my package folder under processors/mgr/default created a changealias field and then created the listbox input selector for the CMP and then added the above code with this change:

              if (isset($postvalues['changealias']) && $postvalues['changealias'] == 'auto') {
                  $aliasresource = $modx->newObject('modResource');
                  $object->set('alias', $aliasresource->cleanAlias($postvalues['model']));
              }


              where i have posted the value of 'model' instead of name as i don't have a name field but when i hit done on an old resource it doesn't create an alias and just sets change alias auto.

              If i manually change the alias then the changealias listbox changes to no. So that appears to be working but not the update.php code.

              On a separate note for the changealias listbox options no||auto||input what does 'input' do?

              Sorry about this.
                Twitter @alexmercenary
                • 4172
                • 5,888 Posts
                ok, sorry, it depends on where you put this code into your update-processor.

                if you do it just before

                $object->fromArray($postvalues);


                it should look like this:

                if (isset($postvalues['changealias']) && $postvalues['changealias'] == 'auto') {
                    $aliasresource = $modx->newObject('modResource');
                    $postvalues['alias'] = $aliasresource->cleanAlias($postvalues['model']);
                }
                
                $object->fromArray($postvalues);


                I have another input-field 'alias' in my form where I can put an alias manually

                for updating the alias, only when you have checked 'auto' or 'input' this can look like that in the update-processor:

                if (isset($postvalues['changealias']) && $postvalues['changealias'] == '-') {
                    unset($postvalues['alias']);
                }
                if (isset($postvalues['changealias']) && $postvalues['changealias'] == 'auto') {
                    $aliasresource = $modx->newObject('modResource');
                    $postvalues['alias'] = $aliasresource->cleanAlias($postvalues['model']);
                }
                
                $object->fromArray($postvalues);



                oh, and you don't need to have the field changealias in your table, its just a field in your form
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 38713
                  • 91 Posts
                  Im very sorry about this but i can't seem to get it working.

                  I have the same set up as yourself. A changealias field and an alias field in my form and i have added your code above to the update.php

                  and kept

                  $record = $object->toArray();
                  $record['changealias'] = empty($record['alias']) ? 'auto' : '-';


                  in the fields.php file but it doesn't set an alias value. Slightly stumped.

                  I don't really understand what the '-' signifies either? Sorry for my naivety
                  I would have though you would compate it against no||auto||input
                    Twitter @alexmercenary
                    • 4172
                    • 5,888 Posts
                    it wasn't
                    no||auto||input 
                    but it was
                    -||auto||input 
                    what I have.


                    if (isset($postvalues['changealias']) && $postvalues['changealias'] == '-') {
                        //if changealias is '-' don't update the alias field, so we unset our posted value here, because we want it untouched
                        unset($postvalues['alias']);
                    }
                    if (isset($postvalues['changealias']) && $postvalues['changealias'] == 'auto') {
                        //if we have selected 'auto' then generate a new alias from 'model'
                        $aliasresource = $modx->newObject('modResource');
                        $postvalues['alias'] = $aliasresource->cleanAlias($postvalues['model']);
                    }
                     
                    // in all other cases, for example when we have selected 'input', use the posted 'alias' from alias-input-field
                    
                    $object->fromArray($postvalues);


                    you can try to check the object-array after updating with the firebug-console
                    and this code:

                    $debug = $object->fromArray();
                    print_r($debug);
                    


                    and make sure you have a alias-field in your table and also your schema and map-files up-to-date with an alias-field.




                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 38713
                      • 91 Posts
                      hmm... Everything is mapped, schema is up to date, all fields are in the table and the above code is in the update file in my processors/mgr/default/ folder.

                      It just doesn't update
                        Twitter @alexmercenary