From 0504da41f2d35f12135bb87f3270b536eb018155 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 23 Apr 2016 05:13:08 +1000 Subject: [PATCH] Add hasFields methods formHasOutsideFields, formHasPrimaryTabs + formHasSecondaryTabs --- modules/backend/behaviors/FormController.php | 27 ++++++++++++ modules/backend/classes/FormTabs.php | 36 ++++++++-------- modules/backend/widgets/Form.php | 44 ++++++++++++++++++-- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/modules/backend/behaviors/FormController.php b/modules/backend/behaviors/FormController.php index fe14ddc6d..0ecd49f40 100644 --- a/modules/backend/behaviors/FormController.php +++ b/modules/backend/behaviors/FormController.php @@ -465,6 +465,15 @@ class FormController extends ControllerBehavior return $this->formRender(['preview' => true]); } + /** + * Helper to check if a form tab has fields. + * @return bool + */ + public function formHasOutsideFields() + { + return $this->formWidget->getTab('outside')->hasFields(); + } + /** * Helper for custom layouts. Renders Outside Fields. * @return string The area HTML markup. @@ -474,6 +483,15 @@ class FormController extends ControllerBehavior return $this->formRender(['section' => 'outside']); } + /** + * Helper to check if a form tab has fields. + * @return bool + */ + public function formHasPrimaryTabs() + { + return $this->formWidget->getTab('primary')->hasFields(); + } + /** * Helper for custom layouts. Renders Primary Tabs. * @return string The tab HTML markup. @@ -483,6 +501,15 @@ class FormController extends ControllerBehavior return $this->formRender(['section' => 'primary']); } + /** + * Helper to check if a form tab has fields. + * @return bool + */ + public function formHasSecondaryTabs() + { + return $this->formWidget->getTab('secondary')->hasFields(); + } + /** * Helper for custom layouts. Renders Secondary Tabs. * @return string The tab HTML markup. diff --git a/modules/backend/classes/FormTabs.php b/modules/backend/classes/FormTabs.php index ecf3fd751..2003643a3 100644 --- a/modules/backend/classes/FormTabs.php +++ b/modules/backend/classes/FormTabs.php @@ -132,21 +132,6 @@ class FormTabs implements IteratorAggregate, ArrayAccess return false; } - /** - * Returns an array of the registered fields, without tabs. - * @return array - */ - public function getFields() - { - $tablessFields = []; - - foreach ($this->getTabs() as $tab) { - $tablessFields += $tab; - } - - return $tablessFields; - } - /** * Returns true if any fields have been registered for these tabs * @return boolean @@ -160,11 +145,26 @@ class FormTabs implements IteratorAggregate, ArrayAccess * Returns an array of the registered fields, including tabs. * @return array */ - public function getTabs() + public function getFields() { return $this->fields; } + /** + * Returns an array of the registered fields, without tabs. + * @return array + */ + public function getAllFields() + { + $tablessFields = []; + + foreach ($this->getFields() as $tab) { + $tablessFields += $tab; + } + + return $tablessFields; + } + /** * Get an iterator for the items. * @return ArrayIterator @@ -172,8 +172,8 @@ class FormTabs implements IteratorAggregate, ArrayAccess public function getIterator() { return new ArrayIterator($this->suppressTabs - ? $this->getFields() - : $this->getTabs() + ? $this->getAllFields() + : $this->getFields() ); } diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index d4f1ec59d..c843eb5dd 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -446,15 +446,15 @@ class Form extends WidgetBase /* * Convert automatic spanned fields */ - foreach ($this->allTabs->outside->getTabs() as $fields) { + foreach ($this->allTabs->outside->getFields() as $fields) { $this->processAutoSpan($fields); } - foreach ($this->allTabs->primary->getTabs() as $fields) { + foreach ($this->allTabs->primary->getFields() as $fields) { $this->processAutoSpan($fields); } - foreach ($this->allTabs->secondary->getTabs() as $fields) { + foreach ($this->allTabs->secondary->getFields() as $fields) { $this->processAutoSpan($fields); } @@ -604,8 +604,20 @@ class Form extends WidgetBase } /** + * Programatically remove all fields belonging to a tab. * + * @param string $name + * @return bool */ + public function removeTab($name) + { + foreach ($this->allFields as $fieldName => $field) { + if ($field->tab == $name) { + $this->removeField($fieldName); + } + } + } + /** * Creates a form field object from name and configuration. * @@ -811,6 +823,32 @@ class Form extends WidgetBase return null; } + /** + * Get all tab objects for the instance. + * + * @return object[FormTabs] + */ + public function getTabs() + { + return $this->allTabs; + } + + /** + * Get a specified tab object. + * Options: outside, primary, secondary. + * + * @param string $field + * @return mixed + */ + public function getTab($tab) + { + if (isset($this->allTabs->$tab)) { + return $this->allTabs->$tab; + } + + return null; + } + /** * Parses a field's name * @param string $field Field name