We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52804
    • 36 Posts
    I am working with lexicons in PHP outside of Modx and everything is working fine but for some reason I can't access the functions defined in the lexicon handling class /model/modx/modlexicon.class.php. In particular I am trying to find if a specific lexicon key exists using the exists() function but just get the error 'Call to undefined function exists()...'

    I have checked that the class file exists and the exists() function is present. The code in the class is...

    
     public function exists($index) {
            return (is_string($index) && isset($this->_lexicon[$index]));
        }
    
    


    My code is...

    require_once $_SERVER['DOCUMENT_ROOT'].'/config.core.php';
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    
    $modx = new modX();
    $modx->initialize('web');
    $modx->getService('error','error.modError', '', '');
          
    $modx->getService('lexicon','modLexicon');
    $modx->setOption('cultureKey', 'en');
    $modx->lexicon->load('datasheets:default');
    
    //if(exists('contact')) {    	
     $result=$modx->lexicon('contact');    
    //}
    


    Everything works as expected except if I uncomment the if clause i get 'Call to undefined function exists().. etc.'

    Am I missing something?

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      you would need
      $modx->lexicon->exists('contact');
      


      but
      $modx->lexicon('contact')
      checks allready, if the key exists.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 52804
        • 36 Posts
        Thank you very much Bruno. This works perfectly...

        $modx->lexicon->exists('contact');
        

        however I found if the key doesn't exist, this returns the string 'contact' instead of boolean false...

        $modx->lexicon('contact')
        

        • discuss.answer
          • 3749
          • 24,544 Posts
          You just need to test the return value against the key. If it matches, the key doesn't exist:

          /* Returns lexicon string, or false if key doesn't exist */
          function getLexiconString($key) {
              $result = $modx->lexicon($key);
              return ($result === $key)? false : $result;
          }
            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