menu
書いてる野郎
orebike@gmail.com
FuelPHP1.7
通常は Field クラスにはテンプレートを含んだ出力をするメソッドしか無い、内部でも分離して記述されていない。不便なので拡張する。
build メソッドをほとんどそのままコピペして、最後にtemplateに渡す部分を除去する。
このメソッドは View 内で使われることを想定しているのでその出力時点でJavaScriptやCSS用の id や class や各種属性を取得したいので、その場で付けられるように引数で取ることにした。
class Fieldset_Field extends \Fuel\Core\Fieldset_Field { public function form_field($attributes = array()) { $form = $this->fieldset()->form(); foreach ($attributes as $attr => $val) { $this->set_attribute($attr, $val); } // Add IDs when auto-id is on if ($form->get_config('auto_id', false) === true and $this->get_attribute('id') == '') { $auto_id = $form->get_config('auto_id_prefix', '') .str_replace(array('[', ']'), array('-', ''), $this->name); $this->set_attribute('id', $auto_id); } switch( ! empty($this->attributes['tag']) ? $this->attributes['tag'] : $this->type) { case 'hidden': $build_field = $form->hidden($this->name, $this->value, $this->attributes); break; case 'radio': case 'checkbox': if ($this->options) { $build_field = array(); $i = 0; foreach ($this->options as $value => $label) { $attributes = $this->attributes; $attributes['name'] = $this->name; $this->type == 'checkbox' and $attributes['name'] .= '['.$i.']'; $attributes['value'] = $value; $attributes['label'] = $label; if (is_array($this->value) ? in_array($value, $this->value) : $value == $this->value) { $attributes['checked'] = 'checked'; } if( ! empty($attributes['id'])) { $attributes['id'] .= '_'.$i; } else { $attributes['id'] = null; } $build_field[$form->label($label, null, array('for' => $attributes['id']))] = $this->type == 'radio' ? $form->radio($attributes) : $form->checkbox($attributes); $i++; } } else { $build_field = $this->type == 'radio' ? $form->radio($this->name, $this->value, $this->attributes) : $form->checkbox($this->name, $this->value, $this->attributes); } break; case 'select': $attributes = $this->attributes; $name = $this->name; unset($attributes['type']); array_key_exists('multiple', $attributes) and $name .= '[]'; $build_field = $form->select($name, $this->value, $this->options, $attributes); break; case 'textarea': $attributes = $this->attributes; unset($attributes['type']); $build_field = $form->textarea($this->name, $this->value, $attributes); break; case 'button': $build_field = $form->button($this->name, $this->value, $this->attributes); break; case false: $build_field = ''; break; default: $build_field = $form->input($this->name, $this->value, $this->attributes); break; } if (empty($build_field) or $this->type == 'hidden') { return $build_field; } return $build_field; } }
こいつを
fuel/app/classes/fieldset/field.php
として保存する。
そして
fuel/app/bootstrap.php
に、このように追記する。
Autoloader::add_classes(array( // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'Fieldset_Field' => APPPATH . 'classes/fieldset/field.php', ));
これで core の Fieldset クラスが Field クラスを使うときに今実装した版の Field クラスを使ってくれることになる。
core Fieldset を継承して拡張しても結局その親が扱うクラスは core の Field クラスになる。 なので Fieldset を拡張する場合は単に継承して使えばいいだけだが Field を拡張したい場合はこのようにして core の Field を取り替える記述をする必要がある。