modxcms / fred

The friendly front-end editor for visual, drag-and-drop content building in MODX CMS
https://fred.modx.com
MIT License
59 stars 25 forks source link

Proper use of default element? #410

Closed claytonk closed 3 years ago

claytonk commented 3 years ago

Trying out Fred for the redesign of an existing site and I’m not able to get my existing content to show up using Fred’s default element setting. The documentation shows the syntax for this setting as ID|target where the ID is the identifier of the Fred element to use and target is the HTML object within that element. It’s not clear in the documentation what reference we should use for the ‘target’, but I assume it's the value of the data-fred-name attribute, correct? I’ve also tried the HTML tag name and the id attribute, but neither helps.

My current default element setting is: 31|default

Element 31 is: <div id="default-content" class="text" contenteditable="true" data-fred-name="default" data-fred-rte="true" data-fred-rte-config="text"> default content </div>

I've removed all but one dropzone from my template to be sure multiple dropzones aren't causing the trouble.

What am I missing? Does this element need to be included in the template in some way (tried that already with no success)?

Any help would be greatly appreciated.

MODX 2.8.1 | FRED 1.2.3 | MODX Cloud hosting

matdave commented 3 years ago

The default element is triggered when you convert an existing page into a Fred page. Is that what you are doing?

claytonk commented 3 years ago

AH, I see it only works with a template change and a save action! Didn't realize that, I was testing on a page that had already had the template switched prior to the "default element" setting change. However, this brings up another issue. If I reset the template setting for all resources in the DB with SQL I'll have to trigger a save event on every resource manually or is there a way to trigger this action via scripting so I don't have to go through the whole site page by page?

matdave commented 3 years ago

So what I'd do is just create an iterator Snippet to do the bulk of the work and trigger the save processes, like:

$c = $modx->newQuery('modResource');
$c->where(array(//your criteria));

$collection = $modx->getCollection('modResource', $c);
foreach($collection as $resource){
  $resource->set('template', X); // ID of your fred template
  if(!$resource->save()){
    echo "Problem Saving Resource $resource->pagetitle ($resource->id) <br/>";
  }
}
matdave commented 3 years ago

Disclaimer, the script above is untested, just an example. I usually do these one by one because I end up having to touch most of the resources anyway. My process is usually:

  1. Create a non-Fred template to apply everything to
  2. Create a Fred version and change everything to it and add elements as needed
claytonk commented 3 years ago

Based on q quick test $resource->save() doesn't appear to trigger the necessary events for the default element to implement. I'm guessing that runs via plugin? This site contains many hundreds of pages, many of which are simple content that doesn't require my attention, so I really don't want to go through it all manually.

Looks like my best bet will be to use the existing content field to build the JSON Fred stores in the properties column and save that via an iterator. Regardless, you've answered my original question, so I'll close this. Thanks very much for your quick and helpful reply.

matdave commented 3 years ago

Don't want to leave you totally empty handed, I think you might just need to call Fred's render call.

$corePath = $modx->getOption('fred.core_path', null, $modx->getOption('core_path', null, MODX_CORE_PATH) . 'components/fred/');
/** @var Fred $fred */
$fred = $modx->getService(
    'fred',
    'Fred',
    $corePath . 'model/fred/',
    [
        'core_path' => $corePath
    ]
);
$c = $modx->newQuery('modResource');
$c->where(array(//your criteria));

$collection = $modx->getCollection('modResource', $c);
foreach($collection as $resource){
  $resource->set('template', X); // ID of your fred template
  $renderResource = new \Fred\RenderResource($resource, $modx);
  $renderResource->render();
  if(!$resource->save()){
    echo "Problem Saving Resource $resource->pagetitle ($resource->id) <br/>";
  }
}
claytonk commented 3 years ago

Thanks! That might just be the silver bullet. I’ll test that out later today and will report back on results.

claytonk commented 3 years ago

Tried this out last evening, worked like a charm. Many thanks!