• Resolved Windurin

    (@windurin)


    I have a repeater on a custom post type with 3 fields: a Text field, a Choice field and a True/False field.

    On the Forms settings, When using the dynamic tags from the cheatsheet to generate an email when a new post of this custom post type is created, it shows ALL data from the repeater including the field keys, etc.

    How can I get JUST the Text fields from the repeater in the email?

    Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback! You can format the value of any ACFE Form field output when using {field} & {fields} Template Tags using the acfe/form/format_value hook. See documentation.

    In your case, you can use the following hook in your functions.php file to format value of the Repeater:

    add_filter('acfe/form/format_value/type=repeater', 'my_acfe_form_repeater_format', 10, 4);
    function my_acfe_form_repeater_format($value, $_value, $post_id, $field){
    
        $value = acf_get_array($_value);
        $return = '';
        
        foreach($value as $i => $sub_fields){
    
            $array = array();
            $return .= "<br/>\n- ";
    
            foreach($sub_fields as $key => $val){
        
                $sub_field = acf_get_field($key);
        
                if(!$sub_field) continue;
        
                // Label
                $label = !empty($sub_field['label']) ? $sub_field['label'] : $sub_field['name'];
        
                // Value
                $sub_field['name'] = $field['name'] . '_' . $i . '_' . $sub_field['name'];
                $val = acfe_form_format_value($val, $sub_field);
        
                // Add
                $array[] = $label . ': ' . $val;
        
            }
            
            $return .= implode(', ', $array);
            
        }
    
        return $return;
        
    }
    

    I’ll add that default Repeater value format in the next patch.

    On a side note, when seding emails with ACFE Form, you will probably prefer to fully customize the email output when using complex fields like here. You can achieve that using the acfe/form/submit/email_args filter. See documentation. In that hook you’ll be able to use get_field('my_field') and have_rows('my_repeater') just like in front-end.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter Windurin

    (@windurin)

    Hi Konrad,

    Thanks very much for the detailed response. With that filter, the formatting is better but it is still showing ALL subfields from the repeater when I only want one of them.

    So I tried the email args filter and I don’t think I’m doing it right.

    With this code here:

    add_filter('acfe/form/submit/email_args/form=add-task-list-form', 'my_form_email_args', 10, 3);
    function my_form_email_args($args, $form, $action){
        
        $task_list = get_field('job_task_list');
    	
    	if( have_rows('job_list_list') ){
    
    	    while( have_rows('job_list_list') ){
    		
    		    the_row();
    	
    		    $task = get_sub_field('job_list_task');
    			
    		$args['content'] = $task;
    			
    	    }
    	}
        
        return $args;
        
    }

    It is only outputting the last row of the subfield in the repeater. What’s the correct formatting for the [‘content’] if I want to list the rows?

    Thanks very much.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    With that filter, the formatting is better but it is still showing ALL subfields from the repeater when I only want one of them.

    Yes, this is why I recommended you the second solution to fully customize the email output in PHP.

    The filter you’re using allows you to customize everything in the email settings: from, to, headers, content… Each one as an array key. The problem in your code is that you completely overwrite the $args['content'] in every row of the Repeater (this is why you only get the last row value, since it has overwritten all other rows).

    To be more confortable with HTML/PHP output inside an array key, I would recommend to use ob_start() and ob_get_clean(). See PHP documentation.

    It allows you to write HTML/PHP just like in the front-end, and get all the output content inside a variable, that you can then assign to the $args['content']. Usage example:

    add_filter('acfe/form/submit/email_args/form=add-task-list-form', 'my_form_email_args', 10, 3);
    function my_form_email_args($args, $form, $action){
        
        // start PHP buffer
        ob_start();
        
        ?>
        <p>Hello,</p>
        
        <p>This is the content of the email.</p>
        <p>Repeater data:</p>
        
        <?php if(have_rows('job_list_list')): ?>
            <?php while(have_rows('job_list_list')): the_row(); ?>
            
                Task: <?php echo get_sub_field('job_list_task'); ?>
                <br />
            
            <?php endwhile; ?>
        <?php endif; ?>
        
        <p>Regards.</p>
        
        <?php
        
        // end PHP buffer and set output in $html variable
        $html = ob_get_clean();
        
        // assign $html to the email content
        $args['content'] = $html;
        
        // return
        return $args;
        
    }
    

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter Windurin

    (@windurin)

    Hi Konrad,

    That’s exactly what I was looking for, thanks! I was struggling to figure out how to use even html within the block without the code editor shouting at me ha. Being able to write the code like frontend development is much easier for me.

    Thanks again. I really appreciate the help and for proving the actual code.

    Chris.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    You’re welcome!

    If you enjoy this plugin and its support, feel free to submit a review. It always helps and it’s much appreciated 🙂

    Have a nice day!

    Regards.

    Thread Starter Windurin

    (@windurin)

    Thanks Konrad, I will surely leave a review soon. I have just 1 more question if you don’t mind. How can I get the permalink of the post being created by the form?

    E.g:

    <p>Click <a href="<?php echo get_the_permalink(); ?>">here</a> to view it now.</p>

    Links to the URL of the place where the form was which makes sense. I know I need a variable in the () but what would it be in this case?

    On the email settings in the Form options it’s:

    Click <a href="{action:post:permalink}">here</a> to view it now.

    So what would the equivalent be in the code snippet?

    Thanks.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    There is a code snippet example in the acfe/form/submit/email_args documentation I linked, explaining how to retrieve a previous action output.

    The function you’re looking for is: acfe_form_get_action(). See helper documentation.

    In your case, the equivalent would be acfe_form_get_action('post', 'permalink'), or acfe_form_get_action('post') to retrieve the whole post output array.

    Regards.

    Thread Starter Windurin

    (@windurin)

    Thanks Konrad, got it working now.

    Just a quick note that in your snippet above you have it as acfe_get_form_action but according to the docs it’s acfe_form_get_action

    So it seems the form and get were the wrong way around but I copy/pasted it from the docs so it’s all good 🙂

    Thanks a lot.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Oops! 🙂

    Just updated my previous answer.

    Glad to hear it now works!

    Regards.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Getting Repeater Text Field In Email’ is closed to new replies.