Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do use a custom datatype as a field in drupal 7?

I have several custom datatypes in drupal 7

  1. restaurant
  2. menu
  3. recipe
  4. chef

i want to create associations between this data chef -> restaurant -> menu -> recipe so that recipe can get the chefs name and the restaurants address and menu can get a list of recipes, etc

In SQL land, i'd call this a foreign key, but i'm having a lot of problems finding how to do this in drupal 7.

I suspect that there must be some module or functionality i'm not familiar with. But drupal uses it's own lingo and I think i'm falling down on my google-fu

anyone know what i'm looking for ?

like image 241
David Chan Avatar asked May 31 '11 22:05

David Chan


1 Answers

These are probably not datatypes but most likely content types. What you are looking for are

  • A way to relate nodes (ie. content) of one content type to nodes of another (chef to restaurant, menu to restaurant, recipe to menu, etc.).
  • A way to display information from related nodes (direct or indirect relations) when displaying a particular node.

The relation between nodes can be achieved using the References module (a Drupal 7 port of the nodereference module included in CCK for Drupal 6). This allow you to add references to nodes when editing another. The relation can be configured to allow only nodes of the specified type to be referenced. These kind of reference aren't bi-directional and can only (easily) navigated from the referrer node, not from the referenced node. In Drupal 6, the Node referrer provided a complementary field to navigate the relation both way. Unfortunately, it has not (yet) been ported to Drupal 7.

References may be deprecated in a near future in favor of the Entity reference module. Entity reference clains to provide bi-directional queries via views and proper integration with the Entity API module. The later should ensure that relation defined with the module are fully (problematically) navigable and usable with modules using the Entity metadata (Seach API and others).

Another solution which is also using the Entity system, is the Relation module. It provide complete bi-directional relations. These relations are themselves fieldable entitiesm which means that you add properties to the relations (for instance the dates at which a chef started and ended working at a restaurant).

Once you get the relation, there is various ways to display the related information on a node page. I don't known for Relation, but if References behave like the Drupal 6 version, it will only allow basics display of information from the directly related nodes. One solution is to implement hook_node_view() in a custom module to navigate the relation, retrieve the different nodes, format the gathered information using a custom theme hook and add it to the $node->content for rendering.

Since Entity reference integrates with view, you should be able to build a view to display the node related to the currently displayed node (using the current node as contextual argument). You can then embed the view programmatically in your node view (again, via hook_node_view()) or use a block display.

like image 52
Pierre Buyle Avatar answered Oct 30 '22 22:10

Pierre Buyle