pepebe
4/10/2015 - 1:03 PM

Copy the content of a richtext TV into the content area every time a resource is saved.

Copy the content of a richtext TV into the content area every time a resource is saved.

<?php

/*
  CopyTVtoContent
  by info@pepebe.de
  
  What it does: Copy the content of a richtext TV into the content area every time
  a resource is saved.

  Why do you need it: Although keeping the content area below the tabs 
  is sometimes not a good choice there is no native solution to move this field around.
  
  For this reason plugins like Collections or MoreGallery 
  provide settings to move this field to another location.

  With this plugin you can use a Richtext TV as a container for your main content. 
  The plugin will copy the TV's content into the Content field 
  every time the resource is saved.
  
  Usage: 
  1. Create a new Richtext Tv (for example Main Content). Remember the Id of your new TV.
  2. Copy this code into a new plugin 
  3. Edit the ContentTV variable below
  3. Trigger the plugin OnDocFormSave. 
  4. Do some tests and hide your content field with form customization if you are satisfied.
  
  Variables:
  contentTV: int, the id of your Content TV
  
*/

  /* Edit this */
  $contentTV = ''; // Example: 29;

  /* */
  switch ($modx->event->name) {
  
    case 'OnDocFormSave':
      if($resource->getTVValue($contentTV)) {
          $content = $resource->getTVValue($contentTV);
          if(!empty($content)){
              $resource->set('content',$content);
              $resource->save();
          }
      }
  }
  
  return;