Drupal Custom Elements API Errors

Posted on July 29, 2013

The Drupal Form API is powerful, but very extensive and complicated. In another post we discussed how to add a custom element from scratch. Watch out for the errors and mistakes highlighted in this advanced-level post.

Element and Field Defaults

To fully understand the API, you need to understand how elements are built. When you define a form field, custom or not, you have to give it a #type.

When the form is being built, Drupal will look at the #type and decide what to do with it. If it's a custom element you've defined, Drupal will look at the hook_element_info() that defined that custom element, and grab all the information from that array. That is, keys and values in that array will be merged into your field. This allows you to set defaults for that element #type. Let's look at an example. For simplification, let's define a basic custom element type:

1
2
3
4
5
6
7
8
<?php

function example_element_info() {
  $elements['custom_element'] = array(
    // Element declaration here ...
  );
  return $elements;
}

Note that this element won't do much. But now, let's use it in a form:

1
2
3
4
5
6
7
8
<?php

function example_some_form() {
  $form['my_field'] = array(
    '#type' => 'custom_element',
  );
  return $form;
}

If we want each usage of the element to, by default, have certain properties, we can just add it to the hook_element_info() function. So doing this:

1
2
3
4
5
6
7
8
9
<?php

function example_element_info() {
  $elements['custom_element'] = array(
    // Rest of element declaration here ...
    '#some_property' => 'some_property',
  );
  return $elements;
}

Will mean that all uses of your element in forms would be as if you had declared the form like so:

1
2
3
4
5
6
7
8
9
<?php

function example_some_form() {
  $form['my_field'] = array(
    '#type' => 'custom_element',
    '#some_property' => 'some_property',
  );
  return $form;
}

In other words, #some_property will "bubble" up from the element declaration to the actual element useful.

Learning Drupal? Subscribe to my Drupal articles, tips and tutorials.

This means you can do powerful things, like declaring default #theme functions, default #prefix and #suffix content, etc.

To summarize: the form declaration for that element will get merged to the properties defined in hook_element_info(). So keep this in mind when defining your custom elements.

Hook Theme and Render Element

So you've defined your custom element, but now want to control its appearance. Easy right? Just give it a #theme function in hook_element_info(). Well, it's not that simple.

You might notice it's not rendering now or ignoring your theme function. So you go and add that function to the theme registry, using hook_theme().

But now we have another problem: our theme function isn't being called with the right arguments. That's because when defining this function in hook_theme(), you need to use the render element property, rather than your typical variables property. As follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php

// DO THIS
function example_theme() {
  return array(
    'gitbucket_field' => array(
      'render element' => 'element',
    ),
  );
}

// NOT THIS
function example_theme() {
  return array(
    'gitbucket_field' => array(
      'variables' => array('element' => NULL),
    ),
  );
}

And there you go: two big secrets about custom Drupal elements. With this knowledge on hand, you should be able to create all the custom elements you'll need. Without surprises.

Comments

Jacob archApril 1, 2019, 9:15 p.m.

Starting with Drupal 8.5.1, it’s now reasonable to impair inline form errors for particular forms (only appropriate when the Inline Form Errors module is enabled). do my assignment free

Reply
AllenMay 7, 2019, 9:19 a.m.

Excellent post! I just listen this complete session which I get unique information that's valuable for me and I must share it on my profile. Now I wanna to meet with essay writer service who are well qualified and deliver quality papers.

Reply
BeonymoMay 16, 2019, 3:12 a.m.

Array is like a variable but it contains value in listed form. As one variable contain one value but if we take notice of writing done in http://www.topratedessayservice.com/ programming field we judge that array contains many values that are assigned to only one variable.

Reply
fggfggfMay 18, 2019, 2:05 p.m.

Préparez soigneusement tous les articles de vente pouvant être nécessaires pendant le processus de vente, ce qui constituera sans aucun doute une base solide pour le succès de vos ventes. replique audemars piguet Parce qu'une préparation minutieuse des outils de vente peut non seulement laisser les clients ressentir la sincérité des vendeurs, mais aussi les aider à établir une bonne image et à créer une atmosphère de négociation amicale, harmonieuse et détendue, propice au bon développement de la promotion des ventes.

Reply
ScottJune 7, 2019, 5 p.m.

I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that's at the other blogs. I appreciate your sharing this best posting. Best Home Improvement Loans

Reply
kevinJune 8, 2019, 9:07 p.m.

If you have any confusion about pdf merging then go to the following link https://fr.altomerge.com/faq. It has all the frequently asked questions about merging pdf files.

Reply
rickJune 13, 2019, 4:51 p.m.

Thanks for sharing this quality information with us. I really enjoyed reading. Will surely going to share this URL with my friends fidelitydating.com/make-him-worship-you-review

Reply
muhammad zeeshanJune 13, 2019, 7:19 p.m.

Thank you for this. Really nice piece of work. Creation of backlinks made easy because of you. Thanks! All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks

maninerd

maniwebify

almillat

hitscorner

Reply
carlJune 14, 2019, 12:45 p.m.

My friends have asked me a lot of times about this subject, and now I have ample information. I can even write my essay reviews now, and that wasn't possible before. I want to thank the author who wrote this.

Reply
carlJune 16, 2019, 10:28 a.m.

Thanks so much with this fantastic new web site. I’m very fired up to show it to anyone. It makes me so satisfied your vast understanding and wisdom have a new channel for trying into the world. Welcome to Betta Fish Top

Reply
davidJune 17, 2019, 7:07 p.m.

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates. bank nifty share price

Reply

Post New Comment