Support » Plugin: CMB2 » New form – User Profile section

  • Resolved dkurth

    (@dkurth)


    I have successfully implemented another repeatable form set, but I need to turn off
    the ability to delete a record and add a group. You can see it in the image here:
    https://snag.gy/Q6tdgj.jpg

    Here is a summary of the startup code.

    //--------------------------------------------------------------------
    /////////////////////////////////////////////////////////////////
    //   User List forms
    ////////////////////////////////////////////////////////////////
    //--------------------------------------------------------------------
    add_shortcode('MMD_DISPLAY_LISTINGS', 'mmd_list_DisplayUserLists');
    function mmd_list_DisplayUserLists($atts = array())
    {
    
    global $post;
    global $LastUserDataBaseRead;
    global $DrawUserLoopCnt;
    
     $LastUserDataBaseRead = 0;	
     $current_user  = wp_get_current_user(); 
    
     $prefix     = 'mmdUserlist'; 
     $PostId     = $post->ID;	
     $metabox_id = 'mmd_lists_user_manual';
     $object_id  = absint( $PostId );	
     
     $DatabaseData = array();
     $DatabaseData  = mmd_lists_retrieve_records_by_email($current_user->user_email);
     if(!empty($DatabaseData))
        {
        $form  = cmb2_get_metabox_form( $metabox_id, $object_id );     // Display nothing if there are no listings found
        return  $form;
    	}
    else
       echo "No Listings Found";
     
    }
    
    //===========================================================================
    //-----------------------------------------------------------------
    // ENTRIES OF CUSTOM POST IN THE ADMIN SECTION
    //-----------------------------------------------------------------
    //==========================================================================
    function mmdlist_add_user_manual_form()   
     { 
    mmd_DebugLog('mmdlist_add_user_manual_form');	 
     
     $prefix = 'mmdUserlist';                                 // Custom Post Name
     $cmb = new_cmb2_box( array(
    		'id'            => 'mmd_lists_user_manual',
    		'title'         => __( 'Your Listings', 'mmd-user' ),
    		'object_types'  => $prefix,                // Post type
    		'context'       => 'normal',
    		'priority'      => 'high',
    		//'hookup'        => false,
    		//'save_fields'   => false,
    		'show_names'    => true,                         // Show field names on the left
    		//'closed'        => true,
    		// 'cmb_styles' => false,                        // false to disable the CMB stylesheet
    	) );
    
    //-------------------------------------------------------------------
    // Group Asssignment
    $group_field_id = $cmb->add_field( array(
    	'id'          => 'MMDUserListsRecord',
    	'type'        => 'group',
    	'description' => __( '', 'mmd-user' ),
    	'options'     => array(
    		'group_title'       => __( 'Record {#}', 'mmd-user' ), // since version 1.1.4, {#} gets replaced by row number
    		//'add_button'        => __( 'Add Another Record', 'mmd' ),
    		//'remove_button'     => __( 'Remove Record', 'mmd' ),
    		//'sortable'          => true,
    		'closed'            => true,
    	),
      'after_group' => 'mmdlist_add_user_js_for_repeatable_titles',
    ) );
    

    From here I add fields, have a custom override to stuff the data into the fields.
    But I can not find where I can turn off this button or remove the delete.

    Can you point me in the right direction?

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Adding some supporting information:

    The site starting acting werid, so I removed the code above and things are back to normal. I have additional questions pf whether using the postid, like I did, causes issues. It was from the online example, but I am open to suggestions.

    Would it better that I create another custom post? These fields are for a user profile information containing their subscription data and the ability to change information. There is no “new post”, just updating my local table. But the could also have more than one set of subscription data, thus the repeatable fields.

    What they can not do, is add records or delete records. That is through woocommerce in the subscription section.

    Ultimately, I am looking for another windows into particular user’s subscription information, that I have stored in my own table.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    The site starting acting werid,

    This could very well be because your code from your other metabox is hijacking the value setting/getting for this box.

    Remember here: https://wordpress.org/support/topic/set-field-values-from-database-table-but-not-metadata/#post-11296830

    You do not need to put the metagroup name in the function call or the callback type. Direct works just fine for storing fields and retrieving them.

    And my response: https://wordpress.org/support/topic/set-field-values-from-database-table-but-not-metadata/page/2/#post-11299199

    If you override the values in a global filter, you override for everything. This is why the filters with the field-ids exist.

    It’s possible this is not related, but I wouldn’t be surprised if it is.

    Re: preventing the adding/removing of rows, you will have to use some Javascript to remove those buttons, as that is not supported out of the box.

    • This reply was modified 8 months, 3 weeks ago by Justin Sternberg. Reason: fix block-quotes

    i am not not a javascript queen. I really hate the language and i get frustrate with it. Any trouble i ever have with websites is always because of javascript. That said, i know this is based on it. Can you make some suggestions on how to do that?

    The reason i do not understand why a naming convention would make any difference is that it is just a call back assignment to a function call, this which is “fixed” address no matter what it’s name. Not a variable. I did put it in, but it made no difference.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    The js solution is going to be admittedly a bit case-by-case as the markup output is going to be dependent on your CMB2 ID values, and field setup. If there’s any way that a custom class can be appended to the intended field, then a jQuery selector should be easy enough to do up.

    I know that I work with a plugin that does similar, and it has this as part of the code:

    $('#cmb2-metabox-ctct_2_fields_metabox #custom_fields_group_repeat .cmb-repeatable-grouping').each( ...

    It then checks finds the “.cmb-remove-group-row” DOM item/button and runs jQuery’s hide() on it.

    Hopefully this perhaps gives some ideas.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    The function name is irrelevant, to that point you are correct. The filter name, however, is crucial. The first parameter passed to add_filter here should be the filter name with the field ID in it: https://gist.github.com/dskurth/d56744de4ae076c417f59b2a459eaf4b#file-demoexample-php-L224

    As I said, the cmb2_override_meta_value filter is called on every value request for every field registered through CMB2, whereas the cmb2_override_MMDListsRecord_meta_value filter will only be called for value requests made to the MMDListsRecord field.

    Same with cmb2_override_meta_save and cmb2_override_meta_remove. You should be using cmb2_override_MMDListsRecord_meta_save and cmb2_override_MMDListsRecord_meta_remove.

    I will now stop harping on the subject as it does not seem to be making a difference. I wish you luck.

    YOu can “harp” all you want. I am willing to try any a good argument. I have just never seen a “variable” override function before in any SDK. So it did not make sense to me. But ok, if you are saying that the override value is flexible, I can try it.

    Michael..I am totally lost with JS. Since I did not write this tool kit, I have no clue (uncomfortable feeling) of where to even begin. Thanks for the snippet, I have not idea what to do with it. Thoughts?

    Micheal, found another simpler way around the problem, that does not involve the dreaded JS.

    change the cmb2-front.min.css and cmb2-front.css with the following fields:

    .cmb-remove-row {
        text-align: right;
        visibility: hidden;  <======= ADD THIS
     }
    
    .cmb-add-row {
        margin: 1.8em 0 0;
        visibility: hidden;  <======= ADD THIS
    }
    
    cmb2-metabox button.dashicons-before.dashicons-no-alt.cmb-remove-group-row:not([disabled]) {
        cursor: pointer;
        color: #a00;
        opacity: 0;           <======== change this from 1 to 0
        visibility: hidden;   <======= ADD THIS
    }
    

    Problem solved. It also enables you to customize the save button to being something a bit more elegant than a block.

    • This reply was modified 8 months, 3 weeks ago by dkurth.
    • This reply was modified 8 months, 3 weeks ago by dkurth.

    For other’s use. Here is some code that change the look of the front-side button:

    .button-primary {
    	-moz-box-shadow: 0px 0px 0px 2px #9fb4f2;
    	-webkit-box-shadow: 0px 0px 0px 2px #9fb4f2;
    	box-shadow: 0px 0px 0px 2px #9fb4f2;
    	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #7892c2), color-stop(1, #476e9e));
    	background:-moz-linear-gradient(top, #7892c2 5%, #476e9e 100%);
    	background:-webkit-linear-gradient(top, #7892c2 5%, #476e9e 100%);
    	background:-o-linear-gradient(top, #7892c2 5%, #476e9e 100%);
    	background:-ms-linear-gradient(top, #7892c2 5%, #476e9e 100%);
    	background:linear-gradient(to bottom, #7892c2 5%, #476e9e 100%);
    	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#7892c2', endColorstr='#476e9e',GradientType=0);
    	background-color:#7892c2;
    	-moz-border-radius:10px;
    	-webkit-border-radius:10px;
    	border-radius:10px;
    	border:1px solid #4e6096;
    	display:inline-block;
    	cursor:pointer;
    	color:#ffffff;
    	font-family:Arial;
    	font-size:19px;
    	padding:12px 37px;
    	text-decoration:none;
    	text-shadow:0px 1px 0px #283966;
    }
    .button-primary:hover {
    	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #476e9e), color-stop(1, #7892c2));
    	background:-moz-linear-gradient(top, #476e9e 5%, #7892c2 100%);
    	background:-webkit-linear-gradient(top, #476e9e 5%, #7892c2 100%);
    	background:-o-linear-gradient(top, #476e9e 5%, #7892c2 100%);
    	background:-ms-linear-gradient(top, #476e9e 5%, #7892c2 100%);
    	background:linear-gradient(to bottom, #476e9e 5%, #7892c2 100%);
    	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#476e9e', endColorstr='#7892c2',GradientType=0);
    	background-color:#476e9e;
    }
    .button-primary:active {
    	position:relative;
    	top:1px;
    }
    
    
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    You’ll want to store those CSS customizations, all of them, somewhere outside of the CMB2 css files because they’ll be lost any time you update the plugin. Don’t be editing the original files.

    Thankfully, you can use something like the admin_head action hook, just like wp_head but for the WP admin. Same thing with admin_enqueue_scripts if you want to add entire files to be loaded instead.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘New form – User Profile section’ is closed to new replies.