Add hasFields methods

formHasOutsideFields, formHasPrimaryTabs + formHasSecondaryTabs
This commit is contained in:
Samuel Georges 2016-04-23 05:13:08 +10:00
parent 72d3fe3ebd
commit 0504da41f2
3 changed files with 86 additions and 21 deletions

View File

@ -465,6 +465,15 @@ class FormController extends ControllerBehavior
return $this->formRender(['preview' => true]); 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. * Helper for custom layouts. Renders Outside Fields.
* @return string The area HTML markup. * @return string The area HTML markup.
@ -474,6 +483,15 @@ class FormController extends ControllerBehavior
return $this->formRender(['section' => 'outside']); 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. * Helper for custom layouts. Renders Primary Tabs.
* @return string The tab HTML markup. * @return string The tab HTML markup.
@ -483,6 +501,15 @@ class FormController extends ControllerBehavior
return $this->formRender(['section' => 'primary']); 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. * Helper for custom layouts. Renders Secondary Tabs.
* @return string The tab HTML markup. * @return string The tab HTML markup.

View File

@ -132,21 +132,6 @@ class FormTabs implements IteratorAggregate, ArrayAccess
return false; 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 * Returns true if any fields have been registered for these tabs
* @return boolean * @return boolean
@ -160,11 +145,26 @@ class FormTabs implements IteratorAggregate, ArrayAccess
* Returns an array of the registered fields, including tabs. * Returns an array of the registered fields, including tabs.
* @return array * @return array
*/ */
public function getTabs() public function getFields()
{ {
return $this->fields; 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. * Get an iterator for the items.
* @return ArrayIterator * @return ArrayIterator
@ -172,8 +172,8 @@ class FormTabs implements IteratorAggregate, ArrayAccess
public function getIterator() public function getIterator()
{ {
return new ArrayIterator($this->suppressTabs return new ArrayIterator($this->suppressTabs
? $this->getFields() ? $this->getAllFields()
: $this->getTabs() : $this->getFields()
); );
} }

View File

@ -446,15 +446,15 @@ class Form extends WidgetBase
/* /*
* Convert automatic spanned fields * Convert automatic spanned fields
*/ */
foreach ($this->allTabs->outside->getTabs() as $fields) { foreach ($this->allTabs->outside->getFields() as $fields) {
$this->processAutoSpan($fields); $this->processAutoSpan($fields);
} }
foreach ($this->allTabs->primary->getTabs() as $fields) { foreach ($this->allTabs->primary->getFields() as $fields) {
$this->processAutoSpan($fields); $this->processAutoSpan($fields);
} }
foreach ($this->allTabs->secondary->getTabs() as $fields) { foreach ($this->allTabs->secondary->getFields() as $fields) {
$this->processAutoSpan($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. * Creates a form field object from name and configuration.
* *
@ -811,6 +823,32 @@ class Form extends WidgetBase
return null; 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 * Parses a field's name
* @param string $field Field name * @param string $field Field name