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
johnJune 23, 2019, 9:07 p.m.

You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site voyance amour eternel 0892 22 20 22 voyance par telephone

Reply
Golalai IsmaeelJune 24, 2019, 11:56 a.m.

Impressive and informative as well. Try cell phone and computer spying app to keep an eye on kids, teens and employees cell phones and computer devices activities.

Reply
fdnJune 25, 2019, 10:05 a.m.

粉嫩公主酒酿蛋是一款怎样的产品呢,粉嫩公主酒酿蛋是丰胸产品清朝民间流传下来的一款丰胸产品,主要以天然能量为粉嫩公主酒酿蛋丰胸效果诉求,经过现代的科技改良后,以实用和实效为特点,是一款100%纯植物提取的美胸品牌。采用的粉嫩公主酒酿蛋丰胸都是天然有机食材:酒酿原汁、土鸡蛋、珍珠糯米、姜汁红糖、泰国野葛根提取物,真正做到健康、安全、便携、高效的丰胸效果口碑产品。粉嫩公主酒酿蛋丰胸解开女人的幸福密码!

Reply
kevinJune 25, 2019, 6:16 p.m.

I ‘d mention that most of us visitors are really endowed to exist in a fabulous place with very many wonderful individuals with very helpful things. nadia nakai naked download

Reply
liamJune 26, 2019, 7:06 p.m.

Thanks for sharing this quality information with us. I really enjoyed reading. Will surely going to share this URL with my friends odzyskiwanie danych Szczecin

Reply
kevinJune 27, 2019, 11:10 a.m.

Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks, I’ll probably keep it short and sweet and link to this instead if thats cool. Thanks https://www.digitekprinting.com/

Reply
goodboyJuly 14, 2019, 5:41 a.m.

Go www.office.com/setup for Microsoft office 365, Office 2016 product key & onedrive for business in UK are require to run Microsoft account. office.com/setup office setup enter product key for Microsoft Office install office 365 personal with product key is specially designed

Reply
goodboyJuly 16, 2019, 5:26 a.m.

CCTV Camera price in Bangladesh from CCTV Camera Company in Dhaka, Bangladesh. Shop now, CCTV Camera Package for Dahua Camera, Avtech CCTV, Wifi Spy camera

Reply
preeti royJuly 17, 2019, 5:12 a.m.

`I am not sensitive here and for this remarkable article. I have got a heap of stupid data for my understanding here, which I need. Every one objective www.delhi-escorts.in/ behind the interest that you oblige us, it was particularly prominent and fulfilling, the commitment of praise to share this amazing post is all around. www.sonalisharma.co.in/

Reply
aliya aroraJuly 17, 2019, 5:13 a.m.

I only say thanks to the author of this amazing article for provide us such a unique and informative article. I am waiting for too long for this great article and i finally found from your website. Thanks. http://priyadelhiescorts.in

Reply
Pinky BansalJuly 17, 2019, 10 a.m.

To an awesome degree revered taking a gander at your blog. It was to a remarkable degree especially made and clear. A devotion of appreciation is all together for your incredible exertion and I was sitting tight for this post. I am fulfilled to analyze this sort of astounding lighting up post. http://www.yashnaescorts.co.in/

Reply
john mickJuly 19, 2019, 9:16 a.m.

mprove however despite everything I say this

Reply
Taniya KhanJuly 19, 2019, 6:11 a.m.

This is an informative post review. I am so pleased to get this post article and nice information. Thanks for sharing with us. Delhi Call Girls || Call Girls in Delhi || Delhi Escorts || Delhi Call Girls|| Call Girls in Delhi|| Delhi Escorts Service || Delhi Call Girls

Reply
goodboyJuly 29, 2019, 2:57 p.m.

Best CCTV Camera price in Bangladesh are provided by Credible Technology BD. We are the best CCTV camera company in Bangladesh, also we are CC Camera solution provider in Dhaka, Bangladesh. Hire us for your CCTV Camera installation and wireless security camera systems solution, if you are looking for best outdoor wireless security camera system with NVR or security camera with DVR along with Dome CCTV Camera, Bullet CCTV Camera, etc. We are offering the best CCTV Cameras price by all renowned CCTV cameras brands with affordable price in Bangladesh. Select us to quote you best Security solutions price by Dahua CCTV Cameras, jovision IP Camera, Hikvision Camera for IP Camera, Dome camera, Bullet Camera, PTZ Camera, Security cameras, Wifi camera, Spy camera & wireless camera. Check C C Camera price list before purchase security services.

Reply
living room decorAug. 8, 2019, 10:06 p.m.

I haven't any word to value this post.....Really i am awed from this post....the individual who make this post it was an extraordinary human..thanks for imparted this to us.

Reply
jakdownAug. 8, 2019, 10:06 p.m.

The web site is lovingly serviced and saved as much as date. So it should be, thanks for sharing this with us. living room decor

Reply
goodboyAug. 23, 2019, 12:11 p.m.

You definitely want your drinking water to be clean, fresh and cool (or hot), and available all the time. Broad Mag

Reply
jakdownAug. 23, 2019, 9:54 p.m.

I haven't any word to value this post.....Really i am awed from this post....the individual who make this post it was an extraordinary human..thanks for imparted this to us. flyer delivery

Reply
arpita yadavAug. 29, 2019, 1:22 p.m.

I am happy to find this post undaunted for me. as it contains store of information. I all around prefer to explore the quality substance and this thing I found in you post. Grateful for sharing. Escort Jaipur Escorts in Jaipur Escort in Jaipur Jaipur Escorts Service Escorts Service in Jaipur

Reply
mansiAug. 29, 2019, 1:23 p.m.

All the substance you referenced in post is idiotically staggering and can be particularly useful. I will survey it. an obligation of thankfulness is all together for sharing the information keep reviving. analyzing forward for more posts.Thanks. Escort Jaipur Escorts in Jaipur Escort in Jaipur Jaipur Escorts Service Escorts Service in Jaipur

Reply
kajalAug. 29, 2019, 1:24 p.m.

Your blog gave us fundamental information to work with. Each and each tip of your post are awesome. Much restoring for sharing. Keep blogging. Escorts in Jaipur Escort in Jaipur Jaipur Escorts Service Escorts Service in Jaipur Jaipur Call Girls

Reply
poonamAug. 29, 2019, 1:25 p.m.

Interesting subject for a blog. I have been searching for after the Internet down fun and wager upon your site page. Excellent post. Grateful a ton for sharing your observation! It is stunning to see that couple of individuals still put an effort into managing their zones. I'll attempt to return again veritable soon. Escorts in Jaipur Escort in Jaipur Jaipur Escorts Service Escorts Service in Jaipur
Jaipur Call Girls

Reply
mtomSept. 3, 2019, 3:29 p.m.

Thanks for sharing this information. I found this information very useful. That’s a good blog post. I will return to read more posts. Barber

Reply
aliya aroraSept. 4, 2019, 6:18 a.m.

great! Much obliged! much obliged to you for sharing. Your blog entries are all the more intriguing and instructive. We are additionally a decent specialist co-op. Snap underneath to appreciate incredible administration in different areas. Delhi Escorts Escorts in Delhi Delhi Escorts

Reply
aliya aroraSept. 4, 2019, 6:19 a.m.

I just express profound gratitude to the writer of this astounding article for give us such a remarkable and useful article. I am sitting tight for a really long time for this incredible article and I at long last found from your site. Much obliged. Escorts in Delhi Delhi Escorts

Reply
Sumitsingh72Sept. 10, 2019, 12:06 p.m.

Moving is no More a Difficult Errand with Movers and Packers Delhi approximate moving cost calculator , local packers and movers Pune , packers and movers Pune cost

Reply
jaipur escortsSept. 10, 2019, 3:02 p.m.

I love polite guys and if I similar to you then you will obtain an memorable erotic experience this time.@ I am an elite behavior and you can appoint me as your dealing friend for a party. Jaipur Escort

Agra Escort Service

Lucknow Escorts

Dehradun Escorts

Ludhiana Escorts service

Jaipur Escort High ProfileCall Girls In Jaipur

Jalandhar Escort Service

Reply
jakdownSept. 15, 2019, 2:33 p.m.

Photo Editing HQ is the name of a pioneer Online Photo editing service provider company. Established with an aim to provide ultimate professional Clipping Path Service, Image Masking Service, Image Editing , Image Masking Service , Image Retouching Service & so on with lowest possible price & within shortest possible turnaround. We have vast experience in editing event photos, product photos, real estate photos and so on. We provide ecommerce photo editing, Deep Etching, Cut Out Image and cheap retouching service for photographer. Photo Editing Service

Reply
mtomSept. 16, 2019, 8:52 a.m.

I real pleased to find this web site on bing, just what I was searching for as well saved to bookmarks . Beards

Reply
arpita yadavSept. 20, 2019, 12:07 p.m.

Nice to visit your blog again. it has been a long centrality for me. Everything considered this article I've been held tight for so long. I need this article to complete my undertaking in school. in like way, it has the same point with your article. Much regarded. shocking offer. Ajmer Escorts Escorts in Pushkar Jaisalmer Escorts Service Udaipur Call Girls Call Girls in Salasar

Reply
manishaSept. 20, 2019, 12:08 p.m.

This is a basic post. I like this topic. This site has social gatherings of advantage. I discovered unequivocal surprising things from this site. It causes me in various ways. Thanks for posting this over once more. Escorts in Malviya Nagar Vaishali Nagar Escorts Service Sanganer Call Girls
Call Girls in Durgapura Barkat Nagar Call Girls Service

Reply
rituSept. 20, 2019, 12:09 p.m.

This is a totally site post. Not various people would truly. the way wherein where you basically did. I am really begun that there is such a huge amount of information about this subject have been uncovered and you've moved a valiant exertion. Civil Lines Escorts Service Lal Kothi Call Girls Call Girls in Rewari
Nasirabad Call Girls Service Mansarovar Escorts

Reply
poojaSept. 20, 2019, 12:11 p.m.

Thanks for sharing the post.. affirms are universes best individual in each closeness of individual..they need or should win to help needs of the family.

Jagatpura Call Girls Call Girls in Gopalpura Ghat Darwaza Call Girls Service
Chandpole Escorts Escorts in Bhiwadi

Reply
ektaSept. 20, 2019, 12:11 p.m.

Awesome and boggling article. Boundless things you've perseveringly regarded us. Much regarded. In a general sense continue making this sort out of post. Call Girls in Bhankarota Beawar Call Girls Service Bani Park Escorts
Escorts in Ambabari Alwar Escorts Service

Reply
swatiSept. 20, 2019, 12:12 p.m.

Wow, I can say this is another surprising article unequivocally verifiably absolutely exactly as expected of this blog. Bookmarked this page. New Colony Call Girls Service Kishangarh Escorts Escorts in Gangori Bazar Bajaj Nagar Escorts Service
Bais Godam Call Girls

Reply
sonamSept. 20, 2019, 12:13 p.m.

Its an unending delight exploring your post. Jaipur Escort Service It is stacked with the information I am checking for and I need to post a comment that "The substance of your post is dazzling" Great work.

Reply
goodboySept. 22, 2019, 9:29 a.m.

Windows 10 pro is the latest version of the popular operating system windows 10 pro key

Reply
Taniya KhanSept. 24, 2019, 7:04 a.m.

Have been looking for information on the subject! thanks to the author for the perfectly presented material! Escorts in Gurgaon || Call Girls in Gurgaon || Gurgaon Call Girls

Reply
jakdownSept. 24, 2019, 8:56 p.m.

You finished a couple fine focuses there. I did an inquiry on the subject and discovered almost all persons will oblige with your online journal. voyance-amour-eternel.com

Reply
mtomSept. 26, 2019, 3:21 p.m.

Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Basically Magnificent. I am also a specialist in this topic therefore I can understand your effort. 안전놀이터

Reply
mtomSept. 29, 2019, 2:23 p.m.

When I originally commented I clicked the -Notify me when new surveys are added- checkbox and from now on when a comment is added I receive four emails with the exact same comment. Can there be however you can get rid of me from that service? Thanks! black friday sverige


There are a couple of interesting points in time on this page but I don’t determine if I see them all center to heart. There exists some validity but I’ll take hold opinion until I consider it further. Great post , thanks and now we want a lot more! Included in FeedBurner at the same time distance entre 2 villes en voiture

Reply
Jayden PlastererOct. 3, 2019, 12:42 p.m.

Howdy, it was a truly superb adventure for me when I dropped by at your internet site. I just needed to applaud you on the high quality of the work you've put in here and additionally to send you bon chance with it as you proceed in the coming days. It definitely was a hoot to browse your blog and I'll most definitely be calling back again shortly to find out exactly how you're developing. Cheers and hopefully I shall no doubt see you here once again soon - Jayden Plasterer

Reply
IDN PokerOct. 11, 2019, 4:02 p.m.

Kronospoker Situs Judi Online, IDN Poker, IDN Poker Mobile, Agen Poker Terpercaya Indonesia untuk permainan Poker Online, Domino Qiu Qiu, Ceme Keliling, Capsa Susun dan Super10

Reply
Mumbai EscortsOct. 21, 2019, 5:15 a.m.

Welcome to Mumbaihotcollection.com, home of the finest Mumbai escorts in Mumbai area. Our Mumbai escorts are the finest Mumbai has to offer and will provide an unforgettable Mumbai Escort experience! Escorts in mumbai Escort service in mumbai Mumbai escorts Mumbai call girls Call girls in mumbai Escorts mumbai

Reply

Post New Comment