One Entity per module?

3 replies [Last post]
obrignoni
Offline
Last seen: 10 years 10 weeks ago
Joined: 2011-02-28

Hello Chris,

I know you haven't started covering the use of Entities in you videos yet but i need to ask this question. Currently, I started building fairly complex module for Drupal 7 and I know that leveraging Entities is going to be essential for this project.

I was looking at one of the DrupalCon 2011 sessions that talked about writing modules for optimal performance. The presenters said that separating the parts of a large application into too many modules is going to make Drupal slower and that you should keep it to as less modules as possible.

When i started setting up the file structure for my application I realized i'm going to need to create at least 4 Entities. Usually you name your entity according to your module's name. But what if i have 4 entities specified in 1 module. The problem is that naming conventions start getting complicated.

My question is...

Is it possible / good practice to create multiple entities in 1 module?
...or should we specify 1 entity per module?

I'd appreciate any suggestions
Thanks

Chris Shattuck
Offline
Last seen: 10 weeks 3 days ago
Joined: 2009-08-16
This is a great question, my

This is a great question, my apologies for the delayed response. I plan on covering Entities before too long and will definitely explore this in the videos. Meanwhile, I'd love to hear if anyone has had experience with this as well.

Cheers!
Chris

obrignoni
Offline
Last seen: 10 years 10 weeks ago
Joined: 2011-02-28
Multiple entities can be specified in one module

I have seen some applications that are built with entities and multiple modules that work together (such as Commerce). Usually they define 1 entity per module.

I started writing a module and I can confirm that it is possible to specify multiple entities within one module.

Usually you see a "monkey.module" specify a "monkey entity" (for example). But what if a want to have a "animals.module" and specify different species (e. g. monkey, elephant, tiger, fish ...) as entities. It would look something like this.

<?php
/**
 * Implements hook_entity_info().
 */

function animals_entity_info() {
  return array(
    'monkey' => array(
      'label' => t('Monkey Entity'),
      'controller class' => 'MonkeyController',
      'base table' => 'monkey',
      'entity keys' => array(
        'id' => 'mid',
      ),
      'label callback' => 'monkey_label',
      'uri callback' => 'monkey_uri',
    ),
    'elephant' => array(
      'label' => t('Elephant Entity'),
      'controller class' => 'ElephantController',
      'base table' => 'elephant',
      'entity keys' => array(
        'id' => 'eid',
      ),
      'label callback' => 'elephant_label',
      'uri callback' => 'elephant_uri',
    ),
    // and you keep following this array structure to define the rest of the animal entities...
  );
?>

Jeje, this is just an example. The module i'm writing doesn't have anything to do with animals. I have to say that working with the Entity API and Entity module is really awesome. I'm looking forward to see those Entity videos. The cool thing is that most of the techniques we have learned through the Drupal 7 Core Concepts videos can be used for designing modules that make heavy use of Entities.

Chris Shattuck
Offline
Last seen: 10 weeks 3 days ago
Joined: 2009-08-16
Awesome, obrignoni, thank you

Awesome, obrignoni, thank you for posting that! Glad to hear you're looking forward to the Entity API videos too. :)

Cheers,
Chris

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.