• Resolved dkurth

    (@dkurth)


    Did a quick search on the web, with nothing specific. So
    1. Can you change a fields attributes dynamically. Say to a greyed out or alternative, read only state? This would include select, text and checkbox fields.

    2. Can this be done in the same show_on_cb callback class? If so, do you have any samples that I could see how it is done.

    I have everything is coming together. Got all the lables changed dynamiclly and hiding fields when associated with a different post category. BUT it occurred to me, that for business purposes, instead of hiding the fields (a free listing), I should grey them out, to promote purchase of a payable listing.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    https://github.com/CMB2/CMB2/wiki/Field-Parameters#attributes

    You can add custom attributes like readonly via this parameter.

    Thread Starter dkurth

    (@dkurth)

    True…but I was talking about dynamically.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    what’s stopping you from using a function to set what value to use or if there should be a value at all?

    attributes = [
        'readonly' = ( $some_value ) ? 'true' : 'false', 
    ],
    
    Thread Starter dkurth

    (@dkurth)

    went another direction

    Thread Starter dkurth

    (@dkurth)

    I closed this and then something occurred to me. Can you add groups together? So that each group would display different sets of fields, but on the same location? back end and front end?

    I tried a quick and dirty method, but the second group did not display.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Nested groups is a fairly often requested thing, but we don’t have it that I know of. The CMB2 github repo has a lot of discussion about that topic. Adding solid support for it…gets complex.

    Thread Starter dkurth

    (@dkurth)

    I get it. instead of 3 dimensions you would now be dealing with 4. It just would have been a way to do different forms within the same metabox, just categorized by type..and thus use less memory. No need to hide fields.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    as long as you don’t need to be switching to each category/group instantly, and are fine with a page refresh, I don’t see why you couldn’t conditionally add fields.

    just set up a conditional and only run the appropriate add_field() calls.

    $cmb2 = new_cmb2_box( [ ... ] );
    
    if ( 'something' == $something ) {
        // Add field specific to "something".
        $cmb2->add_field( [ ... ] );
    } elseif ( 'something_else' == $something ) {
        // Add field specific to "something_else".
        $cmb2->add_field( [ ... ] );
    }
    

    Same metabox, fields based on condition.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Change field attributes dynmaiclly’ is closed to new replies.