GravityView  2.1.1
The best, easiest way to display Gravity Forms entries on your website.
class-gv-field-gravityforms.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The Gravity Forms \GF_Field field object wrapper.
11  */
12 class GF_Field extends Field {
13 
14  /**
15  * @var \GF_Field The backing Gravity Forms field.
16  */
17  public $field;
18 
19  /**
20  * Create self from a configuration array.
21  *
22  * @param array $configuration The configuration array.
23  * @see \GV\Field::as_configuration()
24  * @internal
25  * @since 2.0
26  *
27  * @return \GV\GF_Field|null The field implementation or null on error.
28  */
29  public static function from_configuration( $configuration ) {
30  if ( empty( $configuration['id'] ) || ! is_numeric( $configuration['id'] ) ) {
31  gravityview()->log->error( 'Invalid configuration[id] supplied.' );
32  return null;
33  }
34 
35  if ( empty( $configuration['form_id'] ) || ! $form = \GV\GF_Form::by_id( $configuration['form_id'] ) ) {
36  gravityview()->log->error( 'Invalid configuration[form_id] supplied.' );
37  return null;
38  }
39 
40  $field = self::by_id( $form, $configuration['id'] );
41 
42  if ( ! $field ) {
43  gravityview()->log->error( 'Invalid configuration[id] supplied.' );
44  return null;
45  }
46 
47  $field->update_configuration( $configuration );
48  return $field;
49  }
50 
51  /**
52  * Get a \GV\GF_Field by \GV\GF_Form and Field ID.
53  *
54  * @param \GV\GF_Form $form The Gravity Form form.
55  * @param int $field_id The Gravity Form field ID for the $form.
56  *
57  * @return \GV\Field|null The requested field or null if not found.
58  */
59  public static function by_id( $form, $field_id ) {
60 
61  if ( ! $form || ! is_object( $form ) || ! is_a( $form, '\GV\GF_Form' ) ) {
62  gravityview()->log->error( '$form is not a \GV\GF_Form instance' );
63  return null;
64  }
65 
66  if ( empty( $form->form ) ) {
67  gravityview()->log->error( '$form is not initialized with a backing Gravity Forms form' );
68  return null;
69  }
70 
71 
72  $gv_field = \GFFormsModel::get_field( $form->form, $field_id );
73 
74  if ( ! $gv_field ) {
75  gravityview()->log->error( 'Invalid $field_id #{field_id} for current source', array( 'field_id' => $field_id ) );
76  return null;
77  }
78 
79  $field = new self();
80  $field->ID = $field_id;
81  $field->field = $gv_field;
82 
83  return $field;
84  }
85 
86  /**
87  * Retrieve the label for this field.
88  *
89  * Requires a \GV\GF_Form in this implementation.
90  *
91  * @param \GV\View $view The view for this context if applicable.
92  * @param \GV\Source $source The source (form) for this context if applicable.
93  * @param \GV\Entry $entry The entry for this context if applicable.
94  * @param \GV\Request $request The request for this context if applicable.
95  *
96  * @return string The label for this Gravity Forms field.
97  */
98  public function get_label( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
99 
100  if ( ! $this->show_label ) {
101  return '';
102  }
103 
104  if ( $label = parent::get_label( $view, $source, $entry, $request ) ) {
105  return $label;
106  }
107 
108  if ( ! $source || ! is_object( $source ) || ! is_a( $source, '\GV\GF_Form' ) ) {
109  gravityview()->log->error( '$source is not a valid \GV\GF_Form instance' );
110  return null;
111  }
112 
113  if ( $this->label ) {
114  return $this->label;
115  }
116 
117  /** This is a complex Gravity Forms input. */
118  if ( $input = \GFFormsModel::get_input( $this->field, $this->ID ) ) {
119  $label = ! empty( $input['customLabel'] ) ? $input['customLabel'] : $input['label'];
120  } else {
121  /** This is a field with one label. */
122  $label = $this->field->get_field_label( true, $this->label );
123  }
124 
125  return $label;
126  }
127 
128  /**
129  * Retrieve the value for this field.
130  *
131  * Requires a \GV\GF_Entry in this implementation.
132  *
133  * @param \GV\View $view The view for this context if applicable.
134  * @param \GV\Source $source The source (form) for this context if applicable.
135  * @param \GV\Entry $entry The entry for this context if applicable.
136  * @param \GV\Request $request The request for this context if applicable.
137  *
138  * @return mixed The value for this field.
139  */
140  public function get_value( View $view = null, Source $source = null, Entry $entry = null, Request $request = null ) {
141  if ( ! $entry || ! is_object( $entry ) || ! is_a( $entry, '\GV\GF_Entry' ) ) {
142  gravityview()->log->error( '$entry is not a valid \GV\GF_Entry instance' );
143  return null;
144  }
145 
146  $value = \RGFormsModel::get_lead_field_value( $entry->as_entry(), $this->field );
147 
148  /** Apply parent filters. */
149  return $this->get_value_filters( $value, $view, $source, $entry, $request );
150  }
151 
152  /**
153  * A proxy getter for the backing GravityView field.
154  *
155  * The view field configuration is checked first, though.
156  *
157  * @param string $key The property to get.
158  *
159  * @return mixed The value of the Gravity View field property, or null if not exists.
160  */
161  public function __get( $key ) {
162  if ( $value = parent::__get( $key ) ) {
163  return $value;
164  }
165 
166  if ( $this->field ) {
167  return $this->field->$key;
168  }
169  }
170 }
If this file is called directly, abort.
get_value(View $view=null, Source $source=null, Entry $entry=null, Request $request=null)
Retrieve the value for this field.
If this file is called directly, abort.
gravityview()
Definition: _stubs.php:26
static by_id( $form, $field_id)
Get a by and Field ID.
If this file is called directly, abort.
$gv_field
Definition: time.php:11
static from_configuration( $configuration)
Create self from a configuration array.
get_label(View $view=null, Source $source=null, Entry $entry=null, Request $request=null)
Retrieve the label for this field.
$field_id
Definition: time.php:17
If this file is called directly, abort.
If this file is called directly, abort.
$entry
Definition: notes.php:27
If this file is called directly, abort.
__get( $key)
A proxy getter for the backing GravityView field.
$field
Definition: gquiz_grade.php:11