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

    1) which is the best way to install resource documents with large content(!) with a Revolution transport package?
    2) And how do I preselect a specific template for this resource (which is also installed with the same transport package)?

    For better understanding of my question please see the php remarks in following code:

    $resources = array();
    
    $resources[1] = $modx -> newObject('modResource');
    $resources[1] -> fromArray(array(
    	'id' => 1,
    	'pagetitle' => 'Example Resource',
    	'parent' => 0,
    	'isfolder' => 0,
    	'hidemenu' => 0,
    	'alias' => 'example-resource',
    	'published' => 1,
    	'searchable' => 1,
    	'template' => /* how can I get the id of the template during installation? */,
    	'menuindex' => 0,
    	'longtitle' => 'Example Resource',
    	'description' => 'Example Resource Template',
    	'introtext' => 'Example Resource Template',
    	'content' => /* large part of mixed HTML + snippet calls doesnt fit here very well */
    ), '', true, true, false);
    
    return $resources;
    


    Should I create a static resource which is fetched when building the transport package?
    Is there a build.transport.php example which shows how to install resource documents?

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

      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 3749
      • 24,544 Posts
      I would use MyComponent. Create the resources, specify the pagetitles in the MyComponent project config file under $resources and $exportResources. Then run ExportObjects. That will create all the necessary files and resolvers for you and the build.transport.php file will automatically put them all in the package.
        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
        • 10378
        • 375 Posts
        Hi Bob,

        thanks for your hint but I already have a full featured build.transport.php and only need to integrate some resource documents. The problem is, the resources have a lot of content in its "content" field. So I'd like to know whats best practices to transport those resources.
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
          • 3749
          • 24,544 Posts
          This is how MyComponent does it. The content field is in a file called

          {pagetitle}.content.html


          in the _build/data/resources/ directory.





          $resources[1] = $modx->newObject('modResource');
          $resources[1]->fromArray(array (
            'id' => 1,
            'type' => 'document',
            'contentType' => 'text/html',
            'pagetitle' => 'Resource1',
            'longtitle' => '',
            'description' => '',
            'alias' => 'resource1',
            'link_attributes' => '',
            'published' => false,
            'isfolder' => true,
            'introtext' => NULL,
            'richtext' => false,
            'template' => 'default',
            'menuindex' => 0,
            'searchable' => true,
            'cacheable' => true,
            'createdby' => 0,
            'editedby' => 0,
            'deleted' => false,
            'deletedon' => 0,
            'deletedby' => 0,
            'menutitle' => '',
            'donthit' => false,
            'privateweb' => false,
            'privatemgr' => false,
            'content_dispo' => 0,
            'hidemenu' => false,
            'class_key' => 'modDocument',
            'context_key' => 'example',
            'content_type' => 1,
            'hide_children_in_tree' => 0,
            'show_in_tree' => 1,
            'properties' => NULL,
          ), '', true, true);
          $resources[1]->setContent(file_get_contents($sources['data'].'resources/resource1.content.html'));
          


          If you have a lot of resources, you could still use MyComponent to "export" the content field to those files automatically.
            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
            • 10378
            • 375 Posts
            That was exactly what I needed! Thanks Bob!

            And maybe someone could also help me with the second part of my question:

            How can I preselect a specific template for this resource which will be also installed with the same transport package?
            Is there a simple method of doing this or do I need to write a resolver which immediately reads the template id after it's installed?
              Freelancer @bitego http://www.bitego.com
              ---
              GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
              More infos here: http://www.bitego.com/extras/goodnews/
            • discuss.answer
              • 3749
              • 24,544 Posts
              Yes, you definitely need to write a resolver. MyComponent would do this automatically for you.

              Here's the resource resolver MyComponent creates (it also sets the parent and any TV values):

              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, '[Resource 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' => 'Resource1',
                                'parent' => 0,
                                'template' => 'default',
                              ),
                              1 =>  array (
                                'pagetitle' => 'Resource2',
                                'parent' => 'Resource1',
                                'template' => 'Template2',
                                'tvValues' =>  array (
                                  'Tv1' => 'SomeValue',
                                  'Tv2' => 'SomeOtherValue',
                                ),
                              ),
                          );
              
                          if (is_array($intersects)) {
                              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;
                                  }
                                  if ($fields['template'] == 'default') {
                                      $resource->set('template', $modx->getOption('default_template'));
                                  } else {
                                      $templateObj = $modx->getObject('modTemplate', array('templatename' => $fields['template']));
                                      if ($templateObj) {
                                          $resource->set('template', $templateObj->get('id'));
                                      } else {
                                          $modx->log(MODX::LOG_LEVEL_ERROR, '[Resource Resolver] Could not find template: ' . $fields['template']);
                                      }
                                  }
                                  if (!empty($fields['parent'])) {
                                      if ($fields['parent'] != 'default') {
                                          $parentObj = $modx->getObject('modResource', array('pagetitle' => $fields['parent']));
                                          if ($parentObj) {
                                              $resource->set('parent', $parentObj->get('id'));
                                          } else {
                                              $modx->log(MODX::LOG_LEVEL_ERROR, '[Resource Resolver] Could not find parent: ' . $fields['parent']);
                                          }
                                      }
                                  }
              
                                  if (isset($fields['tvValues'])) {
                                      foreach($fields['tvValues'] as $tvName => $value) {
                                          $resource->setTVValue($tvName, $value);
                                      }
              
                                  }
                                  $resource->save();
                              }
              
                          }
                          break;
              
                      case xPDOTransport::ACTION_UNINSTALL:
                          break;
                  }
              }
              
              return true;
                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
                • 10378
                • 375 Posts
                Thank you Bob! I will investigate how to use this code in my build script!
                  Freelancer @bitego http://www.bitego.com
                  ---
                  GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                  More infos here: http://www.bitego.com/extras/goodnews/
                  • 38878
                  • 255 Posts

                  Hey Bob,

                  First time using myComponent. Thanks much. I am trying to figure out how to set some TV Values on the new resources to ID of other newly created resources (i am assuming in the array to set TV values when creating the resource). According to your code above, somewhere in here:
                  if (isset($fields['tvValues'])) {
                       foreach($fields['tvValues'] as $tvName => $value) {
                           $resource->setTVValue($tvName, $value);
                       }
                  }


                  Assuming the value is the pagetitle passed from the config script in the tvValues array, that's what I need to resolve. I could do something like:
                  if (isset($fields['tvValues'])) {
                       foreach($fields['tvValues'] as $tvName => $value) {
                           $get_resource = $modx->getObject('modResource', array('pagetitle' => $value));
                           $get_id->get($get_resource=>'id');
                           $resource->setTVValue($tvName, $get_id);
                       }
                  }


                  Do I need such a custom resolver? I'd rather not hack myComponent. If not how do I reflect his in the build script to make it happen using the myComponent resolver as is?

                  TIA;)
                    • 3749
                    • 24,544 Posts
                    If you look at the example.config.php file (it should be in core/components/mycomponent/_build/config), there's an example in the Resources section showing a really easy way to set TV values. I believe those values are only set in MODX when you run Bootstrap, so you might have to also set the values for specific resources manually in the Manager. IIRC, the values in the config file are used in the project even if you change their values in the Manager.

                    Once you've done that, the values of those TVs should be set automatically for resources. It's done in the resource resolver as long as the TV values are in the project config file and the TVs themselves are also listed in the TV section of the config file.

                    I never have to touch the build.transport.php file unless I'm doing something really exotic (like building MyComponent itself).

                    You can do just about anything in the default resolver, since it runs after all the objects are installed, but in this case you shouldn't have to.
                      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