CMB2_Field_Display

CMB2 field display base.

Defined (1)

The class is defined in the following location(s).

/includes/CMB2_Field_Display.php  
  1. class CMB2_Field_Display { 
  2.  
  3. /** 
  4. * A CMB field object 
  5. * @var CMB2_Field object 
  6. * @since 2.2.2 
  7. */ 
  8. public $field; 
  9.  
  10. /** 
  11. * The CMB field object's value. 
  12. * @var mixed 
  13. * @since 2.2.2 
  14. */ 
  15. public $value; 
  16.  
  17. /** 
  18. * Get the corresponding display class for the field type. 
  19. * @since 2.2.2 
  20. * @param CMB2_Field $field 
  21. * @return CMB2_Field_Display 
  22. */ 
  23. public static function get( CMB2_Field $field ) { 
  24. switch ( $field->type() ) { 
  25. case 'text_url': 
  26. $type = new CMB2_Display_Text_Url( $field ); 
  27. break; 
  28. case 'text_money': 
  29. $type = new CMB2_Display_Text_Money( $field ); 
  30. break; 
  31. case 'colorpicker': 
  32. $type = new CMB2_Display_Colorpicker( $field ); 
  33. break; 
  34. case 'checkbox': 
  35. $type = new CMB2_Display_Checkbox( $field ); 
  36. break; 
  37. case 'wysiwyg': 
  38. case 'textarea_small': 
  39. $type = new CMB2_Display_Textarea( $field ); 
  40. break; 
  41. case 'textarea_code': 
  42. $type = new CMB2_Display_Textarea_Code( $field ); 
  43. break; 
  44. case 'text_time': 
  45. $type = new CMB2_Display_Text_Time( $field ); 
  46. break; 
  47. case 'text_date': 
  48. case 'text_date_timestamp': 
  49. case 'text_datetime_timestamp': 
  50. $type = new CMB2_Display_Text_Date( $field ); 
  51. break; 
  52. case 'text_datetime_timestamp_timezone': 
  53. $type = new CMB2_Display_Text_Date_Timezone( $field ); 
  54. break; 
  55. case 'select': 
  56. case 'radio': 
  57. case 'radio_inline': 
  58. $type = new CMB2_Display_Select( $field ); 
  59. break; 
  60. case 'multicheck': 
  61. case 'multicheck_inline': 
  62. $type = new CMB2_Display_Multicheck( $field ); 
  63. break; 
  64. case 'taxonomy_radio': 
  65. case 'taxonomy_radio_inline': 
  66. case 'taxonomy_select': 
  67. $type = new CMB2_Display_Taxonomy_Radio( $field ); 
  68. break; 
  69. case 'taxonomy_multicheck': 
  70. case 'taxonomy_multicheck_inline': 
  71. $type = new CMB2_Display_Taxonomy_Multicheck( $field ); 
  72. break; 
  73. case 'file': 
  74. $type = new CMB2_Display_File( $field ); 
  75. break; 
  76. case 'file_list': 
  77. $type = new CMB2_Display_File_List( $field ); 
  78. break; 
  79. case 'oembed': 
  80. $type = new CMB2_Display_oEmbed( $field ); 
  81. break; 
  82. default: 
  83. $type = new self( $field ); 
  84. break; 
  85.  
  86. return $type; 
  87.  
  88. /** 
  89. * Setup our class vars 
  90. * @since 2.2.2 
  91. * @param CMB2_Field $field A CMB2 field object 
  92. */ 
  93. public function __construct( CMB2_Field $field ) { 
  94. $this->field = $field; 
  95. $this->value = $this->field->value; 
  96.  
  97. /** 
  98. * Catchall method if field's 'display_cb' is NOT defined, or field type does 
  99. * not have a corresponding display method 
  100. * @since 2.2.2 
  101. * @param string $method Non-existent method name 
  102. * @param array $arguments All arguments passed to the method 
  103. */ 
  104. public function display() { 
  105. // If repeatable 
  106. if ( $this->field->args( 'repeatable' ) ) { 
  107.  
  108. // And has a repeatable value 
  109. if ( is_array( $this->field->value ) ) { 
  110.  
  111. // Then loop and output. 
  112. echo '<ul class="cmb2-'. str_replace( '_', '-', $this->field->type() ) .'">'; 
  113. foreach ( $this->field->value as $val ) { 
  114. $this->value = $val; 
  115. echo '<li>', $this->_display(), '</li>'; 
  116. echo '</ul>'; 
  117.  
  118. } else { 
  119. $this->_display(); 
  120.  
  121. /** 
  122. * Default fallback display method. 
  123. * @since 2.2.2 
  124. */ 
  125. protected function _display() { 
  126. print_r( $this->value );