0

I need to write a plugin that needs to create table in database and some setting from installation form. I can easily create form but I have difficulty to run the script after installation to read options and create table. Is it possible at all to run such simple script or maybe you need to create everything like for example models, vehicles and so on?

I would appreciate if anyone could give me directions how to do it. Modx documentation is not clear about this and https://github.com/splittingred/Doodles/tree/production sample repo contains multiple elements I'm not familiar with and I believe don't need at all

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Do you just want to create a new table? Or do you want to create a new component? Or do you want to create a extra that can be installed on difference MODX Revolution installations via a .vehicles.zip file too? – OptimusCrime Sep 13 '17 at 10:18
  • Thanks for response, what I've done so far is creating 3rd party component (not sure about naming) with 2 files `build.transport.php` and `setup.options.php` + 3 text files (changelog, license, readme) and I've "hacked" setup_options because in there I fired creating table in database. I know this is not the right place to do it but I have no idea how to launch any custom script after setup.options. I don't need any models and so on, because the only thing what I want is create table in database, add some setting and listen to event. The last one I haven't manged to do this way. – Marcin Nabiałek Sep 13 '17 at 10:22
  • Do you intend to use this component on a single MODX installation, or is this something you should be able to submit to the MODX Extra repository? If you just want it on the installation you are working on, then you don't need the `_build` directory and setup. You can just create the model directly in your components directory. Have you read this tutorial https://docs.modx.com/revolution/2.x/case-studies-and-tutorials/developing-an-extra-in-modx-revolution ? – OptimusCrime Sep 13 '17 at 10:25
  • Unfortunately I need to create package for this because this is non-standard installation using Gitify and the whole core/connectors directories are in .gitignore. I upload this package locally and it does the job when I install it. – Marcin Nabiałek Sep 13 '17 at 10:28
  • So, exactly what is your problem? What works? – OptimusCrime Sep 13 '17 at 10:46
  • The problem is that I create table in setup options and this is place where you should ask for user input but I've done it here because I don't know how I can run any code after typing by user any setup options. `$builder->setPackageAttributes([ 'setup-options' => [ 'source' => $sources['build'] . 'setup.options.php', ], ]);` Is it possible to add here any script that should be run after setup-options has finished their job? – Marcin Nabiałek Sep 13 '17 at 10:49
  • The easiest is to look at other packages that uses setup options and how they do it. For example: https://github.com/Sterc/FormIt/blob/develop/_build/setup.options.php – OptimusCrime Sep 13 '17 at 12:00

1 Answers1

0

Typically you'd use a resolver to run code after the install.

While in the question comments the setup options are discussed, the package attributes there are actually executed to generate the setup options form, not to process the results.

The docs are a tad dated (mostly the screenshots), but Creating a 3rd party build script explains the different parts of a build script, and what they're for, with a fair bit of examples.

The piece you're looking for is this:

$vehicle->resolve('php',array(
    'source' => $sources['resolvers'] . 'setupoptions.resolver.php',
));

You'll need to have a $vehicle (perhaps from a category or other object you're adding to the build) and the file in the provided location. Inside the resolver file you can use $object->xpdo as an instance of the modX class to do your thing.

Mark Hamstra
  • 692
  • 1
  • 4
  • 16