mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Tidy up logic from #3465, move helper functions out
This commit is contained in:
parent
cc082d0510
commit
97e156d43d
@ -1,5 +1,6 @@
|
||||
<?php namespace Backend\Classes;
|
||||
|
||||
use Str;
|
||||
use Html;
|
||||
use October\Rain\Database\Model;
|
||||
use October\Rain\Html\Helper as HtmlHelper;
|
||||
@ -465,12 +466,7 @@ class FormField
|
||||
$triggerAction = array_get($this->trigger, 'action');
|
||||
$triggerField = array_get($this->trigger, 'field');
|
||||
$triggerCondition = array_get($this->trigger, 'condition');
|
||||
|
||||
// Check for parent field reference for the trigger condition
|
||||
$triggerFieldParentLevel = $this->getNumberOfPrecedingSymbols($triggerField, self::HIERARCHY_UP);
|
||||
if ($triggerFieldParentLevel > 0) {
|
||||
$triggerField = substr($triggerField, $triggerFieldParentLevel);
|
||||
}
|
||||
$triggerForm = $this->arrayName;
|
||||
|
||||
// Apply these to container
|
||||
if (in_array($triggerAction, ['hide', 'show']) && $position != 'container') {
|
||||
@ -483,11 +479,14 @@ class FormField
|
||||
}
|
||||
|
||||
// Reduce the field reference for the trigger condition field
|
||||
if ($this->arrayName && $triggerFieldParentLevel > 0) {
|
||||
$parentFormName = $this->reduceFieldNameHierarchy($this->arrayName, $triggerFieldParentLevel);
|
||||
$fullTriggerField = $parentFormName.'['.implode('][', HtmlHelper::nameToArray($triggerField)).']';
|
||||
} elseif ($this->arrayName) {
|
||||
$fullTriggerField = $this->arrayName.'['.implode('][', HtmlHelper::nameToArray($triggerField)).']';
|
||||
$triggerFieldParentLevel = Str::getPrecedingSymbols($triggerField, self::HIERARCHY_UP);
|
||||
if ($triggerFieldParentLevel > 0) {
|
||||
$triggerField = substr($triggerField, $triggerFieldParentLevel);
|
||||
$triggerForm = HtmlHelper::reduceFieldNameHierarchy($triggerForm, $triggerFieldParentLevel);
|
||||
}
|
||||
|
||||
if ($this->arrayName) {
|
||||
$fullTriggerField = $triggerForm.'['.implode('][', HtmlHelper::nameToArray($triggerField)).']';
|
||||
}
|
||||
else {
|
||||
$fullTriggerField = $triggerField;
|
||||
@ -501,43 +500,10 @@ class FormField
|
||||
];
|
||||
|
||||
$attributes = $attributes + $newAttributes;
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduces the field name hierarchy depth by $level levels.
|
||||
* country[city][0][street][0] turns into country[city][0] when reduced by 1 level;
|
||||
* country[city][0][street][0] turns into country when reduced by 2 levels;
|
||||
* etc.
|
||||
*
|
||||
* @param string $fieldName
|
||||
* @param int $level
|
||||
* @return string
|
||||
*/
|
||||
protected function reduceFieldNameHierarchy($fieldName, $level) {
|
||||
$parentFormName = HtmlHelper::nameToArray($fieldName);
|
||||
$sliceLength = count($parentFormName) - $level * 2;
|
||||
if ($sliceLength <= 1) {
|
||||
return $parentFormName[0];
|
||||
}
|
||||
|
||||
$parentFormName = array_slice($parentFormName, 0, $sliceLength);
|
||||
$parentFormNameFirst = array_shift($parentFormName);
|
||||
return $parentFormNameFirst.'['.implode('][', $parentFormName).']';
|
||||
}
|
||||
|
||||
/**
|
||||
* If $string begins with any number of consecutive symbols,
|
||||
* returns the number, otherwise returns 0
|
||||
*
|
||||
* @param string $string
|
||||
* @param string $symbol
|
||||
* @return int
|
||||
*/
|
||||
protected function getNumberOfPrecedingSymbols($string, $symbol) {
|
||||
return strlen($string) - strlen(ltrim($string, $symbol));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds attributes used specifically by the Input Preset API
|
||||
* @param array $attributes
|
||||
|
Loading…
x
Reference in New Issue
Block a user