Colorpicker.php
6 years ago
Field.php
5 years ago
File.php
6 years ago
Group.php
5 years ago
Media.php
6 years ago
Radio.php
6 years ago
Text.php
6 years ago
Textarea.php
6 years ago
Wysiwyg.php
6 years ago
Group.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\FormAPI\Form; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | |
| 7 | class Group extends Field { |
| 8 | /** |
| 9 | * Field options. |
| 10 | * Note: Allow to update repeater aka group field frontend output. |
| 11 | * |
| 12 | * @since 2.7.0 |
| 13 | * @var array |
| 14 | */ |
| 15 | public $options = []; |
| 16 | |
| 17 | /** |
| 18 | * Sub fields |
| 19 | * |
| 20 | * Note: Allow developer to add sub fields to group. |
| 21 | * |
| 22 | * @since 2.7.0 |
| 23 | * @var array |
| 24 | */ |
| 25 | public $fields = []; |
| 26 | |
| 27 | /** |
| 28 | * @inheritDoc |
| 29 | */ |
| 30 | public function parse( $array ) { |
| 31 | parent::parse( $array ); |
| 32 | |
| 33 | $defaultOptions = [ |
| 34 | 'header_title' => esc_attr__( 'Group', 'give' ), |
| 35 | 'add_button' => esc_html__( 'Add Row', 'give' ), |
| 36 | 'group_numbering' => 0, |
| 37 | 'close_tabs' => 0, |
| 38 | ]; |
| 39 | |
| 40 | $this->options = isset( $array['options'] ) ? |
| 41 | array_merge( $defaultOptions, $array['options'] ) : |
| 42 | $defaultOptions; |
| 43 | |
| 44 | $this->fields = isset( $array['fields'] ) ? |
| 45 | $array['fields'] : |
| 46 | []; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @inheritDoc |
| 51 | */ |
| 52 | public function toArray() { |
| 53 | return array_merge( |
| 54 | parent::toArray(), |
| 55 | [ |
| 56 | 'options' => $this->options, |
| 57 | 'fields' => $this->fields, |
| 58 | ] |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Get sub fields. |
| 65 | * |
| 66 | * @since 2.7.0 |
| 67 | * @param string $fieldId |
| 68 | * @return array |
| 69 | */ |
| 70 | public function getFieldArguments( $fieldId ) { |
| 71 | $field = current( |
| 72 | array_filter( |
| 73 | $this->fields, |
| 74 | static function( $field ) use ( $fieldId ) { |
| 75 | return $fieldId === $field['id']; |
| 76 | } |
| 77 | ) |
| 78 | ); |
| 79 | |
| 80 | // Validate field. |
| 81 | if ( ! $field ) { |
| 82 | throw new InvalidArgumentException( |
| 83 | sprintf( |
| 84 | __( 'Field with %1$s Id does not exist in group.', 'give' ), |
| 85 | $fieldId |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | return $field; |
| 91 | } |
| 92 | } |
| 93 |