class-acf-field-textarea.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. if ( ! class_exists( 'acf_field_textarea' ) ) :
  3. class acf_field_textarea extends acf_field {
  4. /*
  5. * initialize
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @type function
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param n/a
  14. * @return n/a
  15. */
  16. function initialize() {
  17. // vars
  18. $this->name = 'textarea';
  19. $this->label = __( 'Text Area', 'acf' );
  20. $this->defaults = array(
  21. 'default_value' => '',
  22. 'new_lines' => '',
  23. 'maxlength' => '',
  24. 'placeholder' => '',
  25. 'rows' => '',
  26. );
  27. }
  28. /*
  29. * render_field()
  30. *
  31. * Create the HTML interface for your field
  32. *
  33. * @param $field - an array holding all the field's data
  34. *
  35. * @type action
  36. * @since 3.6
  37. * @date 23/01/13
  38. */
  39. function render_field( $field ) {
  40. // vars
  41. $atts = array();
  42. $keys = array( 'id', 'class', 'name', 'value', 'placeholder', 'rows', 'maxlength' );
  43. $keys2 = array( 'readonly', 'disabled', 'required' );
  44. // rows
  45. if ( ! $field['rows'] ) {
  46. $field['rows'] = 8;
  47. }
  48. // atts (value="123")
  49. foreach ( $keys as $k ) {
  50. if ( isset( $field[ $k ] ) ) {
  51. $atts[ $k ] = $field[ $k ];
  52. }
  53. }
  54. // atts2 (disabled="disabled")
  55. foreach ( $keys2 as $k ) {
  56. if ( ! empty( $field[ $k ] ) ) {
  57. $atts[ $k ] = $k;
  58. }
  59. }
  60. // remove empty atts
  61. $atts = acf_clean_atts( $atts );
  62. // return
  63. acf_textarea_input( $atts );
  64. }
  65. /*
  66. * render_field_settings()
  67. *
  68. * Create extra options for your field. This is rendered when editing a field.
  69. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  70. *
  71. * @param $field - an array holding all the field's data
  72. *
  73. * @type action
  74. * @since 3.6
  75. * @date 23/01/13
  76. */
  77. function render_field_settings( $field ) {
  78. acf_render_field_setting(
  79. $field,
  80. array(
  81. 'label' => __( 'Default Value', 'acf' ),
  82. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  83. 'type' => 'textarea',
  84. 'name' => 'default_value',
  85. )
  86. );
  87. }
  88. /**
  89. * Renders the field settings used in the "Validation" tab.
  90. *
  91. * @since 6.0
  92. *
  93. * @param array $field The field settings array.
  94. * @return void
  95. */
  96. function render_field_validation_settings( $field ) {
  97. acf_render_field_setting(
  98. $field,
  99. array(
  100. 'label' => __( 'Character Limit', 'acf' ),
  101. 'instructions' => __( 'Leave blank for no limit', 'acf' ),
  102. 'type' => 'number',
  103. 'name' => 'maxlength',
  104. )
  105. );
  106. }
  107. /**
  108. * Renders the field settings used in the "Presentation" tab.
  109. *
  110. * @since 6.0
  111. *
  112. * @param array $field The field settings array.
  113. * @return void
  114. */
  115. function render_field_presentation_settings( $field ) {
  116. acf_render_field_setting(
  117. $field,
  118. array(
  119. 'label' => __( 'Rows', 'acf' ),
  120. 'instructions' => __( 'Sets the textarea height', 'acf' ),
  121. 'type' => 'number',
  122. 'name' => 'rows',
  123. 'placeholder' => 8,
  124. )
  125. );
  126. acf_render_field_setting(
  127. $field,
  128. array(
  129. 'label' => __( 'Placeholder Text', 'acf' ),
  130. 'instructions' => __( 'Appears within the input', 'acf' ),
  131. 'type' => 'text',
  132. 'name' => 'placeholder',
  133. )
  134. );
  135. acf_render_field_setting(
  136. $field,
  137. array(
  138. 'label' => __( 'New Lines', 'acf' ),
  139. 'instructions' => __( 'Controls how new lines are rendered', 'acf' ),
  140. 'type' => 'select',
  141. 'name' => 'new_lines',
  142. 'choices' => array(
  143. 'wpautop' => __( 'Automatically add paragraphs', 'acf' ),
  144. 'br' => __( 'Automatically add &lt;br&gt;', 'acf' ),
  145. '' => __( 'No Formatting', 'acf' ),
  146. ),
  147. )
  148. );
  149. }
  150. /*
  151. * format_value()
  152. *
  153. * This filter is applied to the $value after it is loaded from the db and before it is returned to the template
  154. *
  155. * @type filter
  156. * @since 3.6
  157. * @date 23/01/13
  158. *
  159. * @param $value (mixed) the value which was loaded from the database
  160. * @param $post_id (mixed) the $post_id from which the value was loaded
  161. * @param $field (array) the field array holding all the field options
  162. *
  163. * @return $value (mixed) the modified value
  164. */
  165. function format_value( $value, $post_id, $field ) {
  166. // bail early if no value or not for template
  167. if ( empty( $value ) || ! is_string( $value ) ) {
  168. return $value;
  169. }
  170. // new lines
  171. if ( $field['new_lines'] == 'wpautop' ) {
  172. $value = wpautop( $value );
  173. } elseif ( $field['new_lines'] == 'br' ) {
  174. $value = nl2br( $value );
  175. }
  176. // return
  177. return $value;
  178. }
  179. /**
  180. * validate_value
  181. *
  182. * Validates a field's value.
  183. *
  184. * @date 29/1/19
  185. * @since 5.7.11
  186. *
  187. * @param (bool|string) Whether the value is vaid or not.
  188. * @param mixed $value The field value.
  189. * @param array $field The field array.
  190. * @param string $input The HTML input name.
  191. * @return (bool|string)
  192. */
  193. function validate_value( $valid, $value, $field, $input ) {
  194. // Check maxlength.
  195. if ( $field['maxlength'] && ( acf_strlen( $value ) > $field['maxlength'] ) ) {
  196. return sprintf( __( 'Value must not exceed %d characters', 'acf' ), $field['maxlength'] );
  197. }
  198. // Return.
  199. return $valid;
  200. }
  201. /**
  202. * Return the schema array for the REST API.
  203. *
  204. * @param array $field
  205. * @return array
  206. */
  207. function get_rest_schema( array $field ) {
  208. $schema = parent::get_rest_schema( $field );
  209. if ( ! empty( $field['maxlength'] ) ) {
  210. $schema['maxLength'] = (int) $field['maxlength'];
  211. }
  212. return $schema;
  213. }
  214. }
  215. // initialize
  216. acf_register_field_type( 'acf_field_textarea' );
  217. endif; // class_exists check