Add a custom element to Drupal

31 Oct 2012

It is fairly simple.

First of all, you must define the new element's type with this hook `hook_element_info()`.

For example:

function something_element_info() { return array( "multilevel" => array( "#input" => TRUE, '#default_value' => "", ), ); }

Now let's say the element is a custom HTML. What would you do?

You need to know that Drupal doesn't know `multilevel`, it will render emptiness.

But we can add our own HTML into `#prefix`.

What I did is that I added a pre-render hook for this element. And within the pre-render hook, I assign `#prefix` to the element.

That's easy, huh?