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