How to add a css class to a meta box field? #223

Closed
franz-josef-kaiser opened this Issue Aug 28, 2012 · 16 comments

Comments

Projects
None yet
2 participants
@franz-josef-kaiser

I've been running through code for a while now without really getting around how it works.

What I can't figure out is where the input arguments from p2p_register_connection_type() get transformed (to what one can read when using the scbCustomField class is outputting). All var_dump( $arg )s just threw me already rebuilt/parsed data back.

Here's the test case I used to work on the issue.

// Other class method - not extending plugin classes
function example_init()
{
    p2p_register_connection_type( array(
        'fields'             => array(
            ,'ID'     => array(
                'title' => __( 'Example ID', 'my_texdomain' )
                ,'type'  => 'custom'
                ,'extra' => array(
                    'class' => 'disabled'
                 )
            ,'render' => array( $this, 'p2p_hidden_field' )
         )
    ) );
}
// Doesn't get used
function p2p_hidden_field()
{
    echo '';
}

# Output
object(scbCustomField)[399]
  protected 'args' => 
    array
      'render' => string 'var_dump' (length=8)
      'sanitize' => string 'wp_filter_kses' (length=14)
      'desc' => string '' (length=0)
      'desc_pos' => string 'after' (length=5)
      'wrap' => string '%input%' (length=7)
      'wrap_each' => string '%input%' (length=7)
      'name' => 
        array
          0 => string 'p2p_meta' (length=8)
          1 => string '8' (length=1)
          2 => string 'ID' (length=2)
      'type' => string 'custom' (length=6)

Currently I'm a bit lost on what triggers where, etc. I hope there's some help on how to use those arguments.

Thanks.

@scribu

This comment has been minimized.

Show comment Hide comment
@scribu

scribu Aug 28, 2012

Owner

The thing is that the arguments in 'fields' aren't passed directly to scbForms, so basically 'extra' and 'render' are ignored.

The code that does this can be found in P2P_Field_Generic->render().

Owner

scribu commented Aug 28, 2012

The thing is that the arguments in 'fields' aren't passed directly to scbForms, so basically 'extra' and 'render' are ignored.

The code that does this can be found in P2P_Field_Generic->render().

@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 28, 2012

Thanks - found and inspected. But now I'm really lost :)

How would I throw in the arguments I need (P2P_Field_Generic->render() limits them pretty much) and then build a custom field?

Another question (only part of what I'm trying to do): Is there an easier way of simply add custom classes to scbFormFields?

Thanks - found and inspected. But now I'm really lost :)

How would I throw in the arguments I need (P2P_Field_Generic->render() limits them pretty much) and then build a custom field?

Another question (only part of what I'm trying to do): Is there an easier way of simply add custom classes to scbFormFields?

@scribu scribu closed this in 51d7bc9 Aug 28, 2012

@scribu

This comment has been minimized.

Show comment Hide comment
@scribu

scribu Aug 28, 2012

Owner

That should do it.

Owner

scribu commented Aug 28, 2012

That should do it.

@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 28, 2012

Does it perfectly. 'render' => array( $this, 'p2p_custom_field' ) works like a charm. Thanks for doing it in lightspeed!

Does it perfectly. 'render' => array( $this, 'p2p_custom_field' ) works like a charm. Thanks for doing it in lightspeed!

@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 28, 2012

Ok, I had some time playing around with it and tried to not only echo something in the callback, but to use the data and try to build a form field.

Here's what I did - and I'm pretty sure, that this one's not the intended use:

// The callback
/**
 * Callback function for "posts2posts"-plugin
 * to add a custom field
 * 
 * @param  string $p2p_id 
 * @param  object $args Arguments
 * @return string
 */
public function p2p_disabled( $p2p_id, $args )
{
    foreach ( (array) $args as $key => $value )
    {
        $name = $value['name'];

        $html  = '';
        $html .= html( 'input', array(
             'class'    => $value['extra']['class'].' regular-text'
            ,'name'     => "{$name[0]}[{$name[1]}][{$name[2]}]"
            ,'value'    => get_metadata( 'p2p', $name[1], end( $name ), true )
            ,'type'     => 'text'
            ,'disabled' => $value['extra']['class']
        ) );
        $html .= '';
    }

    return $html;
}

I had to convert the data to an array, as the object wasn't accessible else. When I var_dump( $key ); inside the foreach loop, I found the $key to hold the following value: '�*�args'... wired UTF stuff?

As the $object_id isn't available as input argument, I had to use $name[1] instead of it to get the meta data and you can see other sorts of hacks, like the label, as scbFormField::add_label(); isn't directly available from my class.

Can't get rid of the feeling, that this "solution" will break with a future update. Question: How exactly should I build the form field?

Ok, I had some time playing around with it and tried to not only echo something in the callback, but to use the data and try to build a form field.

Here's what I did - and I'm pretty sure, that this one's not the intended use:

// The callback
/**
 * Callback function for "posts2posts"-plugin
 * to add a custom field
 * 
 * @param  string $p2p_id 
 * @param  object $args Arguments
 * @return string
 */
public function p2p_disabled( $p2p_id, $args )
{
    foreach ( (array) $args as $key => $value )
    {
        $name = $value['name'];

        $html  = '';
        $html .= html( 'input', array(
             'class'    => $value['extra']['class'].' regular-text'
            ,'name'     => "{$name[0]}[{$name[1]}][{$name[2]}]"
            ,'value'    => get_metadata( 'p2p', $name[1], end( $name ), true )
            ,'type'     => 'text'
            ,'disabled' => $value['extra']['class']
        ) );
        $html .= '';
    }

    return $html;
}

I had to convert the data to an array, as the object wasn't accessible else. When I var_dump( $key ); inside the foreach loop, I found the $key to hold the following value: '�*�args'... wired UTF stuff?

As the $object_id isn't available as input argument, I had to use $name[1] instead of it to get the meta data and you can see other sorts of hacks, like the label, as scbFormField::add_label(); isn't directly available from my class.

Can't get rid of the feeling, that this "solution" will break with a future update. Question: How exactly should I build the form field?

@scribu

This comment has been minimized.

Show comment Hide comment
@scribu

scribu Aug 29, 2012

Owner

I don't understand what you mean by "build a form field".

Owner

scribu commented Aug 29, 2012

I don't understand what you mean by "build a form field".

@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 29, 2012

Adding a form field to the list table like for example

<input type="custom" data-foo="bar" style="height: 20em;" class="pinky and brain" step="1" />

Point is, that I already got around it. Adding a field with a type of number and args like foo worked, without using custom as type argument. The same goes for hidden and disabled. Now I'm wondering for what use case the custom field type is meant for ... ?

Adding a form field to the list table like for example

<input type="custom" data-foo="bar" style="height: 20em;" class="pinky and brain" step="1" />

Point is, that I already got around it. Adding a field with a type of number and args like foo worked, without using custom as type argument. The same goes for hidden and disabled. Now I'm wondering for what use case the custom field type is meant for ... ?

@scribu

This comment has been minimized.

Show comment Hide comment
@scribu

scribu Aug 29, 2012

Owner

'type' => 'custom' is for when you want to render the field yourself, which should be pretty rare, since you can add arbitrary attributes with the 'extra' option:

'some-field' => array(
  'type' => 'number',
  'extra' => array( 'disabled' => true, 'step' => 1 )
)
Owner

scribu commented Aug 29, 2012

'type' => 'custom' is for when you want to render the field yourself, which should be pretty rare, since you can add arbitrary attributes with the 'extra' option:

'some-field' => array(
  'type' => 'number',
  'extra' => array( 'disabled' => true, 'step' => 1 )
)
@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 29, 2012

I know. That's what I tried above with function p2p_disabled(). I just don't know what's the "right" way to do this. ↑ seems to be the wrong way to write the callback function. I just want to know, how it's done right.

I know. That's what I tried above with function p2p_disabled(). I just don't know what's the "right" way to do this. ↑ seems to be the wrong way to write the callback function. I just want to know, how it's done right.

@scribu

This comment has been minimized.

Show comment Hide comment
@scribu

scribu Aug 29, 2012

Owner

The callback is fine, except you're not supposed to loop through the args. You use each one for a specific purpose.

Owner

scribu commented Aug 29, 2012

The callback is fine, except you're not supposed to loop through the args. You use each one for a specific purpose.

@scribu

This comment has been minimized.

Show comment Hide comment
@scribu

scribu Aug 29, 2012

Owner

Basically, you have to trust that this will work:

function my_renderer( $value, $args ) {
  echo $args->name;
}

It's magic. ;)

Owner

scribu commented Aug 29, 2012

Basically, you have to trust that this will work:

function my_renderer( $value, $args ) {
  echo $args->name;
}

It's magic. ;)

@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 30, 2012

OMG :) Thanks

P.s.: If you could tell me where this happens, it would help my sanity a lot ;)

OMG :) Thanks

P.s.: If you could tell me where this happens, it would help my sanity a lot ;)

@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 30, 2012

Ah, ok - I'm using magic sometimes myself. As I need to do some other funky stuff (question incoming), I wanted to know where exactly this happens. I assume scbFormField::__get()? - to close this question.

Sidenote: Mustache + SCB Framework + Dozens of classes, abstracts, extends, etc. are slightly confusing me - escpecially with hooks involved, which is hard to trace back :)

Ah, ok - I'm using magic sometimes myself. As I need to do some other funky stuff (question incoming), I wanted to know where exactly this happens. I assume scbFormField::__get()? - to close this question.

Sidenote: Mustache + SCB Framework + Dozens of classes, abstracts, extends, etc. are slightly confusing me - escpecially with hooks involved, which is hard to trace back :)

@scribu

This comment has been minimized.

Show comment Hide comment
@scribu

scribu Aug 30, 2012

Owner

It happens in scbCustomField::__get().

Mustache + SCB Framework + Dozens of classes, abstracts, extends, etc. are slightly confusing me

Maybe this will help: http://i.imgur.com/sTylg.png (or maybe it will scare you off completely :)

Owner

scribu commented Aug 30, 2012

It happens in scbCustomField::__get().

Mustache + SCB Framework + Dozens of classes, abstracts, extends, etc. are slightly confusing me

Maybe this will help: http://i.imgur.com/sTylg.png (or maybe it will scare you off completely :)

@franz-josef-kaiser

This comment has been minimized.

Show comment Hide comment
@franz-josef-kaiser

franz-josef-kaiser Aug 30, 2012

I've looked at it before (not posting questions here without digging into it deeply before).

(or maybe it will scare you off completely :)

scaring ... not really. Confusing: Slightly, as I didn't get the entry points for the input arguments. And as I didn't find the scb classes or the mustache stuff in there, I thought I better ask before loosing sanity ;)

You've answered all my questions ( -Thanks!). I guess we better leave it now, so it can be a source for later readers without forcing them to read our whole book :)

I've looked at it before (not posting questions here without digging into it deeply before).

(or maybe it will scare you off completely :)

scaring ... not really. Confusing: Slightly, as I didn't get the entry points for the input arguments. And as I didn't find the scb classes or the mustache stuff in there, I thought I better ask before loosing sanity ;)

You've answered all my questions ( -Thanks!). I guess we better leave it now, so it can be a source for later readers without forcing them to read our whole book :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment