Custom functions in live template

The drupal has a lots of hook. The way need to implement it, {module_name}_{hook}.
For example, if the hook name is HOOK_form_alter(&$form, &$form_state, $form_id)
The HOOK need to change the filename without extension, within working. eg. in foo.module I need to kall function foo_form_alter(&$form, &$form_state, $form_id)

My goal would be, when typing _form_alter in foo.module, or foo.inc etc, then press tab, or trigger live template anyway, I get the next:

function foo_form_alter(&$form, &$form_state, $form_id) {

}



I see, there are funcions on live template variable like ClassName().
Is there any way to make my custom functions?

Using $SELECTION$ is cool, if not so much live template use this, but I plan to build templates all drupal hooks, it means hundreds of selectable value.
4 comments
Comment actions Permalink

Hi Gabor,

If you want to type _form_alter, press TAB and have full function declaration ready -- you will have to create separate template for each of such function as live template expands based on your typed characters ("_form_alter" will be completely replaced by your template content).

If all hooks have the same function signature (parameter names & order) then it may be better to use $SELECTION$ approach (e.g. type "form_alter" (notice no leading undersore), select it and invoke "Code | Suround With...").

To dynamically find out "module" name, you can apply fileNameWithoutExtension() functionto a template variable.


Example:
1. Create new live template at Settings | Templates
2. Use whatever Abbreviation and Description you like; Applicable in PHP
3. Use this code for Template Text

function $NAME$_$SELECTION$ (&$form, &$form_state, $form_id)
{
    $END$
}

4. Click "Edit Variables" button
5. Put fileNameWithoutExtension() into "Expression" column for NAME row; select "Skip if defined"
6. That's it -- start using it


Test time:
1) type form_alter or meow_yes
2) select it
3) invoke "Code | Surround With.." (Ctrl+Alt+T)
4) choose this live template

Is there any way to make my custom functions?

You cannot create your own (unless, maybe, you will code it as separate plugin -- not sure). But you can always file a Feature Request on Issue Tracker for that and if such function is really usefull (can be widely used / quite universal (not limited to one language / tehnology only) then it can be implemented in near version...)

0
Comment actions Permalink

This sounds good, the only one, what I need, the fileNameWithoutExtension().
How can I see, or create it? I didn't find them under Expression. I'm using the 3.0.3 storm.
Maybe in 4.x-beta?
Selection_179.png

0
Comment actions Permalink

Yes, it may not be available in v3 as I have created this live template for you in latest v4.0.1 RC (build 117.257) -- http://confluence.jetbrains.net/display/WI/Web+IDE+EAP

0
Comment actions Permalink

Thanks, I'll try soon.

0

Please sign in to leave a comment.