accessing Field X value, from Field Y

accessing Field X value, from Field Y

by Justin Hunt -
Number of replies: 4
Picture of Particularly helpful Moodlers Picture of Plugin developers

This is a tricky question, and I think only Itamar can answer it.

In my dataform, I need to access the value of one field from another field.

Basically on the submission of the entry, the "shorturl" field needs to know the value of another field, "longurl" and then do some processing on it, and store it elsewhere.

The name of the other field can change.  The relevant code is below.

$cd->target contains the name of the other field, which we are after. We get the dataform, entry and field objects fine. But the value of the field is nowhere and the $entry object just contains ids and no data. $so ultimately $longurl comes back as empty. I am sure this should be possible but I don't know how to do it. 

 

//Get the dataform, entrymanager and entry
$df = \mod_dataform_dataform::instance($cd->dataformid, null, true);

$entryman = \mod_dataform_entry_manager::instance($cd->dataformid,0);

$entry = $entryman->get_entry_by_id($cd->entryid);


// Get the field name.

list($fieldname) = explode(':', trim($cd->target, '#[]'));

//Try to get the field value into $longurl variable

$options=array();

if ($field = $df->field_manager->get_field_by_name($fieldname)) {

  if ($replacements = $field->renderer->get_replacements(array($cd->target), $entry, $options)) {
  $replacement = reset($replacements);

  $longurl =$replacement;

}}


Average of ratings: -
In reply to Justin Hunt

Re: accessing Field X value, from Field Y

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

Never mind. I figured it out.

I was running that code from an ad-hoc task so probably because there was no user logged in, and I was not being returned any content.

I just used the code from entry_manager.php as a reference and added the content data to the entry object. 

If anyone needs to do something like this, the code ended up like this, I attached the code

In reply to Justin Hunt

Re: accessing Field X value, from Field Y

by Itamar Tzadok -

First, happy to see that you figure it out. I take it to mean that the Dataform code is at least somewhat intelligible. Second, I don't know what exactly you are trying to achieve there so just wondering whether there is a real need for two field types. If short-url and long-url can be seen as two presentations of one special-url you can manage these presentations in the same type by way of patterns. This can make the content manipulation much easier. Also, I would probably use observers for such manipulations. They can listen to the field update event. smile

In reply to Itamar Tzadok

Re: accessing Field X value, from Field Y

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

Thanks for the reply. 

Basically I am creating an activity where the user can record an audio or video file, or upload one. The url for either the file itself, or a view containing it, is sent to a url shortening service on the same server. I call that the target url. 

The short url is immutable. So updates to the original recording, or changing the view, will not change the short url. The target url however is bound to change several times.

I implemented it by creating a custom field called "shorturl." When the entry is first saved the shorturl service is called and returns a new shorturl. After that, and on each update,  an ad-hoc task is created and the short url service is notified of the new target url.

I had not considered using observers. That might have been a faster way to achieve this.  Next time I will seek your advice first. wink 

Thanks for the cool mod. Its really awesome.  

 

 

In reply to Justin Hunt

Re: accessing Field X value, from Field Y

by Itamar Tzadok -

You're welcome. smile

I think that you can safely implement this in one field type. Insofar as it's doable it would be more cost effective to maintain one plugin instead of two.

Consider the standard dataformfield_url. It has two content parts, the url and alternate name. For your purpose the url content part can store the long dynamic url and the alternate name content part can store the short immutable url. After all the short url is an alternate name of sorts. In editing mode you display the long url content for editing and in browse mode you display the short url content. You don't even need two patterns for that. It is just the implementation of the display_edit and display_browse methods. On first update of the entry content you fetch and store the short url.

hth smile