GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-custom.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-custom.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
8 /**
9  * Add custom options for Code field
10  * @since 1.2
11  */
13 
14  var $name = 'custom';
15 
16  var $contexts = array( 'single', 'multiple', 'edit' );
17 
18  /**
19  * @var bool
20  * @since 1.15.3
21  */
22  var $is_sortable = false;
23 
24  /**
25  * @var bool
26  * @since 1.15.3
27  */
28  var $is_searchable = false;
29 
30  var $group = 'gravityview';
31 
32  var $icon = 'dashicons-editor-code';
33 
34  public function __construct() {
35 
36  $this->label = esc_html__( 'Custom Content', 'gk-gravityview' );
37  $this->description = esc_html__( 'Insert custom text or HTML.', 'gk-gravityview' );
38 
39  add_filter( 'gravityview/edit_entry/form_fields', array( $this, 'show_field_in_edit_entry' ), 10, 4 );
40 
41  add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 100, 3 );
42 
43  parent::__construct();
44  }
45 
46  /**
47  * Add as a default field, outside those set in the Gravity Form form
48  *
49  * @since 2.10 Moved here from GravityView_Admin_Views::get_entry_default_fields
50  *
51  * @param array $entry_default_fields Existing fields
52  * @param string|array $form form_ID or form object
53  * @param string $zone Either 'single', 'directory', 'edit', 'header', 'footer'
54  *
55  * @return array
56  */
57  public function add_default_field( $entry_default_fields, $form = array(), $zone = '' ) {
58 
59  $entry_default_fields['custom'] = array(
60  'label' => $this->label,
61  'type' => $this->name,
62  'desc' => $this->description,
63  'icon' => $this->icon,
64  'group' => $this->group,
65  );
66 
67  return $entry_default_fields;
68  }
69 
70  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
71 
72  unset ( $field_options['search_filter'], $field_options['show_as_link'], $field_options['new_window'] );
73 
74  $new_fields = array(
75  'content' => array(
76  'type' => 'textarea',
77  'label' => __( 'Custom Content', 'gk-gravityview' ),
78  'desc' => sprintf( __( 'Enter text or HTML. Also supports shortcodes. You can show or hide data using the %s shortcode (%slearn more%s).', 'gk-gravityview' ), '<code>[gvlogic]</code>', '<a href="https://docs.gravityview.co/article/252-gvlogic-shortcode" data-beacon-article-sidebar="552355bfe4b0221aadf2572b">', '</a>' ) . ' ' . sprintf( __( 'Click the arrow icon next to the content area to add %sMerge Tags%s.', 'gk-gravityview' ), '<a href="https://docs.gravityview.co/article/76-merge-tags" data-beacon-article-inline="54c67bbbe4b051242988551d">', '</a>' ),
79  'value' => '',
80  'class' => 'code',
81  'merge_tags' => 'force',
82  'rows' => 15,
83  'show_all_fields' => true, // Show the `{all_fields}` and `{pricing_fields}` merge tags
84  'priority' => 900,
85  'group' => 'field',
86  ),
87  'wpautop' => array(
88  'type' => 'checkbox',
89  'label' => __( 'Automatically add paragraphs to content', 'gk-gravityview' ),
90  'tooltip' => __( 'Wrap each block of text in an HTML paragraph tag (recommended for text).', 'gk-gravityview' ),
91  'value' => '',
92  'priority' => 950,
93  'group' => 'field',
94  ),
95  'oembed' => array(
96  'type' => 'checkbox',
97  'label' => __( 'Render oEmbeds', 'gk-gravityview' ),
98  'desc' => sprintf( _x( 'Automatically convert oEmbed URLs into embedded content (%slearn more%s).', 'HTML link pointing to WordPress article on oEmbed', 'gk-gravityview' ), '<a href="https://codex.wordpress.org/Embeds" rel="external noopener noreferrer">', '</a>' ),
99  'value' => '',
100  'priority' => 970,
101  'group' => 'field',
102  ),
103  'admin_label' => array(
104  'type' => 'text',
105  'class' => 'widefat',
106  'label' => __( 'Admin Label', 'gk-gravityview' ),
107  'desc' => __( 'A label that is only shown in the GravityView View configuration screen.', 'gk-gravityview' ),
108  'value' => '',
109  'priority' => 2000,
110  'group' => 'label',
111  ),
112  );
113 
114  if ( 'edit' === $context ) {
115  unset( $field_options['custom_label'], $field_options['show_label'], $field_options['allow_edit_cap'], $new_fields['wpautop'], $new_fields['oembed'] );
116  }
117 
118  return $new_fields + $field_options;
119  }
120 
121  /**
122  * Adds the GravityView Custom Content field to the Edit Entry form
123  *
124  * It does this by pretending to be a HTML field so that Gravity Forms displays it
125  *
126  * @since 1.19.2
127  *
128  * @param GF_Field[] $fields Gravity Forms form fields
129  * @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration
130  * @param array $form GF Form array (`fields` key modified to have only fields configured to show in Edit Entry)
131  * @param int $view_id View ID
132  *
133  * @return GF_Field[] If Custom Content field exists, returns fields array with the fields inserted. Otherwise, returns unmodified fields array.
134  */
135  public function show_field_in_edit_entry( $fields, $edit_fields = null, $form = array(), $view_id = 0 ) {
136 
137  // Not configured; show all fields.
138  if ( is_null( $edit_fields ) ) {
139  return $fields;
140  }
141 
142  $new_fields = array();
143  $i = 0;
144 
145  $entry = gravityview()->request->is_edit_entry( $form['id'] );
146 
147  // Loop through the configured Edit Entry fields and add Custom Content fields if there are any
148  // TODO: Make this available to other custom GV field types
149  foreach ( (array) $edit_fields as $id => $edit_field ) {
150  if ( 'custom' === \GV\Utils::get( $edit_field, 'id' ) ) {
151  $field_data = array(
152  'custom_id' => $id,
153  'label' => \GV\Utils::get( $edit_field, 'custom_label' ),
154  'customLabel' => \GV\Utils::get( $edit_field, 'custom_label' ),
155  'content' => \GV\Utils::get( $edit_field, 'content' ),
156  );
157 
158  // Replace merge tags in the content
159  foreach ( $field_data as $key => $field_datum ) {
160  $entry_data = $entry ? $entry->as_entry() : array();
161  $field_data[ $key ] = GravityView_Merge_Tags::replace_variables( $field_datum, $form, $entry_data, false, false );
162  }
163 
164  $field_data['cssClass'] = \GV\Utils::get( $edit_field, 'custom_class' );
165 
166  $new_fields[] = new GF_Field_HTML( $field_data );
167 
168  } else {
169  if( isset( $fields[ $i ] ) ) {
170  $new_fields[] = $fields[ $i ];
171  }
172  $i++;
173  }
174 
175  }
176 
177  return $new_fields;
178  }
179 
180 }
181 
Modify field settings by extending this class.
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
static replace_variables( $text, $form=array(), $entry=array(), $url_encode=false, $esc_html=true, $nl2br=true, $format='html', $aux_data=array())
Alias for GFCommon::replace_variables()
if(gravityview() ->plugin->is_GF_25()) $form
scale description p description
Add custom options for Code field.
if(empty( $created_by)) $form_id
add_default_field( $entry_default_fields, $form=array(), $zone='')
Add as a default field, outside those set in the Gravity Form form.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
$entry
Definition: notes.php:27
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
show_field_in_edit_entry( $fields, $edit_fields=null, $form=array(), $view_id=0)
Adds the GravityView Custom Content field to the Edit Entry form.