/core/fields/page_link.php

  1. <?php 
  2.  
  3. class acf_field_page_link extends acf_field 
  4. /** 
  5. * __construct 
  6. * 
  7. * Set name / label needed for actions / filters 
  8. * 
  9. * @since 3.6 
  10. * @date 23/01/13 
  11. */ 
  12.  
  13. function __construct() 
  14. // vars 
  15. $this->name = 'page_link'; 
  16. $this->label = __("Page Link", 'acf'); 
  17. $this->category = __("Relational", 'acf'); 
  18. $this->defaults = array( 
  19. 'post_type' => array('all'),  
  20. 'multiple' => 0,  
  21. 'allow_null' => 0,  
  22. ); 
  23.  
  24.  
  25. // do not delete! 
  26. parent::__construct(); 
  27.  
  28.  
  29.  
  30. /** 
  31. * load_field() 
  32. *  
  33. * This filter is appied to the $field after it is loaded from the database 
  34. *  
  35. * @type filter 
  36. * @since 3.6 
  37. * @date 23/01/13 
  38. *  
  39. * @param $field - the field array holding all the field options 
  40. *  
  41. * @return $field - the field array holding all the field options 
  42. */ 
  43.  
  44. function load_field( $field ) 
  45.  
  46. // validate post_type 
  47. if( !$field['post_type'] || !is_array($field['post_type']) || in_array('', $field['post_type']) ) 
  48. $field['post_type'] = array( 'all' ); 
  49.  
  50.  
  51. // return 
  52. return $field; 
  53.  
  54.  
  55. /** 
  56. * create_field() 
  57. * 
  58. * Create the HTML interface for your field 
  59. * 
  60. * @param $field - an array holding all the field's data 
  61. * 
  62. * @type action 
  63. * @since 3.6 
  64. * @date 23/01/13 
  65. */ 
  66.  
  67. function create_field( $field ) 
  68. // let post_object create the field 
  69. $field['type'] = 'post_object'; 
  70.  
  71. do_action('acf/create_field', $field ); 
  72.  
  73.  
  74. /** 
  75. * create_options() 
  76. * 
  77. * Create extra options for your field. This is rendered when editing a field. 
  78. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  79. * 
  80. * @type action 
  81. * @since 3.6 
  82. * @date 23/01/13 
  83. * 
  84. * @param $field - an array holding all the field's data 
  85. */ 
  86.  
  87. function create_options( $field ) 
  88. $key = $field['name']; 
  89.  
  90. ?> 
  91. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  92. <td class="label"> 
  93. <label for=""><?php _e("Post Type", 'acf'); ?></label> 
  94. </td> 
  95. <td> 
  96. <?php  
  97.  
  98. $choices = array( 
  99. 'all' => __("All", 'acf') 
  100. ); 
  101. $choices = apply_filters('acf/get_post_types', $choices); 
  102.  
  103.  
  104. do_action('acf/create_field', array( 
  105. 'type' => 'select',  
  106. 'name' => 'fields['.$key.'][post_type]',  
  107. 'value' => $field['post_type'],  
  108. 'choices' => $choices,  
  109. 'multiple' => 1,  
  110. )); 
  111.  
  112. ?> 
  113. </td> 
  114. </tr> 
  115. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  116. <td class="label"> 
  117. <label><?php _e("Allow Null?", 'acf'); ?></label> 
  118. </td> 
  119. <td> 
  120. <?php 
  121.  
  122. do_action('acf/create_field', array( 
  123. 'type' => 'radio',  
  124. 'name' => 'fields['.$key.'][allow_null]',  
  125. 'value' => $field['allow_null'],  
  126. 'choices' => array( 
  127. 1 => __("Yes", 'acf'),  
  128. 0 => __("No", 'acf'),  
  129. ),  
  130. 'layout' => 'horizontal',  
  131. )); 
  132.  
  133. ?> 
  134. </td> 
  135. </tr> 
  136. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  137. <td class="label"> 
  138. <label><?php _e("Select multiple values?", 'acf'); ?></label> 
  139. </td> 
  140. <td> 
  141. <?php 
  142.  
  143. do_action('acf/create_field', array( 
  144. 'type' => 'radio',  
  145. 'name' => 'fields['.$key.'][multiple]',  
  146. 'value' => $field['multiple'],  
  147. 'choices' => array( 
  148. 1 => __("Yes", 'acf'),  
  149. 0 => __("No", 'acf'),  
  150. ),  
  151. 'layout' => 'horizontal',  
  152. )); 
  153.  
  154. ?> 
  155. </td> 
  156. </tr> 
  157. <?php 
  158.  
  159.  
  160.  
  161. /** 
  162. * format_value_for_api() 
  163. * 
  164. * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field 
  165. * 
  166. * @type filter 
  167. * @since 3.6 
  168. * @date 23/01/13 
  169. * 
  170. * @param $value - the value which was loaded from the database 
  171. * @param $post_id - the $post_id from which the value was loaded 
  172. * @param $field - the field array holding all the field options 
  173. * 
  174. * @return $value - the modified value 
  175. */ 
  176.  
  177. function format_value_for_api( $value, $post_id, $field ) 
  178. if( !$value ) 
  179. return false; 
  180.  
  181. if( $value == 'null' ) 
  182. return false; 
  183.  
  184. if( is_array($value) ) 
  185. foreach( $value as $k => $v ) 
  186. $value[ $k ] = get_permalink($v); 
  187. else 
  188. $value = get_permalink($value); 
  189.  
  190. return $value; 
  191.  
  192.  
  193. new acf_field_page_link(); 
  194.  
  195. ?> 
.