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.


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'] = '
'; } }

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

Wings of Desire

Wing on the way back from Spain, April 2009

I always wish aeroplane windows were a bit bigger, and am unable to understand the people who do not stare out of them as you take of and land. It doesn’t matter how many times you have flown, people who just read the in-flight magazine must have dead imaginations.

Hannah Storie, I need your help.

Affect. Effect. I can never remember what the difference between them is. This really bugs me, so much so that this week I actually when seeking an answer. I asked Wolfram Alpha; it was able to tell me the population of the town where I live; it was able to tell me how many angels you can fit on the head of a pin, but it was unable to help me with Affect and Effect.

Eventually Grammar Girl helped me out with a picture of an Aardvark. I still felt it was not enough. I found this:

affect is the act
effect is the result

(here)

That is the best so far, it is a functional mnemonic, but I feel it lack a certain something. Hannah, where are you? Whenever Ben and I ran into gramatical conubdrums you were always there to help with a handy diagram we could pin up on the notice board.