PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Helper functions?

  1. dnb
    Member

    Hi-
    Before I go off and start writing my own small library of helper functions, I thought I would ask first if these already exist. Let me give you an example or two I'm about to write just to see if I'm heading down the wrong path. For these examples the context is I'm writing some custom code that will get used with the gform_after_submission() hook.

    Example 1: My code needs to iterate over the responses to a checkbox field and do something to each option that has been checked off. gform_after_submission() gets handed an entry object. As far as I can tell, there's no predefined way to say "Given this checkbox field, hand me back an array with the items that have been checked off." I've looked at the GF code itself (specifically at the code that makes replaces the {all_fields} merge field with a spiffy HTML table), but nothing in the code jumped out as a function the GF developers have intentionally exposed to the world for doing something like this.

    Being able to get the responses back from a checklist field seems really handy (especially since the entry object stores the responses in a flat data structure. Getting all of the entries associated with a single checkbox field seems to require actually string matching against key names, which seems a bit sub-optimal to me).

    Example 2: the current method of retrieving values back from a form seems a bit fragile to me because one has to use essentially indices (i.e. form IDs), instead of names. This means we work with:

    $entry['1.1'] instead of $entry['first_name']

    I suspect GF tries not to ever reuse IDs if it can help it, but I'm not clear if it will do so if you go back and re-edit a form or perhaps move the form and edit it. In any case, I know which of the two forms I'd prefer to read in my code.

    Given that you can get the field name -> ID mapping, it seems like writing a function that retrieves the value of a field from the $entry object should be relatively easy to write. It would be swell to say get_field_value('first_name'). I realize this might break if you have two fields with the same name, but then perhaps one could use an optional second parameter to disambiguate.

    Neither of these functions seem really hard (even to a PHP dabbler like myself), but it would be spiffy if either they already shipped in the product or were documented some place in a "helpful utility functions" part of the doc. Do they and other helper functions exist? (or should I punt this to "Feature Requests")

    Thanks!
    -- dNb

    P.S. On the plus side, the GF code itself is nicely structured and a pleasure to read.

    Posted 12 years ago on Wednesday December 14, 2011 | Permalink
  2. Hi dnb,

    Sorry I missed this. There are no helper function library that I know of. One thing that you might find helpful in general is using some of Gravity Forms own public functions to accomplish some tasks. For example,

    [php]
    GFCommon::get_email_fields($form);

    Would allow you to retrieve all of the email fields from any form object you pass to it. There are a ton of such functions through out Gravity Forms.

    Posted 12 years ago on Friday January 6, 2012 | Permalink
  3. dnb
    Member

    Hi David-
    Thanks for your reply. I had seen the public functions, but wasn't sure if I should consider them internal to the package or something people were encouraged to use knowing they were not going to change as the developers evolved the product. It's cool that the functions exist in the GF source, but I wouldn't want to use them unless they were blessed/stable by the developers for everyone's use (ideally also documented some place other than the code). Having them be scattered throughout the GF code also made me think they weren't really for public use.

    I don't recall seeing a GFCommon:: function specifically for grabbing the values back from a checkbox field. (there's a more general function internal to the package, but it is trying to handle a number of cases at once). Do you know of any I should be using?

    Since there isn't a helper library available, I wonder if it would be a useful project for the community to help create (stick it on github, etc.)? I wound up writing my own small functions for the project I was working on, but I know my PHP coding skills don't match those in the GF-orbit.

    Posted 12 years ago on Saturday January 7, 2012 | Permalink
  4. Hi Dnb,

    We encourage all users to share anything they feel would benefit the community. We wouldn't be able to launch such a project ourselves currently; however, if you'd like to set something up, I'm sure there are other users who would jump on board.

    Feel free to use an public methods in Gravity Forms. There are not currently any plans to document all of these; however, don't let that stop you from using them.

    I don't recall seeing a GFCommon:: function specifically for grabbing the values back from a checkbox field. (there's a more general function internal to the package, but it is trying to handle a number of cases at once). Do you know of any I should be using?

    There is no function that retrieves just the checkbox field values.

    Posted 12 years ago on Wednesday February 1, 2012 | Permalink