magic-fields-team / Magic-Fields-2

Magic Fields 2.X
http://magicfields.org
GNU General Public License v2.0
124 stars 47 forks source link

Related fields issue #11

Closed EivindFS closed 13 years ago

EivindFS commented 13 years ago

Hi again,

There is no unselected state for the related field. So even though no related field is set, it will appear as if the first one is.

I consider this a bug. It would be great if the backend interface gave us the option of setting the text for the dropdown's unselected state ourselves.

Thanks! Eivind

carst commented 13 years ago

I second this. The way it is setup now, using a related field means there always is a value. It would be much more useful if there would be a blank option.

EivindFS commented 13 years ago

Hi carst,

I have edited the code for this field myself if you are interested. Just take this code and paste it into your magic fields directory/fields_type/related_type_field/related_type_field.php

<?php // initialisation global $mf_domain;

// class with static properties encapsulating functions for the field type

class related_type_field extends mf_custom_fields {

public $allow_multiple = TRUE; public $has_properties = TRUE;

public function _update_description(){ global $mf_domain; $this->description = __("This field allows to do relations with other post type",$mf_domain); }

public function _options(){ global $mf_domain;

$posttypes = $this->mf_get_post_types();
$select = array();
foreach($posttypes as $k => $v){
  $select[$k] = $v->label;
}

$data = array(
  'option'  => array(
    'post_type'  => array(
      'type'        =>  'select',
      'id'          =>  'post_type',
      'label'       =>  __('Related Type Panel (Post type)',$mf_domain),
      'name'        =>  'mf_field[option][post_type]',
      'default'     =>  '',
      'options'     => $select,
      'add_empty'   => false,
      'description' =>  '',
      'value'       =>  '',
      'div_class'   => '',
      'class'       => ''
    ),
    'field_order'  => array(
      'type'        =>  'select',
      'id'          =>  'field_order',
      'label'       =>  __('Field for order of Related type',$mf_domain),
      'name'        =>  'mf_field[option][field_order]',
      'default'     =>  '',
      'options'     => array('id' => 'ID','title' =>'Title'),
      'add_empty'   => false,
      'description' =>  '',
      'value'       =>  '',
      'div_class'   => '',
      'class'       => ''
    ),
    'order'  => array(
      'type'        =>  'select',
      'id'          =>  'order',
      'label'       =>  __('Order of Related type',$mf_domain),
      'name'        =>  'mf_field[option][order]',
      'default'     =>  '',
      'options'     => array('asc' => 'ASC','desc' =>'DESC'),
      'add_empty'   => false,
      'description' =>  '',
      'value'       =>  '',
      'div_class'   => '',
      'class'       => ''
    ),
    'notype'  => array(
      'type'        =>  'text',
      'id'          =>  'notype',
      'label'       =>  __('Default option (when no related type has been set)',$mf_domain),
      'name'        =>  'mf_field[option][notype]',
      'default'     =>  '',
      'options'     => '',
      'add_empty'   => false,
      'description' =>  '',
      'value'       =>  '',
      'div_class'   => '',
      'class'       => ''
    )
  )
);

return $data;

}

public function display_field( $field, $group_index = 1, $field_index = 1 ) { $output = '';

$type        = $field['options']->post_type;
$order       = $field['options']->order;
$field_order = $field['options']->field_order;
$notype      = $field['options']->notype;

$options = get_posts( sprintf("post_type=%s&numberposts=-1&order=%s&orderby=%s",$type,$order,$field_order) );
$output = '<div class="mf-dropdown-box">';

$value = $field['input_value'];

$output .= sprintf('<select class="dropdown_mf test" id="%s" name="%s" >',$field['input_id'],$field['input_name']);

//Added by Eivind
$output .= "<option value=''>$notype</option>";

foreach($options as $option) {
  $check = ($option->ID == $value) ? 'selected="selected"' : '';

  $output .= sprintf('<option value="%s" %s >%s</option>',
    esc_attr($option->ID),
    $check,
    esc_attr($option->post_title)
  );
}
$output .= '</select>';
$output .= '</div>';

return $output;

}

}

?>

It is really easy to customize fields in this version. It's quite brilliant really.

Eivind

EivindFS commented 13 years ago

Ignore all the weird stuff github does to my code. Just take out all that's between <?php and ?>

carst commented 13 years ago

Hey thanks! In the meantime I worked around the problem by just adding an option with a zero value and a type of '(none)'.

What you're doing does make more sense however, being able to choose a name for the empty type.

EivindFS commented 13 years ago

Hi Carst,

How exactly do you add an option to a related types field? I can't see that being possible anywhere. Care to explain?

Thanks

carst commented 13 years ago

Hi Eivind, well exactly like you did it, by writing:

// adds an empty value
$output .= sprintf('<option value="0">(none)</option>');
EivindFS commented 13 years ago

Oh, you did it with code as well then. That clarifies it.

Cheers.

E

gnuget commented 13 years ago

Done.

Thank you @EivindFS and @carst :-)

@EivindFS the next time to you want to share some code with us can you use gist (https://gist.github.com/) ? thanks

Antox-xx commented 13 years ago

Also the same issue with a Dropdown Field

gnuget commented 13 years ago

IMHO is not necessary add a "None" option for Dropdown field because in that field you set the options by hand and you can set there the "None" value

Antox-xx commented 13 years ago

Therefore that field always will be set, not empty. And "Required" option will not work correctly, and I will have to do some code to display nothing instead "None".

gnuget commented 13 years ago

Yeah, you are right.

I'm going to work on this.