We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44987
    • 14 Posts
    Hi anyone! I try to create self modifier using snippet.
    $options = explode(",",$options);
    if (in_array($input, $options)) 
        return true; 
    else
        return false;
    

    but when I try to use this modifier
    [[*id:inarray=`2,30,40,80`:then=`id in array!`:else=`id is not in array!`]]
    

    it is not working...

    I fix this problem:
    if (in_array($input, $options)) 
        return 'yes'; 
    else
        return 'no';
    [[*id:inarray=`2,30,40,80`:is=`yes`:then=`id in array!`:else=`id is not in array!`]]
    

    but I think it's wrong solution.

    Can someone help me? I wanna use my modifier without additional :is modifier.
    • Instead of returning a boolean true or false, try returning a numeric 1 or 0 - that should do the trick..
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 44987
        • 14 Posts
        Quote from: markh at Oct 01, 2013, 03:26 PM
        Instead of returning a boolean true or false, try returning a numeric 1 or 0 - that should do the trick..
        tryed, don't worked sad I opened modoutputfilter.class.php placed here core/model/modx/filters. You was right about returning numeric 1 or 0 but it still don't work.
        $options = explode(",",$options);
        return intval(in_array($input, $options));
        

        Don't working too.
          • 3749
          • 24,544 Posts
          I'm not sure why that doesn't work, but why not just do all the work in a snippet. That will be a lot faster:


          [[!MyInArray? $docIds=`2,30,40,90`]]




          <?php
          /* MyInArray Snippet */
          
          $a = explode(',', $docIds);
          if (in_array($modx->resource->get('id'), $a)) {
             $output = 'something';
          } else {
             $output = 'something else';
          }
          
          return $output;


            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
            • 44987
            • 14 Posts
            Quote from: BobRay at Oct 01, 2013, 06:29 PM
            I'm not sure why that doesn't work, but why not just do all the work in a snippet. That will be a lot faster

            Yes we can do it in a snippet. But my purpose is to used like a modifier.. May be it is a bug?
            • Looks like you have to specify the output of the modifier with an :is= clause. The phx core doesn't appear to have a default true or false boolean operation.

              The snippet (true will output 1):
              <?php
              $docIds = explode(",",$options);
              $id = $modx->resource->get('id');
              if(in_array($id, $docIds)) return true;
              return false;

              The tag:
              [[*id:inarray=`2,30,40,80`:is=`1`:then=`id in array!`:else=`id is not in array!`]]
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 44987
                • 14 Posts
                I solved this problem!
                Any returned value should be array! example:
                // snipped name inarray
                $options = explode(",",$options);
                return array ( [0] => intval(in_array($input,$options)));
                
                using [[*id:inarray=`2,8,100,68,...`:then=`code`:else=`code`]]
                

                MODX is a greatest revolution engine! smiley
                • How interesting! That is certainly good to know!

                  <after testing>
                  Hmm... are you sure? It's still not working for me. Always shows not in array. [ed. note: sottwell last edited this post 10 years, 7 months ago.]
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 44987
                    • 14 Posts
                    Should work. I opened modoutputfilter.class.php placed here core/model/modx/filters and insered var_dump($condition) below 173 line:
                     case 'then':
                       var_dump($condition);
                       $output = null;
                         $conditional = join(' ', $condition);
                         try {
                            $m_con = @eval("return (" . $conditional . ");");
                            $m_con = intval($m_con);
                            if ($m_con) {
                                $output= $m_val;
                            }
                        } catch (Exception $e) {}
                        break;
                    

                    and saw how this work, then modifier receives array([0] => 1 or 0), tried to return array([0] => intval(bool)) and my modifier earned! that's all! smiley
                      • 43132
                      • 1 Posts
                      Hmm... but it still not working (MODX Revolution 2.2.14-pl (traditional))
                      // snippet name 'testSnippet'
                      .....
                      // for testing
                      return array(0=>1); // or $condition[]=1
                      

                      usage
                      [[+myString:testSnippet=`someValue`:then=`code1`:else=`code2`]]

                      always return 'code2'
                      case 'then':
                        var_dump($condition);
                        $output = null;
                          $conditional = join(' ', $condition);
                          try {
                             $m_con = @eval("return (" . $conditional . ");");
                             $m_con = intval($m_con);
                             if ($m_con) {
                                 $output= $m_val;
                             }
                         } catch (Exception $e) {}
                         break;

                      always display
                      array(0){}