We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6726
    • 7,075 Posts
    I am working on a multi-lingual website for which I have used Susan’s method, i.e duplicating the document tree and having languages has root folders.

    Doing so is alright for regular pages. My problem is, part of the content is a directory of companies containing many templates variables, which is accessible to the public in french or english. Some of those variables will be identical in french and english (Name, adress, website, telephone, fax, etc...). Since my client will use this directory as a database to store the companies info, he doesn’t want to have to do a double update for the common fields (lost time, source of error).

    Anyway, I had a few ideas to solve this, but I am not sure which way to go :

    1) Get the directory out of the french and english folders, and put it in a directory folder. Each document would be associated with both common, french and english template variables. I’d create two templates, one associated with the common + french TVs and one with the common + english TVs. Then I’d use the template switcher to display the appropriate template for the selected language (the language selecter would be a link with the &template=english and &template=french parameter). The downside is, I’d loose the ability to cache pages.

    2) Have the english document inherit the value of the french document template variables upon the french document edition. Not sure how that could be done, @INHERIT is not meant to do that (or is it ?). Maybe define the value with an @SELECT looking into the proper table fields ? Writing a plugin ?

    How would you go about this ?
    Any other idea ?

    Thanks a lot !
      .: COO - Commerce Guys - Community Driven Innovation :.


      MODx est l'outil id
      • 6726
      • 7,075 Posts
      Nobody has any idea about this ? ? ?

      Let me report on progress.

      I have dug a bit into the API and DB API, wondering if getTemplateVar, getTemplateVarOutPut or getTemplateVars could help me there.

      I started (probably very badly) writing things like :

      $tv = $modx -> getTemplateVar('raison_sociale', 'value', '97', 1);
      return $tv;


      The goal (which might not be obvious) is to retrieve the value of the template variable [*raison_sociale*] from the document whose id is 97.

      I didn’t even dare try it yet (I don’t write php)...

      So I searched for example use of getTemplateVar to make sure this would have any chance to work, and found the GetField snippet (among others). I just had missed that GetField could do exactly what I needed (dumb me for not think of it)...

      Problem is, I tried it and though it works as expected with a single call, multiple calls of GetField on the same page gives me a blank page sad

      So I am back to square one...

      Any input, idea, advice to give me here ?
      Thanks !


      Edit : OK, I tried my poor attempt at using the API and here is what I got :

      SQL: SELECT tv.value, IF(tvc.value!='',tvc.value,tv.default_text) as value FROM `gican`.modx_site_tmplvars tv INNER JOIN `gican`.modx_site_tmplvar_templates tvtpl ON tvtpl.tmplvarid = tv.id LEFT JOIN `gican`.modx_site_tmplvar_contentvalues tvc ON tvc.tmplvarid=tv.id AND tvc.contentid = '97' WHERE tv.name IN ('raison_sociale') AND tvtpl.templateid = 13  
      


      :-[

      I guess I’ll wait till my one week php training due in early december is done and I hopefully get a better understanding
        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l'outil id
        • 22815
        • 1,097 Posts
        Quote from: davidm at Oct 20, 2006, 07:11 AM
        multiple calls of GetField on the same page gives me a blank page

        As I use GetField on a site to simulate a QuickEdit-able chunk, I read the bit before that thinking "Use GetField!".

        As I *am* a PHP programmer, I will now look at GetField and find out what it is doing that falls over on a second usage. I’ve never actually used it twice on one page. I suspect it’s something very simple.

        Although arguably mis-titled, http://wiki.modxcms.com/index.php/Creating_Snippets is a great guide to some PHP best practice, and my first suspicion is that GetField has broken one of the guidelines.
          No, I don't know what OpenGeek's saying half the time either.
          MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
          Forum: Where to post threads about add-ons | Forum Rules
          Like MODx? donate (and/or share your resources)
          Like me? See my Amazon wishlist
          MODx "Most Promising CMS" - so appropriate!
          • 6726
          • 7,075 Posts
          Thanks a lot Paul, I was starting to feel a little lonely with my question there undecided

          I have of course reported the multiple call problem on the GetField support thread.

          Hopefully, my upcoming training and undertaking of finally getting down to learning php will make me able to help on that kind of problem in the future...
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l'outil id
            • 11975
            • 2,542 Posts
            Hi david,

            Coud you try with the following code

            <?php //remove this line
            
            /*
            ==================================================
            	getField
            ==================================================
            
            Gets a field or template variable for current or
            specified document or its parent up to specified
            number of levels until ultimate parent is reached.
            
            Author: Grzegorz Adamiak [grad]
            Version: 1.2.1 @2006-07-19 08:37:53
            License: LGPL
            MODx: 0.9.2.1
            
            Uses idea of getDoc by Mark with modification from
            luke.stokes
            
            History:
            # 1.2.1
            - reworked fetching of template variable (again ;)
              to enable output of widget
            # 1.2
            - reworked fetching of template variable value to
              get INHERITED value
            - removed defaulting to 'pagetitle' when $field
              not found; this is due to fact that tv can have
              empty value
            # 1.1.1
            - fixed small bug with $parentLevel and $topid
              check; now should work as expected
            # 1.1
            - reworked fetching of template variable value,
              now it gets computed value instead of nominal;
              however, still not the inherited value
            - changed license to LGPL
            # 1.0
            - first public release
            --------------------------------------------------
            */
            
            /* Parameters
            
            [!getField? &parent=`1` &field=`menutitle`!]
            [!getField? &parent=`1` &parent=`` &field=`longtitle`!]
            
            ----------------------------------------------- */
            
            # $docid [ int ]
            # Document for which to get a field.
            # Default: current document
            
            $id = (isset($docid)) ? $docid : $modx->documentIdentifier;
            
            # $field [ string ]
            # Field to get for the document:
            # - any of the document object fields
            #   (http://modxcms.com/the-document-object.html)
            # - template variable assigned to the document
            # Default: 'pagetitle'
            
            $field = (isset($field)) ? trim($field) : 'pagetitle';
            
            # $parent [ 0 | 1 ]
            # $parent = 1 - returns the field for the parent
            # of the document.
            # Default: 0
            
            $parent = (isset($parent)) ? $parent : 0;
            
            # $parentLevel [ int ]
            # How high in the document tree to search for the
            # parent of the document?
            # - $parentLevel = 0 - returns the ultimate parent
            #   (right under site root)
            # - $parentLevel = 1 - returns the direct parent
            # Default: 0
            
            $parentLevel = (isset($parentLevel) && is_int((int) $parentLevel)) ? $parentLevel : 0;
            
            # $topid [ int ]
            # Id of the topmost document in the document tree
            # under which to search for a parent.
            # Default: 0
            
            $topid = (isset($topid) && is_int((int) $topid)) ? $topid : 0;
            
            /* End parameters
            ----------------------------------------------- */
            
            # search for parent document id
            # ---------------------------------------------
            if ($parent)
            {
            	# build an array of document's ancestors
            	$arrDocParents[] = $id; // add current document on first place
            
            	# get all parents back to root of document tree
            	while (($pid = $modx->getDocument($id,'parent')) && ($pid['parent'] != 0))
            	{
            		$id = $pid['parent']; // move up one document in document tree
            		$arrDocParents[] = $id; // add parent to array of document's ancestors
            	} // end while
            	$countParents = count($arrDocParents);
            
            	# determine the parent id
            	switch ($topid)
            	{
            		case 0: // not set or set to root of document tree
            
            			($parentLevel && ($parentLevel < $countParents)) ?
            				($docid = $arrDocParents[$parentLevel]) : // find parent in specified levels up
            				($docid = $arrDocParents[$countParents - 1]);
            
            			break;
            
            		default: // set to any other document
            
            			$key = array_search($topid, $arrDocParents); // find the index of parent
            			switch ($key)
            			{
            				case 0: // not an ancestor or the document itself
            
            					$docid = 0;
            					break;
            
            				default: // parent is above the document in document tree
            
            					($parentLevel && ($parentLevel < $key)) ?
            						($docid = $arrDocParents[$parentLevel]) : // find parent in specified levels up
            						($docid = $arrDocParents[$key - 1]);
            			} // end switch
            	} // end switch
            } // end if
            else
            {
            	$docid = $id;
            } // end else
            
            # get field or template variable for document
            # ---------------------------------------------
            // check for document field
            $docFields = $modx->getDocument($docid);
            $output = $docFields[$field];
            
            // check for template variable
            if (!$output)
            {
            	$output = getTV($modx,$docid,$field);
            }
            
            return $output;
            
            # functions
            # ---------------------------------------------
            
            # gets (inherited) value of template variable
            if(!function_exists('getTV')){
            function getTV($modx,$docid,$doctv)
            {
            /* apparently in 0.9.2.1 the getTemplateVarOutput function doesn't work as expected and doesn't return INHERITED value; this is probably to be fixed for next release; see http://modxcms.com/bugs/task/464
            	$output = $modx->getTemplateVarOutput($tv,$docid);
            	return $output[$tv];
            */
            	
            	while ($pid = $modx->getDocument($docid,'parent'))
            	{
            		$tv = $modx->getTemplateVar($doctv,'*',$docid);
            		if (($tv['value'] && substr($tv['value'],0,8) != '@INHERIT') or !$tv['value']) // tv default value is overriden (including empty)
            		{
            			//$output = $tv['value'];
            			$output = $modx->getTemplateVarOutput($doctv,$docid);
            			$output = $output[$doctv];
            			break;
            		}
            		else // there is no parent with default value overriden 
            		{
            			$output = trim(substr($tv['value'],8));
            		}
            		$docid = $pid['parent']; // move up one document in document tree
            	} // end while
            	
            	return $output;
            }
            }
            
            


            I’ve just added an if(function_exists()) and seems to work tested with 1745 (Two call in the same page but just tried with doc properies, I had no Tv to test with)

            :-)

            EDIT : I’ve forgotten !function_exists but fails

            sorry
              Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
              • 11975
              • 2,542 Posts
              Here is the working one.

              Tested with two Tvs

              /*
              ==================================================
              	getField
              ==================================================
              
              Gets a field or template variable for current or
              specified document or its parent up to specified
              number of levels until ultimate parent is reached.
              
              Author: Grzegorz Adamiak [grad]
              Version: 1.2.1 @2006-07-19 08:37:53
              License: LGPL
              MODx: 0.9.2.1
              
              Uses idea of getDoc by Mark with modification from
              luke.stokes
              
              History:
              # 1.2.1
              - reworked fetching of template variable (again ;)
                to enable output of widget
              # 1.2
              - reworked fetching of template variable value to
                get INHERITED value
              - removed defaulting to 'pagetitle' when $field
                not found; this is due to fact that tv can have
                empty value
              # 1.1.1
              - fixed small bug with $parentLevel and $topid
                check; now should work as expected
              # 1.1
              - reworked fetching of template variable value,
                now it gets computed value instead of nominal;
                however, still not the inherited value
              - changed license to LGPL
              # 1.0
              - first public release
              --------------------------------------------------
              */
              
              /* Parameters
              
              [!getField? &parent=`1` &field=`categorieNews`!]
              [!getField? &parent=`1` &field=`moi`!]
              
              ----------------------------------------------- */
              
              # $docid [ int ]
              # Document for which to get a field.
              # Default: current document
              
              $id = (isset($docid)) ? $docid : $modx->documentIdentifier;
              
              # $field [ string ]
              # Field to get for the document:
              # - any of the document object fields
              #   (http://modxcms.com/the-document-object.html)
              # - template variable assigned to the document
              # Default: 'pagetitle'
              
              $field = (isset($field)) ? trim($field) : 'pagetitle';
              
              # $parent [ 0 | 1 ]
              # $parent = 1 - returns the field for the parent
              # of the document.
              # Default: 0
              
              $parent = (isset($parent)) ? $parent : 0;
              
              # $parentLevel [ int ]
              # How high in the document tree to search for the
              # parent of the document?
              # - $parentLevel = 0 - returns the ultimate parent
              #   (right under site root)
              # - $parentLevel = 1 - returns the direct parent
              # Default: 0
              
              $parentLevel = (isset($parentLevel) && is_int((int) $parentLevel)) ? $parentLevel : 0;
              
              # $topid [ int ]
              # Id of the topmost document in the document tree
              # under which to search for a parent.
              # Default: 0
              
              $topid = (isset($topid) && is_int((int) $topid)) ? $topid : 0;
              
              /* End parameters
              ----------------------------------------------- */
              
              # search for parent document id
              # ---------------------------------------------
              if(!function_exists('getTV')){
                  function getTV($modx,$docid,$doctv)
                  {
              /* apparently in 0.9.2.1 the getTemplateVarOutput function doesn't work as expected and doesn't return INHERITED value; this is probably to be fixed for next release; see http://modxcms.com/bugs/task/464
              	$output = $modx->getTemplateVarOutput($tv,$docid);
              	return $output[$tv];
              */
              	
              	while ($pid = $modx->getDocument($docid,'parent'))
              	{
              		$tv = $modx->getTemplateVar($doctv,'*',$docid);
              		if (($tv['value'] && substr($tv['value'],0,8) != '@INHERIT') or !$tv['value']) // tv default value is overriden (including empty)
              		{
              			//$output = $tv['value'];
              			$output = $modx->getTemplateVarOutput($doctv,$docid);
              			$output = $output[$doctv];
              			break;
              		}
              		else // there is no parent with default value overriden 
              		{
              			$output = trim(substr($tv['value'],8));
              		}
              		$docid = $pid['parent']; // move up one document in document tree
              	} // end while
              	
              	return $output;
                  }
              }
              
              
              if ($parent)
              {
              	# build an array of document's ancestors
              	$arrDocParents[] = $id; // add current document on first place
              
              	# get all parents back to root of document tree
              	while (($pid = $modx->getDocument($id,'parent')) && ($pid['parent'] != 0))
              	{
              		$id = $pid['parent']; // move up one document in document tree
              		$arrDocParents[] = $id; // add parent to array of document's ancestors
              	} // end while
              	$countParents = count($arrDocParents);
              
              	# determine the parent id
              	switch ($topid)
              	{
              		case 0: // not set or set to root of document tree
              
              			($parentLevel && ($parentLevel < $countParents)) ?
              				($docid = $arrDocParents[$parentLevel]) : // find parent in specified levels up
              				($docid = $arrDocParents[$countParents - 1]);
              
              			break;
              
              		default: // set to any other document
              
              			$key = array_search($topid, $arrDocParents); // find the index of parent
              			switch ($key)
              			{
              				case 0: // not an ancestor or the document itself
              
              					$docid = 0;
              					break;
              
              				default: // parent is above the document in document tree
              
              					($parentLevel && ($parentLevel < $key)) ?
              						($docid = $arrDocParents[$parentLevel]) : // find parent in specified levels up
              						($docid = $arrDocParents[$key - 1]);
              			} // end switch
              	} // end switch
              } // end if
              else
              {
              	$docid = $id;
              } // end else
              
              # get field or template variable for document
              # ---------------------------------------------
              // check for document field
              $docFields = $modx->getDocument($docid);
              $output = $docFields[$field];
              
              // check for template variable
              if (!$output)
              {
              	$output = getTV($modx,$docid,$field);
              }
              
              return $output;
              
              # functions
              # ---------------------------------------------
              
              # gets (inherited) value of template variable
              
              
              
              
                Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                • 6726
                • 7,075 Posts
                Thanks Helio ! G-R-E-A-T grin

                I am going to try the latest code you posted, before that, just a question : did you intentionally remove the part after

                # gets (inherited) value of template variable ?

                or is it on purpose ?

                  .: COO - Commerce Guys - Community Driven Innovation :.


                  MODx est l&#39;outil id
                  • 11975
                  • 2,542 Posts
                  I haven’t touched this part of the code.
                  It was already commented out.

                  The only changes I made were to check if the function exists and move the code block to the beginning of the file.
                  Anything else.

                  :-)
                    Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                    • 22815
                    • 1,097 Posts
                    Well done heliotrope.

                    Hmmn, seems I was still on 1.1 (you’re on 1.2.1). There’s clearly more activity on this snippet than I thought!

                    Intriguingly, [!GetField!] [!GetField!] worked happily for me on my test box - presumably my setup allows functions to be redeclared. This also means I can’t test the above fixes easily (because I don’t have the problem in the first place!). I’ll have to watch out for that!
                      No, I don&#39;t know what OpenGeek&#39;s saying half the time either.
                      MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
                      Forum: Where to post threads about add-ons | Forum Rules
                      Like MODx? donate (and/or share your resources)
                      Like me? See my Amazon wishlist
                      MODx "Most Promising CMS" - so appropriate!
                      • 6726
                      • 7,075 Posts
                      The working one does miss those lines compared with the previous you posted :

                      # gets (inherited) value of template variable
                      if(!function_exists('getTV')){
                      function getTV($modx,$docid,$doctv)
                      {
                      /* apparently in 0.9.2.1 the getTemplateVarOutput function doesn't work as expected and doesn't return INHERITED value; this is probably to be fixed for next release; see http://modxcms.com/bugs/task/464
                      	$output = $modx->getTemplateVarOutput($tv,$docid);
                      	return $output[$tv];
                      */
                      	
                      	while ($pid = $modx->getDocument($docid,'parent'))
                      	{
                      		$tv = $modx->getTemplateVar($doctv,'*',$docid);
                      		if (($tv['value'] && substr($tv['value'],0,8) != '@INHERIT') or !$tv['value']) // tv default value is overriden (including empty)
                      		{
                      			//$output = $tv['value'];
                      			$output = $modx->getTemplateVarOutput($doctv,$docid);
                      			$output = $output[$doctv];
                      			break;
                      		}
                      		else // there is no parent with default value overriden 
                      		{
                      			$output = trim(substr($tv['value'],8));
                      		}
                      		$docid = $pid['parent']; // move up one document in document tree
                      	} // end while
                      	
                      	return $output;
                      }
                      }


                      You see what was the meaning of my question ? (I am just trying to understand...)


                      This being said, your latest code works like a charm with 15 tv calls grin
                      Thanks A LOT !
                        .: COO - Commerce Guys - Community Driven Innovation :.


                        MODx est l&#39;outil id