Overload $this->tags & $this->map in Apphelper

CakePHP is full of amazingly handy little bits and pieces, the trick is discovering them by exploring the API or paying attention to people who know Cake inside out.

AppHelper sits there begging to be filled, the idea being that you can overload core helper methods here; however there is a lot of debate as to whether or not this is actually a good approach and whether it is infact better (safer?) to overload helpers on a case by case basis (e.g. MyFormhelper extends FormHelper). (Personally I think it would be great to have the core helpers moved into base helpers so you can overload the core methods and still refer to them easily (e.g. HtmlHelper extends BaseHtmlhelper extends AppHelper, where HtmlHelper becomes an empty Class)

Two of the things I always have set in AppHelper are as follows:

1. Setup $this->map how I like it. $map defined in the Formhelper (cake/libs/views/helpers/form.php) is part of the auto magic behind forms. When you create a form in a view, Cake queries the model schema and figures out what sort of field it should generate for each database field.

Whilst I can see the reasoning behind the way Cake generates all the <select> fields, I would much rather have Cake genearte a simple <text> field and then use a JavaScript date picker to populate the field.

If you take a morning or a day to mess about and customise your bake templates then you can save vast amounts of time – if you want a date field why not just have it come to life with all the hooks for your Javascript there already? (A good introduction for custom baking is ad7six’s post.)

2. Redefine Tags. One of my real annoyances (and I know this is silly) is the fact that when Cake creates hidden fields it does not flag them with any specific class. This means that styles applied to other <input> fields can easily cascade down and cause odd little lines to appear all over your forms. Often you wouldn’t even notice this, but once you change the background colour of your form… all hell can break loose.

Tags are defined in /cake/libs/view/helpers/html.php so a quick and dirty solution is just to wrap a div with class around the hidden inputs so they can easily be distinguised css wise from all the other tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
class AppHelper extends Helper {
 
  /*
   * overload the default type mappings is /cake/libs/views/helpers/form.php FormHelper
   */
 
  var $map = array(
          'string'  => 'text',    
          'datetime'  => 'text',
          'boolean'  => 'checkbox',  
          'timestamp' => 'text',
          'text'    => 'textarea',  
          'time'    => 'time',
          'date'    => 'text', 
          'float' => 'text'
        );
 
 
   function __construct() {
        parent::__construct();
 
        /*
        * overload the default html tags /cake/libs/view/helpers/html.php
        */
        $this->tags['hidden'] = '<div class="hidden-input"><input type="hidden" name="%s" %s/></div>';
 
   }
}

If you are looking for other things to stick in your AppHelper then try Matt Curry’s app_helper url caching.