Shomeya

Two web artists. One amazing company.

Articles tagged: Entities

Knowing when to use content types and when to use custom entities

from Michael Prasuhn on April 3, 2015 07:15am

Article_large_photo-1422452228503-a65068227500

Ever catch yourself staring at your editor? Then switching over and staring at the content types overview of your new Drupal site? Then back to the client requirements? And then doing it all over again and again while you face a decision? Drupal content types and fields vs. custom entities. That's the tough decision. The choices you make here will affect almost part of site building to come, but it's so hard to know which to choose.

On one hand, Drupal fields are getting better, faster and more flexible all the time with better integration with contributed modules like views. But at the same time the new entity API with Drupal 7 is more flexible than ever, allowing you to add fields to even your own custom entities. So which route should you go? How can you make a decision like this without second guessing yourself all the time?

Why getting it right is important

While lots of projects can go either way the downsides can make the difference between a successful web project and a maintenance or performance nightmare. Using content types and fields with a complex data model that requires lots of complex queries can hurt performance, but using custom entities when you don't need them can add a maintenance burden when it comes time to upgrade your site or make changes. So how do you make that decision?

Read more

A cheat sheet for hook_entity_api()

from Michael Prasuhn on March 26, 2015 11:10am

Article_large_cheat-sheet-preview

The worst time to read software documentation is when you're trying to fix something that is broken and you have no idea why. I'd say it's like shopping when you're hungry, but it's actually the opposite. When stuff breaks for no apparent reason and you're on edge it's easy to notice every little issue with the docs, and you instantly form a very strong opinion on documentation.

The good news is that as a Drupal developer, you have tons of awesome documentation just a click away on api.drupal.org or drupal.org/documentation/develop and contributed module documentation is better than ever. Even though almost everything you could ever want to know about Drupal internals is available on api.drupal.org (even the source code!) sometimes you need to combine that with contributed docs, or dig in a little deeper.

That's exactly what I had to do while working on the first few chapters of Model Your Data with Drupal. Hooks like hook_entity_info() are well documented on api.drupal.org, but the Entity API module adds it's own options to the mix. Entity API has great docs on it's options as well, but there isn't a lot out there that thoroughly documents them together, or the interaction of their options.

Read more

Introducing Model Your Data with Drupal

from Michael Prasuhn on May 28, 2013 11:40am

On the surface, Drupal makes a great content management system. Flexible fields, custom types of content, and more make it seems ideal and ready to take on any content or application requirements. Work with Drupal past the surface and soon you'll find that those dream features have very real limits and running into them can hurt. Badly.

A little history

The sheer number of different solutions that have presented themselves over the years makes it obvious that there isn't an easy one size fits all solution to data modeling. From the original node system (with types defined each in their own module), the Content Construction Kit, to Drupal 7 Fields, the solutions have always been evolving. With all the flexibility that comes with the sanctioned version there is often a tradeoff. In Drupal 7 Fields API, that tradeoff is usually performance, with large numbers of fields bringing a site to a crawl.

A better way

Imagine a system that allows you the flexibility to construct your database schema anyway that you want, one that gives you performance, and ease of imports while better representing the relations between your data. What if you could have all this without giving up the power of Drupal, with it's easily extended field types? What if you could build your site this way, knowing that you could integrate your data with any contributed module, without having to rewrite everything, or write your own integration code?

I sincerely believe that this isn't just my fantasy, but that of just about every developer out there. Being able to reuse as much code as possible (DRY) but having the flexibility to quickly control the things that matter the most to you without fighting the system you are integrating with. It kinda sounds like the future, right?

The future is here

The surprising truth is that this dream is much closer to reality than you would think. Drupal 7's Entity API is a bit of a sleeper, waiting to be fully discovered and put to work. It was added at the last minute as a way of encapsulating the ideas required by the new Field API in order to function across Nodes, Users, Taxonomy terms and in a unified way. What Drupal actually got, was the hint of core system for modeling data.

When combined with the Entity API contributed module, this system becomes much more complete, eliminating the need for repetitive code to handle things such as saving or deleting custom entities. Despite the ease of use, I've found a bit of a disconnect when talking to folks about the Entity API.

I think alot of folks don't know just how easy it is to model your data with custom entities. Or maybe they don't know how easy it is to encapsulate their domain logic within their models. Maybe the most important feature to them is being able to have control over their schema.

What next?

Whatever it is, I think it's important to get more people using the Entity API. Which is why I've decided to write Model Your Data with Drupal, a book dedicated to understanding and utilizing the Entity API in real world situations. Model Your Data with Drupal will cover the core APIs such as Entity, Fields, Schema, Forms, and more along with contributed modules such as Entity API, Views, and others. While it's not an all inclusive module development book, it will give you all the information you need to write a module that will let you simply, and easily model your data.

To learn more, and keep in touch, visit http://modelyourdatawithdrupal.com and sign up for updates for more info.

Subscribe via RSS