From f8c3eac9c68b07e04455dd9f0671731bf25a34cc Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 7 Mar 2015 10:45:22 +1100 Subject: [PATCH] Streamline data-input-preset in to FormField API --- .../backend/assets/js/october.inputpreset.js | 3 +- modules/backend/classes/FormField.php | 95 +++++++++++++------ 2 files changed, 66 insertions(+), 32 deletions(-) diff --git a/modules/backend/assets/js/october.inputpreset.js b/modules/backend/assets/js/october.inputpreset.js index 33d4bdd1b..87f7a1a46 100644 --- a/modules/backend/assets/js/october.inputpreset.js +++ b/modules/backend/assets/js/october.inputpreset.js @@ -1,7 +1,8 @@ /* * An input preset converter. * - * The API allows to convert text entered into an element to an URL or file name value in another input element. + * The API allows to convert text entered into an element to a URL, slug or file name + * value in another input element. * * Supported data attributes: * - data-input-preset: specifies a CSS selector for a source input element diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 2d8cab757..9b9a9d9b4 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -154,6 +154,11 @@ class FormField */ public $trigger; + /** + * @var array Other field names text is converted in to a URL, slug or file name value in this field. + */ + public $preset; + /** * Constructor. * @param string $fieldName @@ -244,15 +249,38 @@ class FormField */ protected function evalConfig($config) { + /* + * Standard config:property values + */ + $applyConfigValues = [ + 'context', + 'placeholder', + 'cssClass', + 'dependsOn', + 'trigger', + 'preset', + 'path', + 'required', + 'disabled', + 'hidden', + 'stretch', + ]; + + foreach ($applyConfigValues as $value) { + if (array_key_exists($value, $config)) { + $this->{$value} = $config[$value]; + } + } + + /* + * Custom applicators + */ if (isset($config['options'])) { $this->options($config['options']); } if (isset($config['span'])) { $this->span($config['span']); } - if (isset($config['context'])) { - $this->context = $config['context']; - } if (isset($config['size'])) { $this->size($config['size']); } @@ -265,42 +293,15 @@ class FormField if (isset($config['comment'])) { $this->comment($config['comment']); } - if (isset($config['placeholder'])) { - $this->placeholder = $config['placeholder']; - } if (isset($config['default'])) { $this->defaults = $config['default']; } - if (isset($config['cssClass'])) { - $this->cssClass = $config['cssClass']; - } if (isset($config['attributes'])) { $this->attributes($config['attributes']); } if (isset($config['containerAttributes'])) { $this->attributes($config['containerAttributes'], 'container'); } - if (isset($config['dependsOn'])) { - $this->dependsOn = $config['dependsOn']; - } - if (isset($config['trigger'])) { - $this->trigger = $config['trigger']; - } - if (isset($config['path'])) { - $this->path = $config['path']; - } - if (array_key_exists('required', $config)) { - $this->required = $config['required']; - } - if (array_key_exists('disabled', $config)) { - $this->disabled = $config['disabled']; - } - if (array_key_exists('hidden', $config)) { - $this->hidden = $config['hidden']; - } - if (array_key_exists('stretch', $config)) { - $this->stretch = $config['stretch']; - } if (isset($config['valueFrom'])) { $this->valueFrom = $config['valueFrom']; @@ -376,6 +377,7 @@ class FormField $position = strtolower($position); $attributes = $this->filterTriggerAttributes($attributes, $position); + $attributes = $this->filterPresetAttributes($attributes, $position); if ($position == 'field' && $this->disabled) { $attributes = $attributes + ['disabled' => 'disabled']; @@ -385,7 +387,7 @@ class FormField } /** - * Adds attributes used specifically by the TriggerAPI + * Adds attributes used specifically by the Trigger API * @param array $attributes * @param string $position * @return array @@ -425,6 +427,37 @@ class FormField return $attributes; } + /** + * Adds attributes used specifically by the Input Preset API + * @param array $attributes + * @param string $position + * @return array + */ + protected function filterPresetAttributes($attributes, $position = 'field') + { + if (!$this->preset || !is_array($this->preset) || $position != 'field') + return $attributes; + + $presetField = array_get($this->preset, 'field'); + $presetType = array_get($this->preset, 'type'); + + if ($this->arrayName) { + $fullPresetField = $this->arrayName.'['.implode('][', Str::evalHtmlArray($presetField)).']'; + } + else { + $fullPresetField = $presetField; + } + + $newAttributes = [ + 'data-input-preset' => '[name="'.$fullPresetField.'"]', + 'data-input-preset-type' => $presetType, + 'data-input-preset-closest-parent' => 'form' + ]; + + $attributes = $attributes + $newAttributes; + return $attributes; + } + /** * Returns a value suitable for the field name property. * @param string $arrayName Specify a custom array name