We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36769
    • 3 Posts
    Hello,

    I am trying to automatically assign a TV value to use as a class on my body tag in the rendered html page. The css class would change depending on the parent resource. I can't use @INHERIT because the value of the parent needs to be different from the child. I have set a TV called bodyclass in my templates like this:

    <body class="[[*bodyclass]]">


    bodyclass is accessible to all templates. I then have a second TV called setChildbodyclass. I need the value of bodyclass in all child resources to be set to the value of setChildbodyclass from the parent.

    The following code works fine as a snippet:

    <?php
    if ($modx->resource->get('parent')) {
    
      $tv = $modx->getObject('modTemplateVar', array('name' =>'setChildbodyclass'));
      $setTV = $tv->getValue($modx->resource->get('parent'));
      $modx->resource->setTVValue('bodyclass', $setTV);
    
    }


    I need to set the value of setChildbodyclass once on the parent folders and then when I pass the site off to the client have bodyclass set automatically when they create child resources. Having the code in a snippet won't work, it needs to be in a plugin so that the client won't need to do anything consciously to set the value of bodyclass. If I paste that code into a plugin and set the event listener to OnDocFormSave the manager just hangs when I save the child resource.

    My setup:
    Revolution 2.1.3-pl (traditional)
    on a local system using MAMP

    I've tried clearing the cache. I've tried manually deleting the files under the /core/cache directory

    Also, when I check the error.log I get the following:

    [2011-11-08 12:25:03] (ERROR @ /connectors/resource/index.php) Error caching lexicon topic lexicon/en/core/resource
    [2011-11-08 12:41:41] (ERROR @ /connectors/element/index.php) Error caching lexicon topic lexicon/en/core/default
    [2011-11-08 13:06:21] (ERROR @ /connectors/element/propertyset.php) Error caching action map mgr/actions
    [2011-11-08 13:08:48] (ERROR @ /connectors/element/propertyset.php) Error caching lexicon topic lexicon/en/core/default
    [2011-11-08 13:08:48] (ERROR @ /connectors/element/propertyset.php) Error caching action map mgr/actions
    [2011-11-08 13:13:55] (ERROR @ /connectors/element/propertyset.php) Error caching lexicon topic lexicon/en/core/default
    [2011-11-08 13:23:04] (ERROR @ /connectors/element/propertyset.php) Error caching lexicon topic lexicon/en/core/default
    [2011-11-08 13:42:16] (ERROR @ /connectors/element/index.php) Error caching lexicon topic lexicon/en/core/element


    Any help would be much appreciated.

    Thanks,

    Adam

    This question has been answered by [email protected]. See the first response.

    [ed. note: [email protected] last edited this post 12 years, 6 months ago.]
    • discuss.answer
      • 36769
      • 3 Posts
      Ok I think I've fixed it. Here's the code I ended up with in my plugin:

      //test if this is a child of a container
      if ($resource->getOne('Parent')) {
      
      //create a parent object to retrieve the value of the TV we want (setChildbodyclass)
        $parentObj= $resource->getOne('Parent');
      
      //retrieve the value
        $parentSetChildVal= $parentObj->getTVValue('setChildbodyclass');
      
      //set the value of bodyclass in the current resource to the value of setChildbodyclass in parent
        $resource->setTVValue('bodyclass', $parentSetChildVal);
      
      }


      This is pasted in a plugin that is called on the OnDocFormSave event in the System Events tab for the plugin.

      I'm not sure why the code that worked in the snippet wouldn't work in the plugin. Perhaps someone with more expertise would be able to elaborate?

      Either way, I hope this helps someone out in the future.

      Adam