• Resolved tomwhita

    (@tomwhita)


    Well, I manage to go two hours without raising any issues. Here’s my latest question:

    I’ve created a gravity for to allow a user to post a new “Document” custom post type. The CPT has an association field called “doc_project_association” that ties a document to another custom post type “project”.

    I’ve tried using the Gravity forms custom field type to insert a value with the ID of the project but carbon fields does not store the value.

    You made a comment earlier about how the value is serialized. Can you please explain how I might go about creating a form to insert a new record?

    Thanks again. If there’s a way for me to contribute to your project, please let me know.

    TW

    https://www.ads-software.com/plugins/carbon-fields/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author htmlBurger

    (@htmlburger)

    Hey @tomwhita,

    The association field data is serialized like this:

    a:3:{i:0;s:12:"post:page:48";i:1;s:12:"post:page:10";i:2;s:11:"post:page:8";}

    which eventually unserializes to:

    (
        [0] => post:page:48
        [1] => post:page:10
        [2] => post:page:8
    )

    As listed in the field documentation, an association field can have more than one value, and each value can be a post type, taxonomy term, user or comment. In this example, there are 3 selected items, all of them are pages.

    Basically, each value is formed like this:

    {type}:{subtype}:{id}

    where:

    1. {type} is the data type – post, term, user or comment.
    2. {subtype} is the data subtype – which is the post_type if using a post, or the taxonomy if using a term, user for users and comment for comments.
    3. {id} is the ID of the post, term, user or comment.

    So, now to your case. You want to insert a valie with the ID of a project, it would consist of one selected item, a post with post type project. If the project has ID of 123, your entry would look like this:

    post:project:123

    and in order to insert it so it properly serializes, it has to be an array, like this:

    array( 'post:project:123' )

    Please, let us know if you have any other questions.

    Regarding contribution: any kind of contribution is welcome in the GitHub repos of the project: https://github.com/htmlburger/carbon-fields and the docs: https://github.com/htmlburger/carbon-fields-docs .

    In the project repo you could contribute by feature requests, bug fixes, code improvements, and even translations.

    In the docs repo you can always suggest ideas for improvement and for including undocumented features. The latest release of the documentation (corresponding to the latest release of Carbon Fields) is always automatically pushed on https://carbonfields.net/

    Have fun ??

    Thread Starter tomwhita

    (@tomwhita)

    Thanks. I’m having lots of fun with this project so far ??

    I have successfully managed to insert an association value using a gravity form but the association is not shown in the carbon fields UI in the admin. Do you know why this might happen?

    Also, how do I now retrieve data from an association field in a usable way? For example, I have associated users with the custom post type “Project”. How can I show the display name of the user on my page template?

    TW

    Plugin Author htmlBurger

    (@htmlburger)

    Howdy, @tomwhita,

    It appears to be an interesting project ?? Now to your questions:

    I have successfully managed to insert an association value using a gravity form but the association is not shown in the carbon fields UI in the admin. Do you know why this might happen?

    This would indicate that the data is not added correctly, and probably isn’t in the expected format. You should refer to our above explanation about how the association field data should be stored in the database.

    Also, how do I now retrieve data from an association field in a usable way? For example, I have associated users with the custom post type “Project”. How can I show the display name of the user on my page template?

    Let’s say the association field name where you associated users is project_users. Let’s also consider that your project ID is $project_id. In that case, you should use carbon_get_post_meta( $project_id, 'project_users', 'association' ) – this will return an array with the IDs of all selected users. Then you can use the user functions (like get_user_by() to retrieve the user object and information about it.

    Thread Starter tomwhita

    (@tomwhita)

    I really appreciate your patience with me. I’m trying to push my limits as a developer a little…

    The way I’ve been able to create an association when submitting a form is by sending the unserialize value. Like this:

    '"post:project:' . $post->ID . '"' But like I said, the association doesn’t show up in the Admin panel. Though i’m able to see associated data on my page template if I pull it using:

    $args = array(
                        'post_type' => array('document'),
                        'post_status' => array('publish'),
                        'meta_query' => array(
                                  array(
                                           'key' => '_doc_project_association',
                                           'compare' => 'LIKE',
                                           'value' => '"post:project:'.$post->ID.'"',
                                                                 ),
                                                             ),
                                        );
                      $docs = get_posts($args);

    When I try to use:

    array( 'post:project:$post->ID' ) to set the field variable, it does not work.

    What am I doing wrong?

    Also, can you go one step further in the get_user_by() reference. For example, would it be get_user_by(project_users->ID)?

    Plugin Author htmlBurger

    (@htmlburger)

    Hey @tomwhita,

    The way I’ve been able to create an association when submitting a form is by sending the unserialize value. Like this: ‘”post:project:’ . $post->ID . ‘”‘

    You’re probably setting that as a plain text value. However, it should be set as an PHP array language construct instead. So it basically becomes an array of multiple values in the database, thus it would get serialized.

    When I try to use: array( ‘post:project:$post->ID’ ) to set the field variable, it does not work.

    As mentioned, you are probably doing that manually and inserting it as a plain text value. If you’re manually calling update_post_meta() to set the value, just passing array( 'post:project:$post->ID' ) to the value will work and the value will be automatically serialized. In case you are using a plugin for inserting the data, you should be looking for a possibility to insert arrays of data into one value – this is how the association field is stored.

    Also, can you go one step further in the get_user_by() reference. For example, would it be get_user_by(project_users->ID)?

    Once you have the ID, get_user_by($id) will work properly and retrieve the user object. Note: association data is returned as an array, so you would probably have to call $project_users[0]['id'] in the get_user_by() call.

    Good luck ??

    Thread Starter tomwhita

    (@tomwhita)

    Once you have the ID, get_user_by($id) will work properly and retrieve the user object. Note: association data is returned as an array, so you would probably have to call $project_users[0][‘id’] in the get_user_by() call.

    I don’t undertand. Here is what I’ve tried. This doesn’t work:

    $foremanid = carbon_get_post_meta( $post->ID, 'foreman_association', 'association' ); $foreman = get_user_by( $foremanid[0]['id'] ); echo esc_html($foreman->display_name)

    Please help. I’m so close to being finished with this feature.

    Thanks

    TW

    Thread Starter tomwhita

    (@tomwhita)

    I got it!!!!

    I used:

    $foreman_association = carbon_get_post_meta( $post->ID, 'foreman_association', 'association' ); $foreman = get_user_by('id', $foreman_association[0]['id']); echo esc_html($foreman->display_name);

    I’m still hung up on inserting association values though. I’m trying to insert a user association when I submit a form to create a new project. The gravity form has a drop down field with the display name of the foreman and a value of:

    "user:user:' . $User->ID . '"

    This doesn’t work at all. I can’t find any documentation for gravity forms about submitting values as arrays. Can you suggest anything else?

    Plugin Author htmlBurger

    (@htmlburger)

    Hey @tomwhita,

    Are you using the “Post Custom Field” field in Gravity Forms?

    If you do, you might want to try the Multi Select field type in that field, as it contains multiple values and probably inserts the data in a serialized array.

    If there’s no working way to insert an array of data in a custom field with Gravity Forms, some custom code will probably be necessary to insert that meta after the form submission – Gravity Forms have a lot of hooks available that you can use.

    Thread Starter tomwhita

    (@tomwhita)

    I ended up just changing the association field to a plain text field that will store the ID of the foreman. Since the user won’t be accessing the wordpress admin, the fancy association UI wasn’t needed. I would’ve liked to have figured this out but I have to move on to other things.

    Thanks again for all your help.

    TW

    Plugin Author htmlBurger

    (@htmlburger)

    Howdy @tomwhita,

    It’s always difficult to solve a particular issue without access to some more code. I’m sure it was something minor that was missing in your code.

    Good luck with your project!

    Thread Starter tomwhita

    (@tomwhita)

    Can I hire you to help me populate the right value in my gravity forms function when adding associations? I just need this done and I can’t figure it out. Let me know.

    Plugin Author htmlBurger

    (@htmlburger)

    Howdy @tomwhita,

    Sure you can. But it is not the right place to do so.

    Due to www.ads-software.com forum’s policy (https://codex.www.ads-software.com/Forum_Welcome) we’re not allowed to discuss any commercial work here, nor to share our company URL.

    But we’re sure you can find us and our site by searching for our www.ads-software.com username. Feel free to do so.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Inserting values via Gravity Forms’ is closed to new replies.