Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By shenagarg on
I have a custom module, which has its own tables and it runs some queries to provide the data.
Can someone help how can I create a new class in drupal custom module?
I looked into the documentation and could not find anything helpful. If someone can provide with some link, it is highly appreciated.
Thanks
Comments
Database API
You can use Database API's to fetch/insert/update your content of custom tables. A new class is not required to achieve this.
And to add to that, you can
And to add to that, you can use hook_menu() to create paths at which database events can happen, and the Form API to create forms if necessary.
I have already used hook_menu
I have already used hook_menu and Form API to add/update database records. I would like to create a class to return object when we select database records.
I have coded the past to add
I have coded the past to add/edit/delete the records in database.
Drupal system is synchronized with other system. Drupal returns the data from database in form of array of particular class. So, I need to create the class for it and not to add/update/delete records from database.
I don't get what your
I don't get what your question is. If you want to declare a new class, you can declare it:
What is the problem you are having?
There is no problem. I just
There is no problem. I just want to know if I should define class in Drupal as we do for Core PHP OR there are some other practices recommended by Drupal.
I see.
I see.
Well, the only real question is whether you want to define your class as an entity type or not. If you do, you'll want to extend DrupalDefaultEntityController, and implement hook_entity_info(). Otherwise you can just declare it as a regular class.
I want to define my class as
I want to define my class as an entity type. Is there some documentation related to DrupalDefaultEntityController to define classes? If yes, could you please let me know the link(s).
Many thanks.
You can work with this
You can work with this tutorial: http://www.sitepoint.com/build-custom-entities-drupal-setup/
It can be done !!
Hi,
First of all whatever class you are using , you need to decide the scope of functionality whether you want in the whole application as re usability or just inside your module. Because in Drupal you can access any function anywhere. So it is very important to decide the scope of your classes and functions.
It will be useful to use spl_autoload_register() function for calling classes and methods without including files and try to use name spaces as well for better code.
http://php.net/manual/en/function.spl-autoload-register.php
Nitin
Nitin
In my module, I have few functions which return array of classes. Any other module can call these functions. Scope of class and function is to whole application and not only limited to the module.