GravityView  1.22.6
The best, easiest way to display Gravity Forms entries on your website.
class-gv-collection-field.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  * A collection of \GV\Field objects.
11  */
13  /**
14  * Add a \GV\Field to this collection.
15  *
16  * @param \GV\Field $field The field to add to the internal array.
17  *
18  * @api
19  * @since future
20  * @return void
21  */
22  public function add( $field ) {
23  if ( ! $field instanceof Field ) {
24  gravityview()->log->error( 'Field_Collections can only contain objects of type \GV\Field.' );
25  return;
26  }
27  parent::add( $field );
28  }
29 
30  /**
31  * Get a \GV\Field from this list by UID.
32  *
33  * @param int $field_uid The UID of the field in the field to get.
34  *
35  * @api
36  * @since future
37  *
38  * @return \GV\Field|null The \GV\Field with the $field_uid as the UID, or null if not found.
39  */
40  public function get( $field_uid ) {
41  foreach ( $this->all() as $field ) {
42  if ( $field->UID == $field_uid ) {
43  return $field;
44  }
45  }
46  return null;
47  }
48 
49  /**
50  * Get a copy of this \GV\Field_Collection filtered by position.
51  *
52  * @param string $position The position to get the fields for.
53  *
54  * @api
55  * @since
56  *
57  * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by position.
58  */
59  public function by_position( $position ) {
60  $fields = new self();
61  foreach ( $this->all() as $field ) {
62  if ( $field->position == $position ) {
63  $fields->add( $field );
64  }
65  }
66  return $fields;
67  }
68 
69  /**
70  * Get a copy of this \GV\Field_Collection filtered by visibility to current user context.
71  *
72  * @api
73  * @since
74  *
75  * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by visibility.
76  */
77  public function by_visible() {
78  $fields = new self();
79 
80  foreach ( $this->all() as $field ) {
81  if ( ! $field->cap || \GVCommon::has_cap( $field->cap ) ) {
82  $fields->add( $field );
83  }
84  }
85  return $fields;
86  }
87 
88  /**
89  * Parse a configuration array into a Field_Collection.
90  *
91  * @param array $configuration The configuration, structured like so:
92  *
93  * array(
94  *
95  * [other zones]
96  *
97  * 'directory_list-title' => array(
98  *
99  * [other fields]
100  *
101  * '5372653f25d44' => array(
102  * @see \GV\Field::as_configuration() for structure
103  * )
104  *
105  * [other fields]
106  * )
107  *
108  * [other zones]
109  * )
110  *
111  * @return \GV\Field_Collection A collection of fields.
112  */
113  public static function from_configuration( $configuration ) {
114  $fields = new self();
115  foreach ( $configuration as $position => $_fields ) {
116 
117  if ( empty( $_fields ) || ! is_array( $_fields ) ) {
118  continue;
119  }
120 
121  foreach ( $_fields as $uid => $_field ) {
122  $field = new \GV\Field();
123  $field->UID = $uid;
124  $field->position = $position;
125  $field->from_configuration( $_field );
126 
127  $fields->add( $field );
128  }
129  }
130  return $fields;
131  }
132 
133  /**
134  * Return a configuration array for this field collection.
135  *
136  * @return array See \GV\Field_Collection::from_configuration() for structure.
137  */
138  public function as_configuration() {
139  $configuration = array();
140 
141  /**
142  * @var \GV\Field $field
143  */
144  foreach ( $this->all() as $field ) {
145  if ( empty( $configuration[ $field->position ] ) ) {
146  $configuration[ $field->position ] = array();
147  }
148 
149  $configuration[ $field->position ][ $field->UID ] = $field->as_configuration();
150  }
151  return $configuration;
152  }
153 }
by_visible()
Get a copy of this filtered by visibility to current user context.
If this file is called directly, abort.
If this file is called directly, abort.
If this file is called directly, abort.
add( $field)
Add a to this collection.
by_position( $position)
Get a copy of this filtered by position.
gravityview()
The main GravityView wrapper function.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
static from_configuration( $configuration)
Parse a configuration array into a Field_Collection.
$field
Definition: gquiz_grade.php:11