Status message

Maintenant, vous regardez: Create a node type programmatically

Create a node type programmatically

Example of writing a node module that lets users add a job posting to a site. A job posting node will include a
title, a body where the details of the job posting will be entered, and a field where the user can enter the
name of the company. For the job posting title and a body, I’ll use the built-in node title and body that
are standard with all Drupal nodes. I’ll need to add a new custom field for the company’s name.
I’ll start by creating a folder named job_post in your sites/all/modules/custom directory.

1. job_post.info file:

  1. name = Job Post
  2. description = A job posting content type
  3. package = French
  4. core = 7.x
  5. files[] = job_post.install
  6. files[] = job_post.module

2. job_post.install
The install file for the job post module performs all of the set-up operations for things like defining the
node type, creating the fields that make up our new node type, and handling the uninstall process when
an administrator uninstalls the module.

  1. /**
  2. * @file
  3. * Install file for Job Post module.
  4. */
  5. /**
  6. * Implements hook_install().
  7. * - Add the body field.
  8. * - Configure the body field.
  9. * - Create the company name field.
  10. */
  11. function job_post_install() {
  12. node_types_rebuild();
  13. $types = node_type_get_types();
  14. // add the body field to the node type
  15. node_add_body_field($types['job_post']);
  16. // Load the instance definition for our content type's body
  17. $body_instance = field_info_instance('node', 'body', 'job_post');
  18. // Configure the body field
  19. $body_instance['type'] = 'text_summary_or_trimmed';
  20. // Save our changes to the body field instance.
  21. field_update_instance($body_instance);
  22. // Create all the fields we are adding to our content type.
  23. foreach (_job_post_installed_fields() as $field) {
  24. field_create_field($field);
  25. }
  26.  
  27. // Create all the instances for our fields.
  28. foreach (_job_post_installed_instances() as $instance) {
  29. $instance['entity_type'] = 'node';
  30. $instance['bundle'] = 'job_post';
  31. field_create_instance($instance);
  32. }
  33. }
  34.  
  35. /**
  36. * Return a structured array defining the fields created by this content type.
  37. * For the job post module there is only one additional field – the company name
  38. * Other fields could be added by defining them in this function as additional elements
  39. * in the array below
  40. */
  41.  
  42. function _job_post_installed_fields() {
  43. $t = get_t();
  44. return array(
  45. 'job_post_company' => array(
  46. 'field_name' => 'job_post_company',
  47. 'label' => $t('Company posting the job listing'),
  48. 'type' => 'text',
  49. ),
  50. );
  51. }
  52.  
  53. /**
  54. * Return a structured array defining the field instances associated with this content type.
  55. */
  56. function _job_post_installed_instances() {
  57. $t = get_t();
  58. return array(
  59. 'job_post_company' => array(
  60. 'field_name' => 'job_post_company',
  61. 'type' => 'text',
  62. 'label' => $t('Company posting the job listing'),
  63. 'widget' => array(
  64. 'type' => 'text_textfield',
  65. ),
  66. 'display' => array(
  67. 'example_node_list' => array(
  68. 'label' => $t('Company posting the job listing'),
  69. 'type' => 'text',
  70. ),
  71. ),
  72. ),
  73. );
  74. }
  75.  
  76. /**
  77. * Implements hook_uninstall().
  78. */
  79. function job_post_uninstall() {
  80. // Gather all the example content that might have been created while this
  81. // module was enabled.
  82. $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  83. $result = db_query($sql, array(':type' => 'job_post'));
  84. $nids = array();
  85. foreach ($result as $row) {
  86. $nids[] = $row->nid;
  87. }
  88. // Delete all the nodes at once
  89. node_delete_multiple($nids);
  90.  
  91. // Loop over each of the fields defined by this module and delete
  92. // all instances of the field, their data, and the field itself.
  93. foreach (array_keys(_job_post_installed_fields()) as $field) {
  94. field_delete_field($field);
  95. }
  96. // Loop over any remaining field instances attached to the job_post
  97. // content type (such as the body field) and delete them individually.
  98. $instances = field_info_instances('node', 'job_post');
  99. foreach ($instances as $instance_name => $instance) {
  100. field_delete_instance($instance);
  101. }
  102. // Delete our content type
  103. node_type_delete('job_post');
  104. // Purge all field infromation
  105. field_purge_batch(1000);
  106. }

3.job_post.module
Last, you need the module file itself. Create a file named job_post.module, After you’ve completed the module, you can enable the module
on the module listings page (Modules). You begin with the opening PHP tag and Doxygen comments.

  1. /**
  2. * @file
  3. * This module provides a node type called job post
  4. */
  5.  
  6. /**
  7. * Implements hook_node_info() to provide our job_post type.
  8. */
  9. function job_post_node_info() {
  10. return array(
  11. 'job_post' => array(
  12. 'name' => t('Job Post'),
  13. 'base' => 'job_post',
  14. 'description' => t('Use this content type to post a job.'),
  15. 'has_title' => TRUE,
  16. 'title_label' => t('Job Title'),
  17. 'help' => t('Enter the job title,
  18. job description, and the name of the company that posted the job'),
  19. ),
  20. );
  21. }

A single module can define multiple node types, so the return value should be an array. Here’s the
breakdown of metadata values that may be provided in the node_info() hook:
"name": The human-readable name of the node type. Required.
"base": The base string used to construct callbacks corresponding to this node
type (i.e., if base is defined as example_foo, then example_foo_insert will be
called when inserting a node of that type). This string is usually the name of the
module, but not always. Required.
"description": A brief description of the node type. Required.
"help": Help information shown to the user when creating a node of this type.
Optional (defaults to '').
"has_title": Boolean indicating whether this node type has a title field. Optional
(defaults to TRUE).
"title_label": The label for the title field of this content type. Optional (defaults
to “Title”).
"locked": Boolean indicating whether the administrator can change the
machine name of this type. FALSE = changeable (not locked), TRUE =
unchangeable (locked). Optional (defaults to TRUE).

Tags:

Comments

http://tribtaiti.webcindario.com/ good canadian online pharmacies
canadian online pharmacies legitimate http://tribtaiti.webcindario.com/
canada online pharmacy http://tribtaiti.webcindario.com/

It's a shame you don't have a donate button! I'd most certainly donate

to this brilliant blog! I guess for now i'll settle for book-marking and adding your

RSS feed to my Google account. I look forward to new updates and will talk about this website with my Facebook group.

Talk soon!

Also visit my site hosting services

Wonderful, what a website it is! This webpage gives valuable data to us, keep it up.

Also visit my website; best web hosting 2020

https://canadianhpharmacy.com/ trust pharmacy of canada
canadian pharmacies without an rx https://canadianhpharmacy.com/
canada medication list https://canadianhpharmacy.com/

Highly descriptive article, I enjoyed that a lot.

Will there be a part 2?

My blog post cheap flights

https://viagrawwithoutdoctor.com/ online canadian pharmacy
the best canadian online pharmacies https://viagrawwithoutdoctor.com/
drugstore online shopping reviews https://viagrawwithoutdoctor.com/

I love what you guys are usually up too. This type of clever work and coverage!

Keep up the great works guys I've included you guys to blogroll.

Feel free to surf to my web-site - black mass

Great web site you have got here.. It's difficult cheap flights to hawaii find high quality writing like yours these days.

I seriously appreciate individuals like you! Take care!!

Thanks for finally talking about > Add new comment

| Moi Verhole < Liked it!

Look into my site :: v bucks free

Wonderful work! That is the type of info that

are supposed to be shared across the net.

Shame on the search engines for now not positioning this submit higher!

Come on over and seek advice from my website .

Thanks =)

Feel free to surf to my web site - free v bucks

https://canadianhpharmacy.com/ canada pharmacies online prescriptions
canada medication list https://canadianhpharmacy.com/
northwest pharmacy canada https://canadianhpharmacy.com/

You are so cool! I do not believe I've truly read through a single thing like this

before. So nice to find another person with original thoughts on this

topic. Really.. thanks for starting this up. This website is something that's needed on the

web, someone with some originality!

my page :: black mass

https://viagracwithoutdoctor.com/ canadian government approved pharmacies
how safe are canadian online pharmacies https://viagracwithoutdoctor.com/
canadian pharmacys https://viagracwithoutdoctor.com/

https://canadianhpharmacy.com/ canadian medications list
canada pharmacies account https://canadianhpharmacy.com/
canadian online pharmacies https://canadianhpharmacy.com/

https://viagrawwithoutdoctor.com/ canada pharmaceuticals online
pharmacy canada online prescriptions https://viagrawwithoutdoctor.com/
canadian pharmacies shipping to usa https://viagrawwithoutdoctor.com/

https://viagracwithoutdoctor.com/ most reliable canadian online pharmacies
pharmacy https://viagracwithoutdoctor.com/
aarp recommended canadian pharmacies https://viagracwithoutdoctor.com/

Magnificent items from you, man. I've consider your stuff previous to and you are simply too excellent.

I really like what you've got here, certainly like what you're saying and the way by which you are saying it.

You make it enjoyable and you continue to care for to stay it wise.

I can not wait to learn much more from you.

This is actually a terrific site.

Feel free to visit my web-site ... v bucks free

Nice replies in return of this issue with genuine arguments and describing everything concerning that.

My web site free the vbucks

https://canadianlpharmacy.com/ northwest pharmacies mail order
order canadian prescriptions online https://canadianlpharmacy.com/
drugs for sale usa https://canadianlpharmacy.com/

Nice post. I was checking continuously this blog and I

am impressed! Very helpful information specially the ultimate section :)

I maintain such information much. I used to be seeking this particular info for a very long time.

Thank you and good luck.

my page: find cheap flights

If some one desires expert view about blogging

and site-building after that i recommend him/her to visit this webpage, Keep up

the good work.

Visit my web blog v bucks free generator

https://viagrawwithoutdoctor.com/ canadian prescription drugstore
canadian government approved pharmacies https://viagrawwithoutdoctor.com/
canada medication prices https://viagrawwithoutdoctor.com/

Do you mind if I quote a few of your posts as long as

I provide credit and sources back to your weblog?

My blog site is in the exact same area of interest

as yours and my visitors would certainly benefit from a lot of the information you present here.

Please let me know if this ok with you. Thanks a lot!

Feel free to visit my web site: free vbucks generator

https://canadianhpharmacy.com/ canadian discount pharmacies in canada
pharmacy https://canadianhpharmacy.com/
canadian pharmaceuticals reviews https://canadianhpharmacy.com/

That is very attention-grabbing, You're an overly professional blogger.

I have joined your rss feed and stay up for in search of extra of your wonderful post.

Additionally, I have shared your web site in my social networks

Feel free to surf to my webpage many g

I think what you published made a lot of sense.

But, what about this? suppose you were to create a killer headline?

I ain't saying your information isn't good, however what if you added a

post title that makes people desire more? I mean Add new comment

| Moi Verhole is kinda vanilla. You ought to peek at Yahoo's home page and watch how they

create post headlines to grab people interested. You might

add a video or a picture or two to get people excited about what you've written.

Just my opinion, it would bring your blog a little

bit more interesting.

Also visit my web-site :: free vbucks

Yesterday, while I was at work, my sister stole my iPad and tested to see if it can survive a 25 foot

drop, just so she can be a youtube sensation. My apple ipad is now broken and she has

83 views. I know this is entirely off topic but I had to share it with someone!

Visit my web page - senzori pescuit

Hey terrific blog! Does running a blog like this require a

lot of work? I've absolutely no expertise in coding

however I was hoping to start my own blog in the near future.

Anyways, should you have any suggestions or tips for new blog owners please share.

I know this is off topic however I simply had to ask.

Appreciate it!

Review my web blog: v bucks free

If some one needs expert view concerning blogging and site-building after that

i suggest him/her to pay a visit this web site,

Keep up the fastidious job.

Have a look at my blog post: black mass

https://viagracwithoutdoctor.com/ canadian mail order pharmacies
drugs for sale in mexico https://viagracwithoutdoctor.com/
top rated canadian pharmacies online https://viagracwithoutdoctor.com/

My brother recommended I might like this website.

He was entirely right. This post actually made my day. You cann't imagine simply how

much time I had spent for this info! Thanks!

my blog cheap flights

https://viagracwithoutdoctor.com/ canadian pharmaceuticals companies
canadian medications 247 https://viagracwithoutdoctor.com/
canada medication list https://viagracwithoutdoctor.com/

I couldn't refrain from commenting. Well written!

Take a look at my site; cheap flights

Wonderful, what a weblog it is! This website gives valuable facts to us,

keep it up.

Feel free to visit my homepage ... bigfish

https://viagrawwithoutdoctor.com/ canadian pharmacies stendra
canadian drug https://viagrawwithoutdoctor.com/
drugstore online shopping reviews https://viagrawwithoutdoctor.com/

https://canadianhpharmacy.com/ pharmacy canada reviews
canadian discount pharmacies in ocala fl https://canadianhpharmacy.com/
safe canadian online pharmacies https://canadianhpharmacy.com/

fantastic points altogether, you just won a new reader. What may you recommend in regards to your post that you

made a few days ago? Any positive?

My blog :: free vbucks generator

Pages

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
1 + 13 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.