Search code examples
modxmodx-revolution

Modx TV multi select list not saving values


I have a TV multi select list type that is evaluating a snippet:

@EVAL return $modx->runSnippet('getFeaturedResourceTree');

Evaluating this snippett:

<?php
$output = array();

$context = $modx->resource->get('context_key'); 

$sql = "select * from modx_site_content where context_key = '$context' order by `pagetitle`;";

$results = $modx->query($sql);

foreach($results as $result){

    $output[] = $result['pagetitle'].'=='.$result['id'];

}

$output = implode('||', $output);

echo  $output;

return;

This does work in the manager, I can select and pick multiple resources in the list. However, when I save the TV, nothing is actuially saved. the TV values are not present in the database and when I reload the resource, the TV field is blank.

what could the problem be here?


Solution

  • I'm fairly certain you can accomplish what you're trying to do with an @SELECT binding rather than @EVAL. This has 2 potential benefits:

    1. @EVAL is Evil, LOL. Not all the time, mind you—there are certainly legitimate uses of @EVAL but I've personally always tried very hard to find an alternative, whenever I've considered using @EVAL.
    2. The method I'm about to show you has worked for me in the past, so I'm speculating it will work for you.

      @SELECT pagetitle, id FROM modx_site_content WHERE context_key = 'web' ORDER BY `pagetitle`

    If you're using @EVAL because you have multiple contexts and you want the context of the Resource currently being edited, then you could use your Snippet, but I would try:

    1. Rather than echo-ing your output, return it.
    2. Call the snippet in a Chunk, and render the Chunk on a test page to ensure it has the output you want, formatted for the TV Input Options exactly the way it should be.
    3. If the Chunk output passes the test, call it into the TV Input Options field with the @CHUNK binding.

    One more note: I can't remember if the current Resource is available in the TV as $modx->resource or $resource, but that might be something you want to double check.