Skip to content

Commit

Permalink
Support ftype default defs
Browse files Browse the repository at this point in the history
  • Loading branch information
bmidget committed Mar 28, 2014
1 parent e3ffa22 commit 216e335
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
4 changes: 3 additions & 1 deletion classes/Formo/Core/Formo.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function __construct( array $array = NULL)

$array = $this->_resolve_construct_aliases($array);

$this->_setup_from_ftype($array);

foreach ($array as $key => $value)
{
$this->set($key, $value);
Expand Down Expand Up @@ -1448,7 +1450,7 @@ public function set($var, $val = NULL)
{
$field['parent'] = $this;
$new_field = Formo::factory($field);
$this->_fields[] = $new_field;
$this->add($new_field);
}

return $this;
Expand Down
60 changes: 60 additions & 0 deletions classes/Formo/Core/Innards.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ abstract class Formo_Core_Innards {
*/
protected $_editable = true;

/**
* Used for auto-loading config defaults
*
* @var string
* @access protected
*/
protected $_ftype;

/**
* HTML instide a tag
*
Expand Down Expand Up @@ -995,6 +1003,58 @@ protected function _set_id( array & $array)
}
}

/**
* Default field definitions from config file
*
* @access protected
* @param array & $array
* @return void
*/
protected function _setup_from_ftype( array $array)
{
$config = Kohana::$config->load('formo.ftype');
if (empty($config))
{
// No need to continue if there aren't any definitions
return;
}

$driver = Arr::get($array, 'driver', $this->_driver);
$ftype = Arr::get($array, 'ftype', $this->_ftype);
$parent = Arr::get($array, 'parent', $this->_parent);
$parent_ftype = ($parent)
? $parent->get('ftype')
: null;

$config_path = null;

// Build the config path
if ($parent)
{
$config_path.= $parent_ftype
? $parent_ftype
: ':all';

$config_path.= $ftype
? '.ftype.'.$ftype
: '.driver.'.$driver;
}
else
{
$config_path.= $ftype
? $ftype.':self'
: ':all.:self';
}

if ($arr = Arr::path($config, $config_path))
{
// If the config path was found, set all the defaults
$this->set($arr);
}

return;
}

/**
* Set the field's value
*
Expand Down
1 change: 1 addition & 0 deletions config/formo.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@
array('Formo::range', array(':field', ':form')),
),
),
'ftype' => [],
);

0 comments on commit 216e335

Please sign in to comment.