Support

Account

Home Forums General Issues Group-field multiple choice button – behavior

Solved

Group-field multiple choice button – behavior

  • I’m having some trouble with a group field that i can’t seem to wrap my head around.
    I need to have a button on a page that could take either an E-mail adress field and output it in a mailto: link, or a regular http://www… address field or a .pdf file field.

    I created a group field like this: The switch activates the Card that will contain the button.

    Card-overview

    Details of the group-field:

    Card-details

    Basically the Radio button is suppose to make the switch between the three fields, Url, Email & File.

    This is how the user will see it in the backend of the site:

    Card-backend

    This is how my thinking of structuring the code goes:

    <article class="card">
    
      <?php
      // Variable
      $card = get_field( 'card' );
      ?>
    
      <?php if( $card['button-file'] ) { ?>
    
        <a class="button" href=""><?php echo $card['button-text']; ?></a>
    
      <?php } elseif( $card['button-mail'] ) { ?>
    
        <a class="button" href=""><?php echo $card['button-text']; ?></a>
    
      <?php } else { ?>
    
        <a class="button" href=""><?php echo $card['button-text']; ?></a>
    
      <?php } ?>
    
    </article>

    I haven’t, completley figured out how to output the href for the email & file field yet, because i run into problems with the group field behavior. (or so i belive)
    It seems that the first time i tested the different fields they output the right value, but the values is being saved to the db so even if i choose the Url field it will still output the file field since that is first in my if statement.
    Is it possible that I’m using the wrong fieldtype for this function?
    I need the Backend to look and function like the picture, but it’s possible i don’t fully understand how the group field save values to the db.

  • In this case I would check for the value of the field that sets the condition for the field you want to display. That way even if there is a previous value saved in the database, the one you’d like to be currently displayed will still show up.

    <article class="card">
    	<?php
    	// Variables.
    	$card = get_field( 'card' );
    	$button_type = get_field( 'card' )['button-type'];
    	?>
    
    	<?php if ( $button_type == 'file' ) { ?>
    
    		<a class="button" href="<?php echo $card['button-file']['url']; ?>"><?php echo $card['button-text']; ?></a>
    
    	<?php } elseif ( $button_type == 'email' ) { ?>
    
    		<a class="button" href="mailto:<?php echo $card['button-mail']; ?>"><?php echo $card['button-text']; ?></a>
    
    	<?php } else { ?>
    
    		<a class="button" href="<?php echo $card['button-url']; ?>"><?php echo $card['button-text']; ?></a>
    
    	<?php } ?>
    </article>
  • Thank you!
    This looks like it might solve my problem!
    I’m going to try it out asap, but i have a question to fully understand what is happening here.

    These values:

    Card Value

    Is that the value set in in the Radio button field, or the field type?

    I have my radio button field set up like this:

    radio button field setup

    Just trying to wrap my head around what is what here.

    As a sidenote, I’m thinking that the group field might be the wrong field type for the result I’m trying to achieve?
    The fact that the field values is saved to the db makes for a somewhat strange user experience.
    Lets say i need to use this button field for a “banner card” where the site owner can activate the card, enter some info, add an image & activate a button, it’s a bit off that the previous values is shown?
    Is there any of the similar fields(repeater, flexible-content) that flush the db values upon deactivation, or some hook that i can link to the card activation true / false field that flush the db values upon deactivation?

  • Ah, thanks for asking! Those values you circled are set via the values under “Choices” in the Radio Button field.

    As far as I know, I don’t think that the behaviour would shift whether you are using ‘group’ or not as that is an ACF thing rather than something field-specific. Flexible Content might achieve what you’d like because you would be deleting each component rather than activating/deactivating (if I’m understanding correctly). So that might achieve a similar goal in a slightly different way – just don’t know if that’s desirable for you/ your user.

    Also could be worth looking into hooks like save_post, update_field or update_value to achieve something along these lines depending on what you want to clear/keep?

    This thread might also be of use.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.