0

I want users to insert information. I can define a complex Content type with multiple fields, restricting the kind of info user is allowed to insert (integer, text…), as well as rules… and expose it as if it were a form when user clicks on 'add new info'.

But I want more complex things to happen, for example, when user submits data.

I am confused about how to achieve that (hooks in a module?).

Moreover, I have seen many tutorials around about creating forms (which imply a lot of coding for things like defining the data types, validation...that can be easily defined in Content type creation UI). Which difference would be between 'hacking' the 'add new content type' operation and creating a brand new form?

Sorry, as you may notice I am a brand new beginner in Drupal.

2 Answers 2

1

You don't usually create a custom module just to have fancy fields in a form. If it can be done with fields, you do it with fields (i.e. you use the content type creation UI). For data types that doesn't exist in the core (e.g. links and entity references) there is almost always a module that let you add fields of those types to a standard Drupal entity.

You only create a custom content type if you want to do a lot more than just capturing the data. With "more", I means such things as computing aggregates, transforming it, combining it with other data, visualizing it, etc.

My rule of thumb is: If it can be done with fields and contributed modules - do it with fields. If it can't be done with fields, create a custom module. And in that case, I let the custom module also manage the form and data validation (but built-in top of the Form API).

If you want do just want to interact with the creating, validation or deletion of an entity, there is a lot of good examples of what you can do in the entity_example.module.

For instance, to have a submit handler that does extra validation before saving, look at function entity_example_basic_form_validate.

4
  • OK. I just want to add an extra 'submit' action to the Fields UI interface (I have to submit first data to an external database, if everything is OK, let's save the new node). Which kind of hook I should use? Or should I alter the Fields UI module? Commented Jun 11, 2014 at 6:48
  • @user1249791, I've expanded by answer. Commented Jun 11, 2014 at 10:56
  • excuse my ignorance, but where should I put this code? create a module? alter the node.tpl.php (after giving a new name)? Commented Jun 11, 2014 at 21:58
  • @user1249791, you add it creating a custom module. That is why the example code snippets I linked to all are in a file where the name is ending with .module. Commented Jun 12, 2014 at 5:03
0

If you are just looking to add new fields to a content type, then you should edit it via the UI and add fields that way. If you have another type of field you'd like to add that doesn't already exist in Drupal, you can look up modules that provide the functionality you're looking for. For example, the link module provides the ability to add link fields to your entities.

If you can't find one that gives you the field type you want you can always fork a module or start one from scratch.

I recommend watching this helpful module development tutorial before you get started. You can then use the Drupal Form API reference page to help you edit forms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.