17 define(
'FIELD_POSITION_BEFORE',
'before');
18 define(
'FIELD_POSITION_AFTER',
'after');
70 public function addField($field, $position = []) {
71 if (empty($position)) {
72 $this->fields[] = $field;
74 $this->fields = $this->
addToPosition($position[1], $this->fields, $field, $position[0]);
86 $this->fields = array_filter($this->fields,
function($field) use ($fieldName) {
87 return $field->name !== $fieldName;
98 public function getField($fieldName) {
99 foreach ($this->fields as $field) {
100 if ($field->name === $fieldName) {
121 public function addGroup($args, $position = []) {
122 if (empty($args[
'id'])) {
123 fatalError(
'Tried to add a form group without an id.');
125 if (empty($position)) {
126 $this->groups[] = $args;
128 $this->groups = $this->
addToPosition($position[1], $this->groups, $args, $position[0]);
140 $this->groups = array_filter($this->groups,
function($group) use ($groupId) {
141 return $group[
'id'] !== $groupId;
143 $this->fields = array_filter($this->fields,
function($field) use ($groupId) {
144 return $field[
'groupId'] !== $groupId;
164 public function addPage($args, $position = []) {
165 if (empty($args[
'id'])) {
166 fatalError(
'Tried to add a form page without an id.');
168 if (empty($position)) {
169 $this->pages[] = $args;
171 $this->pages = $this->
addToPosition($position[1], $this->pages, $args, $position[0]);
183 $this->pages = array_filter($this->pages,
function($page) use ($pageId) {
184 return $page[
'id'] !== $pageId;
186 foreach ($this->groups as $group) {
187 if ($group[
'pageId'] === $pageId) {
204 $index = count($list);
205 foreach ($list as $key => $val) {
206 if ((is_a($val,
'PKP\components\forms\Field') &&
$id === $val->name) || (!is_a($val,
'PKP\components\forms\Field') &&
$id === $val[
'id'])) {
212 array_unshift($list, $item);
219 array_slice($list, 0, $slice),
221 array_slice($list, $slice)
233 if (empty($this->
id) || empty($this->method) || empty($this->action)) {
234 throw new Exception(
'FormComponent::getConfig() was called but one or more required property is missing: id, method, action.');
240 if (!$this->groups) {
241 $this->
addGroup(array(
'id' =>
'default'));
242 $this->fields = array_map(
function($field) {
243 $field->groupId =
'default';
249 $this->
addPage(array(
'id' =>
'default',
'submitButton' => array(
'label' => __(
'common.save'))));
250 $this->groups = array_map(
function($group) {
251 $group[
'pageId'] =
'default';
256 $fieldsConfig = array_map([$this,
'getFieldConfig'], $this->fields);
265 'method' => $this->method,
266 'action' => $this->action,
267 'fields' => $fieldsConfig,
268 'groups' => $this->groups,
269 'pages' => $this->pages,
271 'visibleLocales' => $visibleLocales,
272 'supportedFormLocales' => array_values($this->locales),
273 'errors' => (
object) [],
288 $config = $field->getConfig();
291 if (!array_key_exists(
'value', $config)) {
292 $config[
'value'] = $field->isMultilingual ? array() : $field->getEmptyValue();
294 if ($field->isMultilingual) {
295 foreach ($this->locales as $locale) {
296 if (!array_key_exists($locale[
'key'], $config[
'value'])) {
297 $config[
'value'][$locale[
'key']] = $field->getEmptyValue();