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

    in the documentation it is said that "@ bindings will work only when used inside 'Input Option Values' or 'Default Value' fields". Well, I tried a very simple thing and put

    @EVAL return $modx->resource->get('id');

    into both fields (I chose radio buttons as input type). The radio button is shown with the correct value, but it isn't selected. When I try to display the value using

    [[!getResourceField? &id=`24` &field=`JobId` &isTV=`1` processTV=`1` &default=`no value`]]

    I consequently get "no value" as result.

    Is there a way to set the value of the TV dynamically to a value that has been calculated from the Resource ID?

    The background to this is that I use an additional context for administering job offers. Each job offer has its own resource; the data is entered into TVs. In the frontend, the data is read via getResources etc. (at the moment; I'll write some specific snippets later). Now I need an ID for every job offer, which I want to calculate from the Resource ID (a very easy example would be to add 100.000 to the ID). The Job ID should be shown in the TV tab of the Resource (a single radio button would be okay) and be used in the frontend.

    I could of course just show the calculated Job ID in the Manager and use the Resource ID in the frontend, calculating the Job ID again, but I'd rather set the Job ID correctly when creating the new job offer Resource.

    Maybe I'm just too tired to see the obvious solution, but I'm losing too much time with this, so I'd be very grateful for any helpful hints.

    Thanks in advance!

    Cheers,
    Jan
      This message has been ROT-13 encrypted twice for higher security.
      • 22427
      • 793 Posts
      You could make a Custom output modifier calculate the Job ID from the Resource ID.
      Create a snippet "calc":
      <?php
      return $input + 100000; // or any other math
      Then for the TV which holds the Job ID set the Default value to [[*id:calc]]. So the modifier takes the value [[*id]], calculates the Job ID and outputs the result.
        • 22427
        • 793 Posts
        No, that wouldn't work. The MODX tag [[*id:calc]] will not be parsed within the Default value, not even with a @EVAL or @CODE binding.

        But a little Plugin solves the problem. (You do not need the modifier calc anymore.)

        Let's assume your TV is named job_tv and of type Text. (You can let the Default value empty.)

        Create a new plugin named set_job_tv with this Plugin code:
        <?php
        $resource =& $modx->resource;
        $id = $resource->get('id');
        $value = $id + 100000;        // or some other calculation
        $tv = $modx->getObject('modTemplateVar',array('name'=>'job_tv'));
        $tv->setValue($id,$value);
        $tv->save();

        Go to the tab System Events and tick the checkbox at OnWebPagePrerender (the last one in the list).
        Save the plugin.

        For testing, put something like this into the page template:
        [[*id]]---[[!*job_tv]]
        The TV tag has to be uncached.

        Now on the page with ID 42 you will get this output:
        42---100042
          • 8898
          • 181 Posts
          Hi ottogal,

          thank you very much for your reply. I'm already using several plugins, but somehow here it didn't come to my mind. After sleeping for ten hours, the world looks much better, and everything is easier... wink


          I'll try it the way you suggested, and I'm sure it'll work. Nevertheless I'm a little bit irritated that the documentation says that @ bindings are possible in the default values - I don't see how this should work...

          Well, maybe I'll take a look at this later. For the moment I'm lucky with your solution.

          Thanks again! smiley


          Cheers,
          Jan
            This message has been ROT-13 encrypted twice for higher security.
            • 8898
            • 181 Posts
            Okay, I quickly implemented this. Since I wanted to set the TV value in the Manager, I used OnDocFormPrerender for this:

            <?php
                /**
                 * OnDocFormPrerender event parameters:
                 * $mode     - Either 'new' or 'upd', depending on the circumstance.
                 * $resource - A reference to the modResource object. Will be null for new Resources.
                 * $id       - The ID of the Resource. Will be 0 for new Resources.
                 */
            
                if ('upd' == $mode) {
                    $jobId = $id + 100000;
            
                    $tv = $modx->getObject('modTemplateVar', array('name' => 'JobId'));
                    $tv->setValue($id, $jobId);
                    $tv->save();
                }

            This works fine. smiley


            The value is set when "updating" (or displaying) the Resource; the Job ID TV is hidden via Form Customization when the Resource is created because the Resource ID is 0 then.

            Thanks again!

            Cheers,
            Jan
              This message has been ROT-13 encrypted twice for higher security.
              • 22427
              • 793 Posts
              Two things I don't understand:

              (1)
              I wanted to set the TV value in the Manager
              You want the TV value calculated dependent on the resource ID - so you won't set it by yourself - ??

              (2)
              In your first posting you wrote:
              I chose radio buttons as input type
              Why that? Radio buttons are meant to have a choice from different options. (You would need Input Option Values like "foo||bar".) But you have a unique value for the TV.
                • 8898
                • 181 Posts
                The Job ID just has to be unique, and it has to be displayed in the TV tab; there shouldn't be a way to modify it. A single radio button option that has already been selected (which is what I wanted to achieve) can't be modified (well, at least if you're playing by the rules wink), so it's the best way to do this. I could have chosen a listbox with a single option, but that wouldn't have changed anything, technically.

                There's possibly a way to display this without using TVs by modifying the Manager templates, but unfortunately I don't have much time for this project, so I decided to do this the easy way. Maybe this can be changed later.

                Cheers,
                Jan [ed. note: enigmatic_user last edited this post 11 years, 6 months ago.]
                  This message has been ROT-13 encrypted twice for higher security.
                  • 22427
                  • 793 Posts
                  Why not just take the Input type Text?
                  Seeing a single Radio button (or a listbox with a single option) can be rather irritating for the back end user.
                    • 22427
                    • 793 Posts
                    I just saw that my plugin code above has to be hardened for the case you create a new resource.
                    Line 3
                    $id = $resource->get('id');
                    would throw a Fatal error "Call to a member function get() on a non-object" because the id of the new resource the object $resource is not defined for the new resource unless it is saved for the first time.
                    So there is the need to treat this case seperately. I'll try that as soon as I find the time to do so. [ed. note: ottogal last edited this post 11 years, 6 months ago.]
                      • 8898
                      • 181 Posts
                      Well, because a text can be changed by the user, which is not an option. The users will be instructed; also there's an explanatory text.

                      I know that a single radio button is not exactly semantic wink, but I guess I don't have many options here. As I said, when the project is online, maybe this can be changed, but for now I'll leave it at that.

                      Cheers,
                      Jan
                        This message has been ROT-13 encrypted twice for higher security.